2017-03-04 72 views
-1

我只有關於android的基本知識。今天我開發了一個音頻錄製應用程序,但問題是用戶可以錄製音頻,停止錄製並可以播放音頻,但播放完後當用戶再次按下錄製按鈕時,應用程序崩潰,任何人都可以幫助我?嘗試在空對象引用上調用虛擬方法'void android.media.MediaRecorder.prepare()'

這是我的主要活動

package com.hackerinside.jaisonjoseph.sample_recorder; 

    import android.media.MediaPlayer; 
    import android.media.MediaRecorder; 
    import android.os.Bundle; 
    import android.os.Environment; 
    import android.provider.MediaStore; 
    import android.support.design.widget.FloatingActionButton; 
    import android.support.design.widget.Snackbar; 
    import android.support.v7.app.AppCompatActivity; 
    import android.support.v7.widget.Toolbar; 
    import android.view.View; 
    import android.view.Menu; 
    import android.view.MenuItem; 
    import android.widget.Button; 
    import android.widget.Toast; 

    import java.io.IOException; 

public class MainActivity extends AppCompatActivity { 

private Button play,stop,record; 
private MediaRecorder myAudioRecorder; 
private String outputfile; 


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

    play=(Button)findViewById(R.id.play); 
    stop=(Button)findViewById(R.id.stop); 
    record=(Button)findViewById(R.id.record); 
    stop.setEnabled(false); 
    play.setEnabled(false); 


    outputfile= Environment.getExternalStorageDirectory().getAbsolutePath()+"/recording_"+System.currentTimeMillis()+".mp3"; 



    myAudioRecorder=new MediaRecorder(); 
    myAudioRecorder.setAudioSource(MediaRecorder.AudioSource.MIC); 
    myAudioRecorder.setOutputFormat(MediaRecorder.OutputFormat.DEFAULT); 
    myAudioRecorder.setAudioEncoder(MediaRecorder.OutputFormat.DEFAULT); 
    myAudioRecorder.setOutputFile(outputfile); 

record.setOnClickListener(new View.OnClickListener() { 
    @Override 
    public void onClick(View view) { 

     try { 

      myAudioRecorder.prepare(); 
      myAudioRecorder.start(); 
    } 
    catch (IllegalStateException ise){ 



    }catch (IOException ioe){ 



     } 
      record.setEnabled(false); 
      stop.setEnabled(true); 
      Toast.makeText(getApplicationContext(),"record startded",Toast.LENGTH_LONG).show(); 

    } 

}); 





    stop.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View view) { 



      myAudioRecorder.stop(); 
      record.setEnabled(true); 
      myAudioRecorder.release(); 
      myAudioRecorder=null; 
      stop.setEnabled(false); 
      play.setEnabled(true); 
      Toast.makeText(getApplicationContext(),"recorded audio",Toast.LENGTH_LONG).show(); 

     } 

    }); 
      play.setOnClickListener(new View.OnClickListener() { 
       @Override 
       public void onClick(View view) { 
        MediaPlayer mediaPlayer=new MediaPlayer(); 

        try { 
          mediaPlayer.setDataSource(outputfile); 
          mediaPlayer.prepare(); 
          mediaPlayer.start(); 
          Toast.makeText(getApplicationContext(),"playing audio",Toast.LENGTH_LONG).show(); 
    } 
    catch (Exception e) 
    { 
// here 
    } 

} 
    }); 








    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); 
    setSupportActionBar(toolbar); 

    FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab); 
    fab.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View view) { 
      Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG) 
        .setAction("Action", null).show(); 
     } 
    }); 
} 

@Override 
public boolean onCreateOptionsMenu(Menu menu) { 
    // Inflate the menu; this adds items to the action bar if it is present. 
    getMenuInflater().inflate(R.menu.menu_main, menu); 
    return true; 











} 

@Override 
public boolean onOptionsItemSelected(MenuItem item) { 
    // Handle action bar item clicks here. The action bar will 
    // automatically handle clicks on the Home/Up button, so long 
    // as you specify a parent activity in AndroidManifest.xml. 
    int id = item.getItemId(); 

    //noinspection SimplifiableIfStatement 
    if (id == R.id.action_settings) { 
     return true; 
    } 

    return super.onOptionsItemSelected(item); 
} 
} 
+1

你可以告訴我的解決方案,而不是找到副本? –

+0

你的問題是缺少LogCat,你也發佈了很多不需要的代碼。只發布相關的代碼部分可以幫助你吸引更多的人幫助你。請閱讀[如何創建最小,完整和可驗證示例](http://stackoverflow.com/help/mcve)。 – Sufian

回答

1

嘗試在

record.setOnClickListener(new View.OnClickListener() { 
@Override 
public void onClick(View view) { 

    try { 
     myAudioRecorder=new MediaRecorder(); 
     myAudioRecorder.prepare(); 
     myAudioRecorder.start(); 
} 

添加此

myAudioRecorder=new MediaRecorder(); 

或者U可以從停止按鈕點擊刪除myAudioRecorder.release();

應該在onPause()您的活動發佈

+0

當按下停止按鈕時,它崩潰 –

+0

您是否刪除了外部初始化'myAudioRecorder = new MediaRecorder();'? – rafsanahmad007

0

如果你看看documentations它說你沒有訪問記錄你releasd後,這是你在做什麼,當你按下停止,你有實例化新的刻錄機或者根本不釋放它。

1

那是因爲,你將它設置null而停止mediaRecorder

stop.setOnClickListener(new View.OnClickListener() { 
    @Override 
    public void onClick(View view) { 
     myAudioRecorder.stop(); 
     record.setEnabled(true); 
     myAudioRecorder.release(); 
     myAudioRecorder=null; 
     stop.setEnabled(false); 
     play.setEnabled(true); 
     Toast.makeText(getApplicationContext(),"recorded audio",Toast.LENGTH_LONG).show(); 
    } 

}); 

當您記錄再次點擊此意願得到執行,

record.setOnClickListener(new View.OnClickListener() { 
    @Override 
    public void onClick(View view) { 

     try { 
      // myAudioRecorder is null when you come up second time. 
      // Initialize myAudioRecorder to prevent an exception. 
      myAudioRecorder.prepare(); 
      myAudioRecorder.start(); 
     } catch (IllegalStateException ise){ 

     } catch (IOException ioe) { 

     } 
     record.setEnabled(false); 
     stop.setEnabled(true); 
     Toast.makeText(getApplicationContext(),"record startded",Toast.LENGTH_LONG).show(); 
    } 
}); 

myAudioRecorder.prepare();將通過NullPointerException as myAudioRecorder is null
儘管上面的代碼在try.. catch中,但您沒有注意到NullPointerException,應用程序將崩潰。

相關問題