无法在Nexus 5上打两个MediaPlayer的在同一时间 [英] Unable to play two MediaPlayer at same time in Nexus 5

查看:147
本文介绍了无法在Nexus 5上打两个MediaPlayer的在同一时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试播放使用两个 MediaPlayers 服务里的一些背景声音。一切工作正常,如果我想打只有一个MP3,但如果我尝试在同一时间打两场,使用的MediaPlayer 的两个实例,只有最后一个作品。这是奇怪,因为它工作在模拟器和一些设备,但我无法使其在一台Nexus 5。这项工作是在code我使用创造了两个的MediaPlayer 实例:

I am trying to play some audio in background using two MediaPlayers inside a Service. Everything works fine if I want to play only one mp3, but If I try to play two at the same time, using two instances of MediaPlayer, only the last one works. It is weird because it works in emulator and some devices, but I am unable to make it work in a Nexus 5. This is the code I am using to create the two MediaPlayer instances:

private void playWithPiano() {
    mp1 = initializePlayer(filenameVoz); // filenameVoz is the mp3 file name in raw folder
    mp2 = initializePlayer("base_piano");
    mp1.start();
    mp2.start();
}

private MediaPlayer initializePlayer(String filename) {
    int identifier = getResources().getIdentifier(filename,
            "raw", getPackageName());
    MediaPlayer mp = MediaPlayer.create(this, identifier);

    mp.setOnErrorListener(new OnErrorListener() {
        @Override
        public boolean onError(MediaPlayer mp, int what, int extra) {
            Log.d(TAG_LOG, "OnErrorListener");
            stop();

            return false;
        }
    });
    mp.setOnCompletionListener(new OnCompletionListener() {
        @Override
        public void onCompletion(MediaPlayer mp) {
            stop();
        }
    });

    return mp;
}

如果我评论的mp2.start线,那么我可以听的音频MP1。这就像当一个MediaPlayer正在启动,其他任何情况下都停止我会AP preciate任何帮助。

If I comment the mp2.start line, then I can listen the mp1 audio. It is like when a MediaPlayer is started, any other instances are stopped I would appreciate any help with this.

推荐答案

我发现描述的Nexus 5设备相同问题的其他职位,但没有一个解决办法呢。
的https://$c$c.google.com/p /安卓/问题/细节?ID = 63099
的https://$c$c.google.com/p /安卓/问题/细节?ID = 62304
我试着用seekTo(0)就像有人提出这些论坛之一,但它没有工作,所以我想这似乎是对我的情况下具体的解决方法的另一件事。

I found other posts describing the same problem in Nexus 5 devices, but don't have a solution yet. https://code.google.com/p/android/issues/detail?id=63099 https://code.google.com/p/android/issues/detail?id=62304 I tried using seekTo(0) like it was suggested in one of those forums, but it didn't work, so I tried another thing that seems like an specific workaround for my case.

我使用这个服务了,里面我的第二个的MediaPlayer 仅播放背景音乐,使我开始这半秒后的第一个和它的工作。

I am using this inside a Service, and my second MediaPlayer is only to play some background music, so I started it half second after the first one and it worked.

new java.util.Timer().schedule( 
                new java.util.TimerTask() {
                    @Override
                    public void run() {
                        if (mp2 != null) mp2.start();
                    }
                }, 
                500
        );

我仍然不知道为什么,我只能在Nexus 5上有这个问题,但我希望这可以帮助别人。

I still don't know why I am having this problem only in the Nexus 5, but I hope this may help somebody.

这篇关于无法在Nexus 5上打两个MediaPlayer的在同一时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆