2015-09-04 35 views
0

我正在嘗試使用媒體錄製器錄製語音。 現在我創建新的介質記錄,然後我嘗試使用準備和啓動都無濟於事,我總是收到以下錯誤:爲什麼mediaRecorder在嘗試錄製語音時無法準備

09-04 09:27:36.335: E/MediaRecorder(26320): prepare failed: -17 
09-04 09:27:36.335: W/System.err(26320): java.io.IOException: prepare failed. 
09-04 09:27:36.350: W/System.err(26320): at android.media.MediaRecorder._prepare(Native Method) 
09-04 09:27:36.350: W/System.err(26320): at android.media.MediaRecorder.prepare(MediaRecorder.java:595) 
09-04 09:27:36.350: W/System.err(26320): at com.abg.quickies.AudioRecorder.start(AudioRecorder.java:57) 
09-04 09:27:36.350: W/System.err(26320): at com.abg.quickies.ServiceFloatingButton.recordCall(ServiceFloatingButton.java:321) 
09-04 09:27:36.350: W/System.err(26320): at com.abg.quickies.ServiceFloatingButton.handlePressAction(ServiceFloatingButton.java:254) 
09-04 09:27:36.350: W/System.err(26320): at com.abg.quickies.ServiceFloatingButton.access$1(ServiceFloatingButton.java:217) 
09-04 09:27:36.350: W/System.err(26320): at com.abg.quickies.ServiceFloatingButton$1.onTouch(ServiceFloatingButton.java:173) 
09-04 09:27:36.350: W/System.err(26320): at android.view.View.dispatchTouchEvent(View.java:3934) 
09-04 09:27:36.355: W/System.err(26320): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:903) 
09-04 09:27:36.355: W/System.err(26320): at android.view.ViewRoot.deliverPointerEvent(ViewRoot.java:2218) 
09-04 09:27:36.355: W/System.err(26320): at android.view.ViewRoot.handleMessage(ViewRoot.java:1889) 
09-04 09:27:36.360: W/System.err(26320): at android.os.Handler.dispatchMessage(Handler.java:99) 
09-04 09:27:36.360: W/System.err(26320): at android.os.Looper.loop(Looper.java:123) 
09-04 09:27:36.360: W/System.err(26320): at android.app.ActivityThread.main(ActivityThread.java:3691) 
09-04 09:27:36.360: W/System.err(26320): at java.lang.reflect.Method.invokeNative(Native Method) 
09-04 09:27:36.365: W/System.err(26320): at java.lang.reflect.Method.invoke(Method.java:507) 
09-04 09:27:36.365: W/System.err(26320): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:847) 
09-04 09:27:36.365: W/System.err(26320): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:605) 
09-04 09:27:36.370: W/System.err(26320): at dalvik.system.NativeStart.main(Native Method) 

的代碼如下:

private void recordCall() 
{ 

    Parameters params=getMyApplication().get_parameters(); 
    Date date=new Date(); 
    SimpleDateFormat formater=new SimpleDateFormat("yyyy-MM-dd-HH-mm-ss"); 
    String fileName=formater.format(date); 
    String path=params.getRecordsPath()+fileName; 

    //record 
    set_recorder(new AudioRecorder(path)); 

    try { 
     get_recorder().start(); 
     set_isRecording(true); 
     showRecorderNotification(); 
    } catch (Exception e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    }  
} 

錄音機:

public class AudioRecorder { 

    final MediaRecorder recorder = new MediaRecorder(); 
    final String path; 
    private int _audioSource; 

    /** 
    * Creates a new audio recording at the given path (relative to root of SD card). 
    * default audio source is voicecall 
    */ 
    public AudioRecorder(String path) { 
    this.path = sanitizePath(path); 
    set_audioSource(MediaRecorder.AudioSource.VOICE_CALL); 

    } 

    private String sanitizePath(String path) { 
    if (!path.startsWith("/")) { 
     path = "/" + path; 
    } 
    if (!path.contains(".")) { 
     path += ".3gp"; 
    } 
    return Environment.getExternalStorageDirectory().getAbsolutePath() + path; 
    } 

    /** 
    * Starts a new recording. 
    */ 
    public void start() throws IOException { 
    Log.i("GABI", "AudioRecorder- start()"); 

    String state = android.os.Environment.getExternalStorageState(); 
    if(!state.equals(android.os.Environment.MEDIA_MOUNTED)) { 
     throw new IOException("SD Card is not mounted. It is " + state + "."); 
    } 

    // make sure the directory we plan to store the recording in exists 
    File directory = new File(path).getParentFile(); 
    if (!directory.exists() && !directory.mkdirs()) { 
     throw new IOException("Path to file could not be created."); 
    } 

    recorder.setAudioSource(get_audioSource()); 
    recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP); 
    recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB); 
    recorder.setOutputFile(path); 
    recorder.prepare(); 
    recorder.start(); 
    } 

    /** 
    * Stops a recording that has been previously started. 
    */ 
    public void stop() throws IOException { 
    Log.i("GABI", "AudioRecorder- stop()"); 
    recorder.stop(); 
    recorder.reset(); 
    recorder.release(); 
    } 

public int get_audioSource() { 
    return _audioSource; 
} 

public void set_audioSource(int _audioSource) { 
    this._audioSource = _audioSource; 
} 

} 

這是爲什麼?

+0

<使用許可權的android:NAME = 「android.permission.WRITE_EXTERNAL_STORAGE」/> />和<使用許可權的android:NAME = 「android.permission.RECORD_AUDIO」/>在你的Manifest.xml完成 – jenuine

+0

@jenuine是的,在那裏,正在創建一個文件,但它總是空的。 –

+0

檢查你到達這裏的路徑。 'recorder.setOutputFile(path);' –

回答

0

嘗試將VOICE_CALL更改爲MIC。此外,如果它不工作,你可以檢查這部分代碼。它被測試和工作。

/* 
* The application needs to have the permission to write to external storage 
* if the output file is written to the external storage, and also the 
* permission to record audio. These permissions must be set in the 
* application's AndroidManifest.xml file, with something like: 
* 
* <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> 
* <uses-permission android:name="android.permission.RECORD_AUDIO" /> 
* 
*/ 
package com.android.audiorecordtest; 

import android.app.Activity; 
import android.widget.LinearLayout; 
import android.os.Bundle; 
import android.os.Environment; 
import android.view.ViewGroup; 
import android.widget.Button; 
import android.view.View; 
import android.view.View.OnClickListener; 
import android.content.Context; 
import android.util.Log; 
import android.media.MediaRecorder; 
import android.media.MediaPlayer; 

import java.io.IOException; 


public class AudioRecordTest extends Activity 
{ 
    private static final String LOG_TAG = "AudioRecordTest"; 
    private static String mFileName = null; 

    private RecordButton mRecordButton = null; 
    private MediaRecorder mRecorder = null; 

    private PlayButton mPlayButton = null; 
    private MediaPlayer mPlayer = null; 

    private void onRecord(boolean start) { 
     if (start) { 
      startRecording(); 
     } else { 
      stopRecording(); 
     } 
    } 

    private void onPlay(boolean start) { 
     if (start) { 
      startPlaying(); 
     } else { 
      stopPlaying(); 
     } 
    } 

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

    private void stopPlaying() { 
     mPlayer.release(); 
     mPlayer = null; 
    } 

    private void startRecording() { 
     mRecorder = new MediaRecorder(); 
     mRecorder.setAudioSource(MediaRecorder.AudioSource.MIC); 
     mRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP); 
     mRecorder.setOutputFile(mFileName); 
     mRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB); 

     try { 
      mRecorder.prepare(); 
     } catch (IOException e) { 
      Log.e(LOG_TAG, "prepare() failed"); 
     } 

     mRecorder.start(); 
    } 

    private void stopRecording() { 
     mRecorder.stop(); 
     mRecorder.release(); 
     mRecorder = null; 
    } 

    class RecordButton extends Button { 
     boolean mStartRecording = true; 

     OnClickListener clicker = new OnClickListener() { 
      public void onClick(View v) { 
       onRecord(mStartRecording); 
       if (mStartRecording) { 
        setText("Stop recording"); 
       } else { 
        setText("Start recording"); 
       } 
       mStartRecording = !mStartRecording; 
      } 
     }; 

     public RecordButton(Context ctx) { 
      super(ctx); 
      setText("Start recording"); 
      setOnClickListener(clicker); 
     } 
    } 

    class PlayButton extends Button { 
     boolean mStartPlaying = true; 

     OnClickListener clicker = new OnClickListener() { 
      public void onClick(View v) { 
       onPlay(mStartPlaying); 
       if (mStartPlaying) { 
        setText("Stop playing"); 
       } else { 
        setText("Start playing"); 
       } 
       mStartPlaying = !mStartPlaying; 
      } 
     }; 

     public PlayButton(Context ctx) { 
      super(ctx); 
      setText("Start playing"); 
      setOnClickListener(clicker); 
     } 
    } 

    public AudioRecordTest() { 
     mFileName = Environment.getExternalStorageDirectory().getAbsolutePath(); 
     mFileName += "/audiorecordtest.3gp"; 
    } 

    @Override 
    public void onCreate(Bundle icicle) { 
     super.onCreate(icicle); 

     LinearLayout ll = new LinearLayout(this); 
     mRecordButton = new RecordButton(this); 
     ll.addView(mRecordButton, 
      new LinearLayout.LayoutParams(
       ViewGroup.LayoutParams.WRAP_CONTENT, 
       ViewGroup.LayoutParams.WRAP_CONTENT, 
       0)); 
     mPlayButton = new PlayButton(this); 
     ll.addView(mPlayButton, 
      new LinearLayout.LayoutParams(
       ViewGroup.LayoutParams.WRAP_CONTENT, 
       ViewGroup.LayoutParams.WRAP_CONTENT, 
       0)); 
     setContentView(ll); 
    } 

    @Override 
    public void onPause() { 
     super.onPause(); 
     if (mRecorder != null) { 
      mRecorder.release(); 
      mRecorder = null; 
     } 

     if (mPlayer != null) { 
      mPlayer.release(); 
      mPlayer = null; 
     } 
    } 
} 
+0

不幸的是,不能回答我的問題,我需要使用VOICE_CALL作爲即時通訊嘗試在通話過程中錄製語音。我也可以在代碼中看到任何內容不同的問題,問題是導致我的代碼崩潰的原因 –

+0

如果您正在撥打電話,那麼我認爲媒體可能被鎖定?也許?我不確定。抱歉 – MAOL