3
是否可以播放原始資源文件夾中的AudioTrack
的wav文件?它嘗試了很多方法來引用我的Android項目中的原始文件,但我有很多例外。AudioTrack Android從原始文件夾播放聲音
這是我現在擁有的,但是當我按下按鈕時,沒有聲音播放。
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Button buttonPlayAudioTrack = (Button) findViewById(R.id.buttonPlayAudioTrack);
buttonPlayAudioTrack.setOnClickListener(this);
int minBufferSize = AudioTrack.getMinBufferSize(44100, AudioFormat.CHANNEL_CONFIGURATION_MONO,
AudioFormat.ENCODING_PCM_16BIT);
audioTrack = new AudioTrack(AudioManager.STREAM_MUSIC, 44100, AudioFormat.CHANNEL_CONFIGURATION_MONO,
AudioFormat.ENCODING_PCM_16BIT, minBufferSize, AudioTrack.MODE_STREAM);
}
public void onClick(View v) {
// TODO Auto-generated method stub
if(v.getId() == R.id.buttonPlayAudioTrack)
playSound();
}
private void playSound() {
audioTrack.play();
int i = 0;
int bufferSize = 512;
byte [] buffer = new byte[bufferSize];
InputStream inputStream = getResources().openRawResource(R.raw.piano12);
try {
while((i = inputStream.read()) != -1)
audioTrack.write(buffer, 0, i);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
inputStream.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
你好,謝謝,我已經固定它;) – sNore