android:使用单个媒体播放器对象播放音频文件并显示图像

发布于 2025-01-01 21:33:21 字数 2046 浏览 2 评论 0原文

我需要依次显示图像并播放相应的音频文件。

我尝试创建多个媒体播放器对象,但它显示 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

冷心人i 2025-01-08 21:33:21

这是我的建议:

首先,将 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. 
}
});

希望有帮助

Here is my suggestion:

First of all, put you MP3 files in assets not raw. This code will play from assets:

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. 
}
});

Hope that helps

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文