2016-11-19 58 views
0

所以我有一個按鈕,當按下時會播放audiotrack,然後再次按下時停止audiotrack。當我點擊按鈕時,它會退出應用程序。我不知道爲什麼。請幫忙!如何使用按鈕啓動audiotrack並停止audiotrack?

public void Playbutton(View view) { 

      File file = new File(Environment.getExternalStorageDirectory(), "test.pcm"); 

      int shortSizeInBytes = Short.SIZE/Byte.SIZE; 

      int bufferSizeInBytes = (int) (file.length()/shortSizeInBytes); 
      short[] audioData = new short[bufferSizeInBytes]; 

      try { 
       InputStream inputStream = new FileInputStream(file); 
       BufferedInputStream bufferedInputStream = new BufferedInputStream(inputStream); 
       DataInputStream dataInputStream = new DataInputStream(bufferedInputStream); 

       int i = 0; 
       while (dataInputStream.available() > 0) { 
        audioData[i] = dataInputStream.readShort(); 
        i++; 
       } 

       dataInputStream.close(); 

       AudioTrack audioTrack = new AudioTrack(
         AudioManager.STREAM_MUSIC, 
         11025, 
         AudioFormat.CHANNEL_IN_MONO, 
         AudioFormat.ENCODING_PCM_16BIT, 
         bufferSizeInBytes, 
         AudioTrack.MODE_STREAM); 

       if (playbuttonstatus){ 
       playBtn.setText(getString(R.string.stop)); 
       audioTrack.play(); 
       audioTrack.write(audioData, 0, bufferSizeInBytes); 
        playbuttonstatus = false;} 
       else{ 
        playBtn.setText(getString(R.string.play)); 
        playbuttonstatus = true; 
        audioTrack.pause(); 
        audioTrack.flush();} 
      } catch (FileNotFoundException e){ 
       e.printStackTrace(); 
      } catch (IOException e) { 
       e.printStackTrace(); 
      } 
+0

發佈應用程序退出時出現的錯誤。 –

+0

也許嘗試先捕捉任何異常..看看你得到什麼樣的錯誤 –

回答

0

在您的項目中使用此代碼。我認爲它會幫助你:

Boolean audiopaused=true; 

    Button play.setOnclickListener(new Onclicklistener){ 
    if (audiopaused){ 
     audio.start(); 
     }esle{ 
     audio.stop(); 
    } 
    } 
0

現在你總是運行相同的初始化,不管playbuttonstatus的狀態如何。因此,在第一次開始播放一個AudioTrack實例後,再次按下按鈕,然後創建另一個新實例AudioTrack,並嘗試停止它,儘管它尚未播放。這可能會導致IllegalStateException。 (而且你也無法停止原來的一個。)

你可以有audioTrack作爲類的playButton()方法以外的成員變量:

private AudioTrack audioTrack; 

然後你就可以改變你的代碼,以便同AudioTrack已啓動並停止。

public void playButton(View view) { 

    if (playbuttonstatus){ 
     File file = new File(Environment.getExternalStorageDirectory(), "test.pcm"); 

     int shortSizeInBytes = Short.SIZE/Byte.SIZE; 

     int bufferSizeInBytes = (int) (file.length()/shortSizeInBytes); 
     short[] audioData = new short[bufferSizeInBytes]; 

     try { 
      InputStream inputStream = new FileInputStream(file); 
      BufferedInputStream bufferedInputStream = new BufferedInputStream(inputStream); 
      DataInputStream dataInputStream = new DataInputStream(bufferedInputStream); 

      int i = 0; 
      while (dataInputStream.available() > 0) { 
       audioData[i] = dataInputStream.readShort(); 
       i++; 
      } 

      dataInputStream.close(); 

      audioTrack = new AudioTrack(
        AudioManager.STREAM_MUSIC, 
        11025, 
        AudioFormat.CHANNEL_IN_MONO, 
        AudioFormat.ENCODING_PCM_16BIT, 
        bufferSizeInBytes, 
        AudioTrack.MODE_STREAM); 

      playBtn.setText(getString(R.string.stop)); 
      audioTrack.play(); 
      audioTrack.write(audioData, 0, bufferSizeInBytes); 
       playbuttonstatus = false; 

     } catch (FileNotFoundException e){ 
      e.printStackTrace(); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 
    } 
    else { 
     playBtn.setText(getString(R.string.play)); 
     playbuttonstatus = true; 
     audioTrack.pause(); 
     audioTrack.flush(); 
    } 
} 
+0

非常感謝。我還發現,出於某種原因,AudioFormat.CHANNEL_IN_MONO必須更改爲AudioFormat.CHANNEL_CONFIGURATION_MONO。當我將它設置爲CHANNEL_CONFIGURATION_MONO時,它表示它已被棄用。你有什麼想法我應該做什麼?這是一件大事嗎?我相信它會導致按鈕響應滯後。 –

+0

'CHANNEL_IN_MONO'用於錄音,'CHANNEL_OUT_MONO'用於播放,所以只需使用'CHANNEL_OUT_MONO'而不是棄用的'CHANNEL_CONFIGURATION_MONO'。 –