无法在Android中录制声音?

时间:2013-02-25 11:35:58

标签: android

import java.io.IOException;

import android.media.MediaPlayer;
import android.media.MediaRecorder;
import android.os.Bundle;
import android.app.Activity;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class MainActivity extends Activity implements OnClickListener{

Button mStart;
Button mStop;
Button mPlay;
Button mPlayStop;

 private static final String LOG_TAG = "AudioRecordTest";
  private static String mFileName = null;



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

    mStart = (Button)findViewById(R.id.strt);
    mStart.setOnClickListener(this);

    mStop = (Button)findViewById(R.id.stp);
    mStop.setOnClickListener(this);

    mPlay = (Button)findViewById(R.id.ply);
    mPlay.setOnClickListener(this);

    mPlayStop = (Button)findViewById(R.id.plystp);
    mPlayStop.setOnClickListener(this);
     mFileName = getFilesDir().getAbsolutePath();
        mFileName += "/audiorecordtest1111";




}






@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.activity_main, menu);
    return true;
}


@Override
public void onClick(View v) {
    switch(v.getId())
    {
    case R.id.strt:start();

                    break;
    case R.id.stp: stop();
                    break;
    case R.id.ply: play();
                break;
    case R.id.plystp:playstop();
                break;

    }
}

public void start(){
    MediaRecorder recorder = new MediaRecorder();
    recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
    recorder.setOutputFormat(MediaRecorder.OutputFormat.DEFAULT);
    recorder.setOutputFile(mFileName);
    recorder.setAudioEncoder(MediaRecorder.AudioEncoder.DEFAULT);
    try {
        recorder.prepare();
    } catch (IOException e) {
        Log.e(LOG_TAG, "prepare() failed");
    }

    recorder.start();


}
public void stop()
{
    MediaRecorder recorder = new MediaRecorder();
    recorder.stop();
    recorder.release();
    recorder = null;

}
public void play()
{
    MediaPlayer mPlayer = new MediaPlayer();
    try {
        mPlayer.setDataSource(mFileName);
        mPlayer.prepare();
        mPlayer.start();
    } catch (IOException e) {
        Log.e(LOG_TAG, "prepare() failed");
    }


}
public void playstop()
{
    MediaPlayer mPlayer = new MediaPlayer();
    mPlayer.release();
    mPlayer = null;

}


}

logcat的

02-25 11:32:18.383: E/MediaRecorder(12854): start failed: -2147483648
02-25 11:32:18.403: E/AndroidRuntime(12854): FATAL EXCEPTION: main
02-25 11:32:18.403: E/AndroidRuntime(12854): java.lang.RuntimeException: start failed.
02-25 11:32:18.403: E/AndroidRuntime(12854):    at android.media.MediaRecorder.start(Native Method)
02-25 11:32:18.403: E/AndroidRuntime(12854):    at com.example.recorder.MainActivity.start(MainActivity.java:93)
02-25 11:32:18.403: E/AndroidRuntime(12854):    at com.example.recorder.MainActivity.onClick(MainActivity.java:68)
02-25 11:32:18.403: E/AndroidRuntime(12854):    at android.view.View.performClick(View.java:4202)
02-25 11:32:18.403: E/AndroidRuntime(12854):    at android.view.View$PerformClick.run(View.java:17340)
02-25 11:32:18.403: E/AndroidRuntime(12854):    at android.os.Handler.handleCallback(Handler.java:725)
02-25 11:32:18.403: E/AndroidRuntime(12854):    at android.os.Handler.dispatchMessage(Handler.java:92)
02-25 11:32:18.403: E/AndroidRuntime(12854):    at android.os.Looper.loop(Looper.java:137)
02-25 11:32:18.403: E/AndroidRuntime(12854):    at android.app.ActivityThread.main(ActivityThread.java:5039)
02-25 11:32:18.403: E/AndroidRuntime(12854):    at java.lang.reflect.Method.invokeNative(Native Method)
02-25 11:32:18.403: E/AndroidRuntime(12854):    at java.lang.reflect.Method.invoke(Method.java:511)
02-25 11:32:18.403: E/AndroidRuntime(12854):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
02-25 11:32:18.403: E/AndroidRuntime(12854):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
02-25 11:32:18.403: E/AndroidRuntime(12854):    at dalvik.system.NativeStart.main(Native Method)

1 个答案:

答案 0 :(得分:1)

您可以尝试几种想法:

  1. 尝试将此添加到AndroidManifest.xml:

    <uses-permission android:name="android.permission.RECORD_AUDIO" />
    <uses-permission android:name="android.permission.STORAGE" />
    
  2. 如果您在模拟器上运行代码,请不要,因为它不应该工作(docs here)

  3. 如果您指望得到更多帮助,我还建议改进您的问题并添加有关您的设置的更多信息。


    编辑:这是一个完整的,有效的解决方案

    您的代码中有几件事情被破坏了:

    1. 您在每次start()/ stop()调用中都使用了MediaRecoder的新实例。你不能这样做,那些是有状态的对象。当您使用一个对象开始录制/播放时,您无法使其无效,再次实例化并调用stop()。
    2. 您正在将音频文件写入应用的缓存目录。事实证明,它已经破碎 - 修复是使用SD卡存储文件。它需要额外的许可:

      <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
      
    3. 这是代码。我删除了一些部分以使它更短一些 - 你必须填写这些地方:

          public class MainActivity extends Activity implements OnClickListener{
      
          /* TODO: Initialize buttons & other stuff here */ 
      
          private static String mFileName = null;
      
          /* NOTE: using one instance of player and recorder. */
          private MediaRecorder mRecorder;
          private MediaPlayer mPlayer;
      
          @Override
          protected void onCreate(Bundle savedInstanceState) {
              super.onCreate(savedInstanceState);
              setContentView(R.layout.activity_main);
      
              /* TODO: Buttons code here */
      
              /* NOTE: Writing to SD card, not app cache */
              mFileName = Environment.getExternalStorageDirectory().getAbsolutePath();
              mFileName += "/audiorecordtest1111";
          }
      
          @Override
          public void onClick(View v) {
              /* TODO: your old code here */
          }
      
          public void start() {
              /* NOTE: Use class variable, not local one. */
              mRecorder = new MediaRecorder();
      
              mRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
              mRecorder.setOutputFormat(MediaRecorder.OutputFormat.DEFAULT);
              mRecorder.setOutputFile(mFileName);
              mRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.DEFAULT);
              try {
                  mRecorder.prepare();
              } catch (IOException e) {
                  Log.e(LOG_TAG, "prepare() failed");
              }
      
              mRecorder.start();
          }
      
          public void stop()
          {
              /* NOTE: Use class variable, not local one. */
              mRecorder.stop();
              mRecorder.release();
              mRecorder = null;
          }
      
          public void play()
          {
              /* NOTE: Use class variable, not local one. */
              mPlayer = new MediaPlayer();
      
              try {
                  mPlayer.setDataSource(mFileName);
                  mPlayer.prepare();
                  mPlayer.start();
              } catch (IOException e) {
                  Log.e(LOG_TAG, "prepare() failed");
              }
          }
      
          public void playstop()
          {
              /* NOTE: Use class variable, not local one. */
              mPlayer.release();
              mPlayer = null;
          }
          }