[英]Android Mediaplayer volume fluctuates
我目前正在開發一個必須播放音頻的應用程序。 我有一個按順序播放一系列.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[] 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();
}
問題在於聲音有時無法播放。 而且,當它們播放時,時間是正確的,順序是正確的, 但是最后播放的音頻資源的音量遠低於之前播放的其他聲音。
告訴我,我哪里出問題了?
如果不存儲和釋放與該曲目相關的MediaPlayer
,如何停止先前的曲目? stopMedia
如何工作?
如果MediaPlayer
處於錯誤狀態,則setVolume
將失敗。 請設置On<Event>Listeners
以便您可以真正了解以前播放的MediaPlayer
發生了什么。 特別是,您可能對實現OnInfoListener
和OnPreparedListener
感興趣。
另外, streamVolume
如何修改? 您可能在某個地方存在競爭條件,並且該代碼可能會搶先設置streamVolume
。 請檢查所有正在修改streamVolume
代碼。
只是好奇為什么你打電話
mp.setVolume(streamVolume,streamVolume);
完全沒有? 真的有必要嗎? 您沒有提到為什么需要通過代碼調整音量。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.