玩MediaPlayer的多首歌曲在同一时间:只有一个是真正玩 [英] Playing multiple songs with MediaPlayer at the same time: only one is really playing

查看:152
本文介绍了玩MediaPlayer的多首歌曲在同一时间:只有一个是真正玩的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在的Andr​​oid

我应该用在完全相同的时间玩三个音频轨道 Android.Media.MediaPlayer

I am supposed to play three audio tracks at the exact same time using Android.Media.MediaPlayer.

昨天我设法使其工作这样做的:

Yesterday I managed to make it work doing so :

MediaPlayer track1 = MediaPlayer.create(this, R.raw.track1);
MediaPlayer track2 = MediaPlayer.create(this, R.raw.track2);
MediaPlayer track3 = MediaPlayer.create(this, R.raw.track3);

正如你所看到的,我的三distincts实例这里 MediaPlaye 的r

As you can see, I have three distincts instances of MediaPlayer here.

由于我是问,我需要有这些的MediaPlayer 后台线程正在播放

Because I am asked to, I need to have these MediaPlayer being played in a background thread.

下面是我开始部分 MediaPlayers

//let's suppose this code snippet is in a run() method inside a Thread (which is the case)
track1.start();
track2.start();
track3.start();

如果我昨天说的,这是因为第二天,它并没有像预想的那样工作。

If I say yesterday, it's because the next day, it did not work as supposed to.

事实上,开始的MediaPlayer似乎停止任何previous播放的MediaPlayer

我在调试模式下测试:显然, track2.start()停止TRACK1的MediaPlayer的,遵循相同的模式, track3.start()停止tack2的MediaPlayer的。

I tested in debugger mode : clearly, track2.start() stops track1's MediaPlayer and following the same pattern, track3.start() stops tack2's MediaPlayer.

因此​​,在年底,仅TRACK3正在播放,我听不到任何previous曲目,无论音量设置,而中的我可以克利里听到所有的人面前:它应该建立某种形式的氛围

So at the end, only track3 is being played, I cannot hear any of the previous tracks, whatever the volume settings are whereas I could cleary hear all of them before : it is supposed to create some sort an ambiance.

当然,改变其轨道已启动不会改变任何东西的顺序:只有最后一首​​曲目将被听到。

Of course, changing the order in which the tracks are started does not change anything : only the last track will be heard.

如果我说一听到这是因为在调试模式下,检查房子 MediaPlayer.isPlaying 正在返回的 真正 所有三名球员都这么说他们是在玩,但只有一个可以听到...

If I say "heard" it is because in debugger mode, checking the property MediaPlayer.isPlaying is returning true : all three players are saying they are playing but only one can be heard... !

为什么要改变呢?
为什么会工作,一旦停止以后的工作?

Why the change ? Why did it work once to stop working afterwards ?

注意:


  • 的音频文件都在相同的持续时间是大约15分钟左右

  • 音频文件均 .MP3 文件COM pressed到 .ACC 文件(从 320kbps的 144kbps的

  • 我在 Xamarin工作室工作在 C#但clarety purpouse我会坚持小的Java code片段。

  • the audio files have all the same duration which is approximately about 15 minutes
  • the audio files were .mp3 files compressed into .acc files (from 320kbps to 144kbps)
  • I am working under Xamarin Studio in C# but for clarety purpouse I will stick to small Java code snippets.

修改1

根据该同时我的解决办法应该是工作,是吧?

According to this Play two mp3 song at the same time my solution should be working, right ?

推荐答案

好吧,我相信我发现我可以说出治标不治本。

Well, I believe I found what I could name "a temporary solution".

MediaPlayer track1 = MediaPlayer.create(this, R.raw.track1);
track1.play();
MediaPlayer track2 = MediaPlayer.create(this, R.raw.track2);
track2.play();
MediaPlayer track3 = MediaPlayer.create(this, R.raw.track3);
track3.play();

这里的问题是,创建()方法创建时,正在播放的歌曲有明显的差距。

The problem with this is that the create() method creates a noticeable gap when the songs are being played.

因此​​,这是不是。

过了一会儿,我试过如下:

After a while, I tried the following :

MediaPlayer track1 = MediaPlayer.create(this, R.raw.track1);
track1.play();
track1.pause();
MediaPlayer track2 = MediaPlayer.create(this, R.raw.track2);
track2.play();
track2.pause();
MediaPlayer track3 = MediaPlayer.create(this, R.raw.track3);
track3.play();
track3.pause();

// resuming...
track1.play(); 
track2.play();
track3.play();

这whay,歌曲都比较同步的。我仍然能听到一个非常小的差距,但它是更好的方式。
我发现这个解决方案很奇怪和丑陋的。

That whay, the songs are more synchronous. I can still hear a very small gap but it is way better. I found this solution quite strange as well as ugly.

除非有人有另外一种想法,我会坚持与解决方案。

Unless someone has another idea, I will stick with that solution.

这篇关于玩MediaPlayer的多首歌曲在同一时间:只有一个是真正玩的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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