android:使用单个媒体播放器对象播放音频文件并显示图像
我需要依次显示图像并播放相应的音频文件。
我尝试创建多个媒体播放器对象,但它显示 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();
}
});
i need to display images and play corresponding audio files one after the other.
i tried to create multiple media player objects but it shows error(1, -17)
so plz help guys i am running out of time.
Right now i have a handler which plays audio file and then get the duration of that audio file while playing another audio file this is my approach but i am unable to play some 10 files
below is my sample code
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);
EDITED THIS MY code
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();
}
});
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这是我的建议:
首先,将 MP3 文件放入非原始资源中。该代码将从资产中播放:
希望有帮助
Here is my suggestion:
First of all, put you MP3 files in assets not raw. This code will play from assets:
Hope that helps