2016-08-01 122 views
-2

如何暫停和恢復錄音。在這裏,如果我暫停記錄並再次開始記錄,那麼它應該從暫停位置開始並保存在SD卡中。如何暫停和恢復錄音

回答

0

嘗試這個............

MediaRecorder recorder = new MediaRecorder(); 
recorder.setAudioSource(MediaRecorder.AudioSource.MIC); 
recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP); 
recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB); 
recorder.setOutputFile(PATH_NAME); 
recorder.prepare(); 
recorder.start(); 
recorder.stop(); 
recorder.reset();  
recorder.release(); 
+0

我已經習慣這一點,但它再次啓動從暫停狀態開始狀態沒有記錄。我想從暫停狀態開始。 – Chitra

0

使用下面的代碼,記得在AndroidManifest.xml中添加RECORD_AUDIO和WRITE_EXTERNAL_STORAGE權限。

在XML佈局:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical" 
    android:paddingBottom="@dimen/activity_vertical_margin" 
    android:paddingLeft="@dimen/activity_horizontal_margin" 
    android:paddingRight="@dimen/activity_horizontal_margin" 
    android:paddingTop="@dimen/activity_vertical_margin" 
    tools:context=".MainActivity"> 

    <Button 
     android:id="@+id/btnRecord" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_gravity="center_horizontal" 
     android:text="Record" /> 

    <Button 
     android:id="@+id/btnPlay" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_gravity="center_horizontal" 
     android:text="Play" /> 

</LinearLayout> 

在Java代碼:

public class MainActivity extends AppCompatActivity { 

    private static final String TAG = "my_log"; 
    Button btnRecord, btnPlay; 
    public static String audioPath = ""; 

    private MediaRecorder myAudioRecorder; 
    private boolean isRecording = false; 

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

     btnRecord = (Button) findViewById(R.id.btnRecord); 
     btnPlay = (Button) findViewById(R.id.btnPlay); 
     btnPlay.setEnabled(false); 

     btnRecord.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       if (!isRecording) { 
        btnPlay.setEnabled(false); 

        // If it exists, delete it 
        File file = new File(audioPath); 
        if (file.exists()) { 
         file.delete(); 
        } 

        // this is directory path to store my audio 
        File dirPath = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + "/MyAudio/"); 
        dirPath.mkdirs(); // make this as directory 

        String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date()); 
        String audioFileName = "audio_" + timeStamp + ".3gp"; 
        audioPath = dirPath + "/" + audioFileName; 
        Log.i(TAG, "audioPath = " + audioPath); 

        isRecording = true; 
        btnRecord.setText("Stop"); 
        recordAudio(); // call this method to record 
       } else { 
        stopRecordAudio(); 
        isRecording = false; 
        btnRecord.setText("Record"); 
        btnPlay.setEnabled(true); 
       } 
      } 
     }); 

     btnPlay.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       playAudio(audioPath); 
      } 
     }); 
    } 

    public void recordAudio() { 
     try { 
      myAudioRecorder = new MediaRecorder(); 
      myAudioRecorder.setAudioSource(MediaRecorder.AudioSource.MIC); 
      myAudioRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP); 
      myAudioRecorder.setAudioEncoder(MediaRecorder.OutputFormat.AMR_NB); 
      myAudioRecorder.setOutputFile(audioPath); 

      myAudioRecorder.prepare(); 
      myAudioRecorder.start(); 
     } catch (IllegalStateException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } catch (IOException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 

     Toast.makeText(getApplicationContext(), "is recording...", Toast.LENGTH_LONG).show(); 
    } 

    public void stopRecordAudio() { 
     myAudioRecorder.stop(); 
     myAudioRecorder.release(); 

     Toast.makeText(getApplicationContext(), "Audio recorded successfully", Toast.LENGTH_LONG).show(); 
    } 

    public void playAudio(String audioPath) { 
     MediaPlayer m = new MediaPlayer(); 

     try { 
      m.setDataSource(audioPath); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 

     try { 
      m.prepare(); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 

     m.start(); 
     Toast.makeText(MainActivity.this, "Playing audio...", Toast.LENGTH_SHORT).show(); 
    } 

}