2015-09-04 125 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(); 
}