我需要一个接一个地显示图像并播放相应的音频文件。
我尝试创建多个媒体播放器对象,但它显示error(1, -17)
所以请帮助我们的时间不多了。
现在我有一个播放音频文件的处理程序,然后在播放另一个音频文件时获取该音频文件的持续时间这是我的方法,但我无法播放10个文件
下面是我的示例代码
img.setBackgroundResource(R.drawable.d002_p001);
d002_ae_p001 = MediaPlayer.create(this, R.raw.d002_ae_p001);
handler.postDelayed(new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
d002_ae_p001.start();
xml();
}
},1000);
d002_ae_p002 = MediaPlayer.create(this,R.raw.d002_ae_p002);
handler.postDelayed(new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
i++;
img.setBackgroundResource(R.drawable.d002_p002);
d002_ae_p002.start();
xml();
}
},d002_ae_p001.getDuration()+2000);
编辑此代码
int image[] = {R.drawable.apple,R.drawable.boy,R.drawable.cat};
String audio[]={"apple_v.mp3","boy_v.mp3","cat_v.mp3"};
MediaPlayer mp = new MediaPlayer();
AssetFileDescriptor descriptor;
try {
descriptor = getAssets().openFd(audio[i]);
mp.setDataSource( descriptor.getFileDescriptor(), descriptor.getStartOffset(),descriptor.getLength());
descriptor.close();
mp.prepare();
mp.start();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
mp.setOnCompletionListener(new OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer arg0) {
// TODO Auto-generated method stub
img.setImageResource(image[i]);
i++;
mp.start();
}
});
答案 0 :(得分:1)
这是我的建议:
首先,将MP3文件放在非原始资产中。此代码将从资产中播放:
MediaPlayer mp = new MediaPlayer();
AssetFileDescriptor descriptor;
descriptor = getAssets().openFd( "filename.mp3" );
mp.setDataSource( descriptor.getFileDescriptor(), descriptor.getStartOffset(), descriptor.getLength() );
descriptor.close();
mp.prepare();
mp.start();
mp.setOnCompletionListener(
new OnCompletionListener(){
@Override
public void onCompletion(MediaPlayer mp) {
//code here to change your image, suggest having an array of filenames and incrementing
//each time you pass through.
}
});
希望有所帮助