所以我有一個按鈕,當按下時會播放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();
}
發佈應用程序退出時出現的錯誤。 –
也許嘗試先捕捉任何異常..看看你得到什麼樣的錯誤 –