2015-09-04 175 views
-3

當試圖運行此代碼時發現運行時異常, 嘗試了很多事情,但每次都失敗。運行時異常代碼

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

    outputFile = Environment.getExternalStorageDirectory().getAbsolutePath() + "/javacodegeeksRecording.3gpp"; 
    myRecorder = new MediaRecorder(); 
    myRecorder.setAudioSource(MediaRecorder.AudioSource.MIC); 
    myRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP); 
    myRecorder.setAudioEncoder(MediaRecorder.OutputFormat.AMR_NB); 
    myRecorder.setOutputFile(outputFile); 

    text = (TextView) findViewById(R.id.text1); 
    startBtn = (Button)findViewById(R.id.start); 
    stopBtn = (Button)findViewById(R.id.stop); 
    playBtn = (Button)findViewById(R.id.play); 
    stopPlayBtn = (Button)findViewById(R.id.stopPlay); 
} 

public void a(View aa) { 
    start(aa); 
} 

public void b(View bb) { 
    stop(bb); 
} 

public void c(View cc) { 
    play(cc); 
} 

public void d(View dd) { 
    stopPlay(dd); 
} 

public void start(View view) { 
    try { 
     myRecorder.prepare(); 
     myRecorder.start(); 
    } 
    catch (IllegalStateException e) { 
     e.printStackTrace(); 
    } 
    catch (IOException e) { 
     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(); 
    } 
    catch (IllegalStateException e) { 
     e.printStackTrace(); 
    } 
    catch (RuntimeException e) { 
     e.printStackTrace(); 
    } 
} 

public void play(View view) { 
    try { 
     myPlayer = new MediaPlayer(); 
     myPlayer.setDataSource(outputFile); 
     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) { 
     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) { 
     e.printStackTrace(); 
    } 
} 
} 
+0

在哪裏堆棧跟蹤? –

回答

0

這段代碼對我沒有任何意義。當你不使用它們時,爲什麼你將視圖傳遞給你的函數?

public void start(View view) {... //view not used } 

是的,我們無法幫助您,沒有堆棧跟蹤。

我唯一的假設是,你需要在每次使用功能爲null檢查你的mplayer,就像你在stopPlay做()

if (myPlayer != null) { 
    mPlayer.doStuff(); 
}