2017-07-14 67 views
0

我的代码:语音录制节目

public class MainActivity extends AppCompatActivity { 

    private Button mRecordBtn; 
    private TextView mRecordLabel; 

    private MediaRecorder mRecorder; 

    private String mFileName = null; 

    private static final String LOG_TAG = "Record_log"; 

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

     mRecordLabel = (TextView) findViewById(R.id.recordLabel); 
     mRecordBtn = (Button) findViewById(R.id.recordBtn); 

     mFileName = Environment.getExternalStorageDirectory().getAbsolutePath(); 
     mFileName = "/recorded_audio.3gp"; 


     mRecordBtn.setOnTouchListener(new View.OnTouchListener() { 
      @Override 
      public boolean onTouch(View v, MotionEvent motionEvent) { 

       if(motionEvent.getAction() == MotionEvent.ACTION_DOWN){ 

        startRecording(); 
        mRecordLabel.setText("Recording in Progress"); 


       } 
       else if (motionEvent.getAction() == MotionEvent.ACTION_UP){ 
        stopRecording(); 
        mRecordLabel.setText("Recording Stopped"); 

       } 
       return false; 
      } 
     }); 

    } 

    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(); //I am getting a bug on this line 
    } 

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


    } 
} 

,当我跑在我的三星Galaxy S7边缘这段代码,以及由机器人工作室的应用程序崩溃给出的Nexxus 7模拟器。我发布了所有的代码,因为我不知道它究竟在哪里搞乱了。当我按下按钮时它崩溃。它作为一个按住记录,并释放停止录制

+1

请张贴您的错误记录。 – ZeekHuge

+0

[可惜MyApp已停止。我怎么能解决这个问题?](https://stackoverflow.com/questions/23353173/unwanted-myapp-has-stopped-how-can-i-solve-this) –

+0

你可以从字面上标记任何应用程序崩溃的问题(所以可能是大多数Android问题)作为那一个的重复 – Markaos

回答

0

检查是否有权限录制音频。

+0

是的,我确定了这两件事情,我运行它的应用程序有录音权限 –