简体   繁体   English

Android MediaPlayer的音量,平移和多个过程

[英]Volume, Panning, and Multiple Processes of Android's MediaPlayer

I had three tasks I wanted to do: 1. Get multiple mediaplayer's running different audio streams. 我要做三个任务:1.让多个Mediaplayer运行不同的音频流。 2. Change the volume of these audio streams. 2.更改这些音频流的音量。 3. Change the left and right ear volume's independently (ie, panning) 3.分别更改左右耳的音量(即平移)

After searching around for a while, I found no satisfactory answer. 经过一会搜索,我没有找到满意的答案。

For 1., most people suggested using SoundPool (my original intuition) or AudioTrack's (the latter of which is a bloody nightmare since it means one basically has to build a whole new mediaplayer). 对于1.,大多数人建议使用SoundPool(我的原始直觉)或AudioTrack的(后者是一场血腥的噩梦,因为这意味着基本上必须构建一个全新的媒体播放器)。

For 2., everyone seemed to have a spaz and keep talking about AudioManager, which is literally a different problem (in most of the contexts I saw, including mine). 对于2.,每个人似乎都有争论,并且一直在谈论AudioManager,这实际上是一个不同的问题(在我看到的大多数情况下,包括我的情况)。 It also cannot deal with 3. 它也不能处理3。

For 3., I found almost nothing with occasional reference to AudioTrack's. 对于3.,我偶然发现AudioTrack几乎没有发现任何东西。

Well, I came up with an incredibly simple solution, which I figured would be useful to put up here. 好吧,我想出了一个非常简单的解决方案,我认为将其放在这里很有用。

The solution is that you can use multiple MediaPlayer's with independent left/right volumes for each track using .setVolume(left, right). 解决方案是,您可以使用.setVolume(left,right)为每个轨道使用具有独立左/右音量的多个MediaPlayer。 However, and this is absolutely crucial ... 但是,这绝对至关重要 ...

It will not work with all audio formats. 不适用于所有音频格式。 At present, I have got .wav files to work completely while .mp3's will not work (in any of the 3 respects). 目前,.wav文件可以完全工作,而.mp3则不能工作(从这三个方面来看)。

I hope this saves some of you some of the time I just wasted accidentally coming up with a solution. 我希望这可以节省您一些时间,因为我只是在无意中浪费了时间想出一个解决方案。

As a caveat, this worked on a Nexus 5 running API 23, so I can make no promises for other contexts. 需要注意的是,这可以在运行API 23的Nexus 5上使用,因此我无法保证其他情况。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM