Android Mediaplayer音量波动
我目前正在开发一个必须播放音频的应用程序。我有一种方法可以按顺序播放一系列.mp3。方法如下:Android Mediaplayer音量波动,android,audio,android-mediaplayer,volume,Android,Audio,Android Mediaplayer,Volume,我目前正在开发一个必须播放音频的应用程序。我有一种方法可以按顺序播放一系列.mp3。方法如下: /** * Plays a series of sounds in order without delay. * * @param audioResourceIds * An in-order array of the audio asset resources to play. * */ public void playQueuedPlaylist(int[] audi
/**
* Plays a series of sounds in order without delay.
*
* @param audioResourceIds
* An in-order array of the audio asset resources to play.
* */
public void playQueuedPlaylist(int[] audioResourceIds)
{
float lastPlayedSoundDuration;
for(int i = 0; i<audioResourceIds.length; i++)
{
lastPlayedSoundDuration = playMedia(audioResourceIds[i], null);
try
{
Thread.sleep((long)lastPlayedSoundDuration);
}
catch(Exception eh)
{
Log.v("BulacsAlmighty","Exception caught at playQueuedPlaylist()");
}
}
}
/**
* Used to play voice overs
*
* @param index
* id of the sound
* @param listener
* on completion listener
*/
public int playMedia(int index, OnCompletionListener listener)
{
MediaPlayer mp = MediaPlayer.create(context, index);
mp.setVolume(streamVolume, streamVolume);
stopMedia(index); // Stops the sound if it still is playing.
mp.start();
return mp.getDuration();
}
/**
*毫不延迟地按顺序播放一系列声音。
*
*@param audioResourceIds
*要播放的音频资源的顺序数组。
* */
public void playQueuedPlaylist(int[]audioResourceIds)
{
浮动lastPlayedSoundDuration;
对于(int i=0;i如果您没有存储和释放与该曲目相关的MediaPlayer
,您如何停止以前的曲目?stopMedia
如何工作
setVolume
将在MediaPlayer
处于错误状态时失败。请设置onListener
,以便您能够真正了解以前播放的MediaPlayer
的情况。特别是,您可能有兴趣实现OnInfoListener
和OnPreparedListener
。
另外,streamVolume
是如何修改的?您可能在某个地方有一个竞争条件,并且该代码可能正在抢先设置streamVolume
。请检查所有正在修改的代码streamVolume
,只是好奇为什么要打电话
mp.setVolume(streamVolume,streamVolume)
有吗?真的有必要吗?您没有提到为什么需要根据代码调整音量。您是否在外部音乐播放器中播放这些MP3,以验证它们是否都是相同的音量?事实上,我有。我甚至有测试按钮,可以单独播放每个声音。它们都以相同的音量播放,但在t中使用时除外上面提到的方法的上下文。只是为了调试的目的,如果你切换曲目的顺序会怎么样?那么它仍然会是最后一个更安静的音量吗?是的,我也试过了,它仍然保持原样:(哦,关于这一点。我在其他地方有这段代码,音量会根据播放的其他声音动态增加或减少。在你引用的代码中,streamVolume是1。我也试着注释这一行,但没有用。