kkxiaoxian 2017-04-08 11:55 采纳率: 0%
浏览 1982
已结题

安卓的录音程序,运行时闪退求解

按照教程写了个安卓的录音程序,在点击结束录音时闪退,求大神帮看看代码,感谢

闪退时报错日志
04-08 19:47:29.702: D/ViewRootImpl(11731): ViewPostImeInputStage processPointer 0
04-08 19:47:29.802: D/ViewRootImpl(11731): ViewPostImeInputStage processPointer 1
04-08 19:47:29.802: E/MediaRecorder(11731): stop called in an invalid state: 4
04-08 19:47:29.802: D/AndroidRuntime(11731): Shutting down VM
04-08 19:47:29.802: E/AndroidRuntime(11731): FATAL EXCEPTION: main
04-08 19:47:29.802: E/AndroidRuntime(11731): Process: com.example.recorder, PID: 11731
04-08 19:47:29.802: E/AndroidRuntime(11731): java.lang.IllegalStateException
04-08 19:47:29.802: E/AndroidRuntime(11731): at android.media.MediaRecorder._stop(Native Method)
04-08 19:47:29.802: E/AndroidRuntime(11731): at android.media.MediaRecorder.stop(MediaRecorder.java:976)
04-08 19:47:29.802: E/AndroidRuntime(11731): at com.example.recorder.MainActivity.stop(MainActivity.java:102)
04-08 19:47:29.802: E/AndroidRuntime(11731): at com.example.recorder.MainActivity$2.onClick(MainActivity.java:61)
04-08 19:47:29.802: E/AndroidRuntime(11731): at android.view.View.performClick(View.java:5698)
04-08 19:47:29.802: E/AndroidRuntime(11731): at android.widget.TextView.performClick(TextView.java:10896)
04-08 19:47:29.802: E/AndroidRuntime(11731): at android.view.View$PerformClick.run(View.java:22565)
04-08 19:47:29.802: E/AndroidRuntime(11731): at android.os.Handler.handleCallback(Handler.java:739)
04-08 19:47:29.802: E/AndroidRuntime(11731): at android.os.Handler.dispatchMessage(Handler.java:95)
04-08 19:47:29.802: E/AndroidRuntime(11731): at android.os.Looper.loop(Looper.java:148)
04-08 19:47:29.802: E/AndroidRuntime(11731): at android.app.ActivityThread.main(ActivityThread.java:7224)
04-08 19:47:29.802: E/AndroidRuntime(11731): at java.lang.reflect.Method.invoke(Native Method)
04-08 19:47:29.802: E/AndroidRuntime(11731): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
04-08 19:47:29.802: E/AndroidRuntime(11731): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)
04-08 19:47:32.042: I/Process(11731): Sending signal. PID: 11731 SIG: 9

DEBUG
线程 [main](已暂挂(异常 IllegalStateException))

MediaRecorder._stop() 行: 不可用 [本机方法]

MediaRecorder.stop() 行: 976
MainActivity.stop() 行: 102

MainActivity$2.onClick(View) 行: 61

Button(View).performClick() 行: 5698
Button(TextView).performClick() 行: 10896

View$PerformClick.run() 行: 22565

Handler.handleCallback(Message) 行: 739

ViewRootImpl$ViewRootHandler(Handler).dispatchMessage(Message) 行: 95

Looper.loop() 行: 148

ActivityThread.main(String[]) 行: 7224

Method.invoke(Object, Object...) 行: 不可用 [本机方法]

ZygoteInit$MethodAndArgsCaller.run() 行: 1230

ZygoteInit.main(String[]) 行: 1120

主程序
package com.example.recorder;

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import java.io.IOException;
import android.media.MediaRecorder;
import android.media.AudioFormat;
import android.media.AudioRecord;
import android.os.Bundle;
import android.os.Environment;
import android.app.Activity;
import android.util.Log;
import android.widget.Button;
import java.io.BufferedOutputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class MainActivity extends Activity {

MediaRecorder  audioRecorder;
Button START;
Button END;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    //creat
START=(Button)findViewById(R.id.start);
END=(Button)findViewById(R.id.end);

START.setOnClickListener(new View.OnClickListener()
{
    public void onClick(View arg0)
    {
    record();
    }
});

END.setOnClickListener(new View.OnClickListener()
{
    public void onClick(View arg0)
    {

        stop();
    }
});
}

public void record()
{
if(audioRecorder==null)
{
    audioRecorder=new MediaRecorder();
}
String path=Environment.getExternalStorageDirectory().getAbsolutePath()+"/test.mp4";
File file=new File(path);
if(file.exists())
    file.delete();
try
{
    file.createNewFile();
    audioRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
    audioRecorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
    audioRecorder.setAudioEncodingBitRate(MediaRecorder.AudioEncoder.AMR_NB);
    audioRecorder.setOutputFile(path);
    audioRecorder.prepare();
    audioRecorder.start();
}
catch(IOException e)
{
    e.printStackTrace();
}
catch (IllegalStateException e)
{
    e.printStackTrace();
}
START.setText("recording");
}


public void stop()
{
if(audioRecorder!=null)
{
        audioRecorder.stop();
        audioRecorder.release();
        audioRecorder=null;
}
START.setText("录音");
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();
    if (id == R.id.action_settings) 
    {
        return true;
    }
    return super.onOptionsItemSelected(item);
}
}

MANIFEXT:
<?xml version="1.0" encoding="utf-8"?>
package="com.example.recorder"
android:versionCode="1"
android:versionName="1.0" >
android:minSdkVersion="19"
android:targetSdkVersion="19" />




android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
android:name=".MainActivity"
android:label="@string/app_name" >






  • 写回答

2条回答 默认 最新

  • xiaofanwei123 2017-04-08 13:25
    关注

    activity 没注册 或者 没在配置文件中 给权限。

    评论

报告相同问题?

悬赏问题

  • ¥15 要给毕业设计添加扫码登录的功能!!有偿
  • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥15 stable diffusion
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件