本文介绍了AudioTrack的Android播放的声音从原文件夹的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
是它可以从资源文件夹中的原始播放wav文件AudioTrack?它试了很多办法来引用我的Android项目的原始文件,但我得到了很多例外。
这是我现在有,但是,当I $上的按钮p $ PSS,无声音播放。
公共无效的onCreate(包savedInstanceState){
super.onCreate(savedInstanceState);
按钮buttonPlayAudioTrack =(按钮)findViewById(R.id.buttonPlayAudioTrack);
buttonPlayAudioTrack.setOnClickListener(本);
INT minBufferSize = AudioTrack.getMinBufferSize(44100,AudioFormat.CHANNEL_CONFIGURATION_MONO,
AudioFormat.ENCODING_PCM_16BIT);
audioTrack =新AudioTrack(AudioManager.STREAM_MUSIC,44100,AudioFormat.CHANNEL_CONFIGURATION_MONO,
AudioFormat.ENCODING_PCM_16BIT,minBufferSize,AudioTrack.MODE_STREAM);
}
公共无效的onClick(视图v){
// TODO自动生成方法存根
如果(v.getId()== R.id.buttonPlayAudioTrack)
playSound();
}
私人无效playSound(){
audioTrack.play();
INT I = 0;
INT缓冲区大小= 512;
byte []的缓冲区=新的字节[BUFFERSIZE]
InputStream中的InputStream = getResources()openRawResource(R.raw.piano12)。
尝试 {
而((ⅰ= inputStream.read())!= - 1)
audioTrack.write(缓冲液,0,i)的;
}赶上(IOException异常E){
// TODO自动生成的catch块
e.printStackTrace();
}
尝试 {
inputStream.close();
}赶上(IOException异常E){
// TODO自动生成的catch块
e.printStackTrace();
}
}
解决方案
一切都很好,你的code,你只需要改变
,而((I = inputStream.read())!= -1)
到
,而((I = inputStream.read(缓冲))!= -1)
is it possible to play wav files with AudioTrack from the resources folder raw? It tried many ways to reference the raw files in my android project, but i got many Exceptions.
this is what i have now, but when i press on the button, no sound is playing.
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();
}
}
解决方案
Everything is fine with your code, You just need to change
while((i = inputStream.read()) != -1)
to
while((i = inputStream.read(buffer)) != -1)
这篇关于AudioTrack的Android播放的声音从原文件夹的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!