Android媒体声音录音机启动失败

时间:2014-08-22 13:50:35

标签: android eclipse android-mediaplayer

每当按下开始按钮,我的应用程序就崩溃了。 Logcat说它因为启动方法失败了。我谷歌错误,但我没有找到任何东西。它在本机方法Start(v);

中给出异常

这是我的logcat:

08-22 18:44:23.420: E/MediaRecorder(3607): start failed: -2147483648
08-22 18:44:23.420: V/MediaRecorderJNI(3607): process_media_recorder_call
08-22 18:44:23.420: W/dalvikvm(3607): threadid=1: thread exiting with uncaught exception (group=0x41234438)
08-22 18:44:23.420: E/AndroidRuntime(3607): FATAL EXCEPTION: main
08-22 18:44:23.420: E/AndroidRuntime(3607): java.lang.RuntimeException: start failed.

这是我的代码:

import java.io.File;
import java.io.IOException;

import android.media.AudioFormat;
import android.media.AudioManager;
import android.media.AudioTrack;
import android.media.MediaPlayer;
import android.media.MediaRecorder;
import android.media.SoundPool;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.app.Activity;
import android.content.ContentResolver;
import android.content.ContentValues;
import android.content.Intent;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity {

     private MediaRecorder myRecorder;
       private MediaPlayer myPlayer;
       private File outputFile = null;
       private AudioTrack mAudioTrack;
       private Button startBtn;
       private Button stopBtn;
       private Button playBtn;
       private Button stopPlayBtn;
       private Spinner sp;
       private TextView text;
       public SoundPool spl;
       public int explosion = 0;
       private Button playMod;
       private int sampleRate = 8000;
       private Uri newUri;
       AudioManager audioManager;
       int counter;
       float actVolume, maxVolume, volume;
       boolean loaded = false;

       private static final String TAG = "SoundRecordingActivity";
       String [] singers = {"Atif Aslam" , "Arijit Singh" , "Shreya Goshal"};

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

          text = (TextView) findViewById(R.id.text1);
          sp = (Spinner)findViewById(R.id.spinner1);

          ArrayAdapter<String> adp=new ArrayAdapter<String>(this,
               android.R.layout.simple_dropdown_item_1line,singers);

               adp.setDropDownViewResource(android.R.layout.simple_dropdown_item_1line);
               sp.setAdapter(adp);
          // store it to sd card
         //outFile = Environment.getExternalStorageDirectory().
            //    getAbsolutePath() + "/AudioRecord.3gpp";

         File sampleDir = Environment.getExternalStorageDirectory();
          try {
              outputFile = File.createTempFile("sound", ".m4a", sampleDir);
          } catch (IOException e) {
              Toast.makeText(this, "No Memory Card Inserted", Toast.LENGTH_LONG).show();
              Log.e(TAG, "sdcard access error");
              return;
          }


          myRecorder = new MediaRecorder();
          myRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
          myRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
          myRecorder.setAudioEncoder(MediaRecorder.OutputFormat.AMR_NB);
          myRecorder.setOutputFile(outputFile.getAbsolutePath());

          startBtn = (Button)findViewById(R.id.start);
          startBtn.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                start(v);
            }
          });

          stopBtn = (Button)findViewById(R.id.stop);
          stopBtn.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                stop(v);
            }
          });

          playBtn = (Button)findViewById(R.id.play);
          playBtn.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                    play(v);    
            }
          });

          stopPlayBtn = (Button)findViewById(R.id.stopPlay);
          stopPlayBtn.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                stopPlay(v);
            }
          });

          playMod = (Button)findViewById(R.id.button1);
          playMod.setOnClickListener(new OnClickListener(){

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
            playModified(v);    
            }


          });
       }

       public void start(View view){
           try {
              myRecorder.prepare();
              myRecorder.start();
           } catch (IllegalStateException e) {
              // start:it is called before prepare()
              // prepare: it is called after start() or before setOutputFormat() 
              e.printStackTrace();
           } catch (IOException e) {
               // prepare() fails
               e.printStackTrace();
            }

           text.setText("Recording Point: Recording");
           startBtn.setEnabled(false);
           stopBtn.setEnabled(true);

           Toast.makeText(getApplicationContext(), "Start recording...", 
                   Toast.LENGTH_SHORT).show();
       }




       public void stop(View view){
           try {
              myRecorder.stop();
              myRecorder.release();
              myRecorder  = null;

              stopBtn.setEnabled(false);
              playBtn.setEnabled(true);
              text.setText("Recording Point: Stop recording");

              Toast.makeText(getApplicationContext(), "Stop recording...",
                      Toast.LENGTH_SHORT).show();
              /////////////////////////////////////
             // addRecordingToMediaLibrary();
             ////////////////////////////////////// 
           } catch (IllegalStateException e) {
                //  it is called before start()
                e.printStackTrace();
           } catch (RuntimeException e) {
                // no valid audio/video data has been received
                e.printStackTrace();
           }
       }

       public void play(View view) {
           try{
               myPlayer = new MediaPlayer();
               myPlayer.setDataSource(outputFile.getAbsolutePath());

               myPlayer.prepare();
               myPlayer.start();

               playBtn.setEnabled(false);
               stopPlayBtn.setEnabled(true);
               text.setText("Recording Point: Playing");

               Toast.makeText(getApplicationContext(), "Start play the recording...", 
                       Toast.LENGTH_SHORT).show();
           } catch (Exception e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
       }

       public void stopPlay(View view) {
           try {
               if (myPlayer != null) {
                   myPlayer.stop();
                   myPlayer.release();
                   myPlayer = null;
                   playBtn.setEnabled(true);
                   stopPlayBtn.setEnabled(false);

                   text.setText("Recording Point: Stop playing");

                   Toast.makeText(getApplicationContext(), "Stop playing the recording...", 
                           Toast.LENGTH_SHORT).show();


               }
           } catch (Exception e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
       }



}

2 个答案:

答案 0 :(得分:0)

将临时文件保存为类型m4a:

 outputFile = File.createTempFile("sound", ".m4a", sampleDir);

但输出格式错误(3gp):

myRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);

<强>解决方案:

如果您想将录制的文件保存为3gp格式,则必须将文件另存为:

 outputFile = File.createTempFile("sound", ".3gp", sampleDir);

否则,如果您想以m4a格式保存,则必须按如下方式更改输出格式:

  myRecorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4); 

这里有list个可用格式

答案 1 :(得分:0)

我有同样的问题。因为我也有MediaPlayer,所以可以通过以下代码解决此问题:

mediaPlayer.setAudioStreamType(AudioManager.ADJUST_LOWER);

这解决了我的问题。