我正在开发一款应用程序,它使用HTTP连接网址在一部Android手机上播放一首歌曲。这是我初始化和启动MediaPlayer的代码: -
Log.d(TAG, "Rohit insidePlaysong of client");
mp.reset();
Log.d(TAG, "Rohit Music player is reset");
mp.setDataSource(url);
Log.d(TAG, "Rohit Music player : data source set");
mp.setAudioStreamType(AudioManager.STREAM_MUSIC);
Log.d(TAG, "Rohit Music player audio stream type set");
//mp.prepare();
//Rohit changing the method of preparing
mp.setOnPreparedListener(new OnPreparedListener() {
@Override
public void onPrepared(MediaPlayer mp) {
// TODO Auto-generated method stub
Log.d(TAG, "Rohit inside onPrepared, Now starting Music Player");
mp.start();
}
});
mp.prepareAsync();
//mp.prepare();
//Log.d(TAG, "Rohit Music player prepared");
// TODO: make sure we have buffered REALLY
// buffered the music, currently this is a big
// HACK and takes a lot of time. We can do
// better!
/*mp.start();
mp.pause();
mp.start();
mp.pause();
mp.start();
mp.pause();
mp.start();
mp.pause();
mp.start();
mp.pause();*/
Log.d(TAG, "Rohit Music player started");
musicTimer = mActivity.retrieveTimer();
Log.d(TAG, "Rohit retrieved timer");
// let the music timer determine when to play the future playback
musicTimer.playFutureMusic(mp, startTime, startPos);
Log.d(TAG, "Rohit future music set to timer");
// Changing Button Image to pause image
// btnPlay.setImageResource(R.drawable.btn_pause);
// set Progress bar values
songProgressBar.setProgress(0);
songProgressBar.setMax(100);
// Updating progress bar
updateProgressBar();
// parsing songTitle
Log.d(TAG, "Rohit updated song progress");
这些是打印的相应日志(用“Rohit”过滤): -
12-19 19:25:42.827 463 463 D Speaker Music Player: Rohit insidePlaysong of client
12-19 19:25:42.827 463 463 D Speaker Music Player: Rohit Music player is reset
12-19 19:25:42.847 463 463 D Speaker Music Player: Rohit Music player : data source set
12-19 19:25:42.847 463 463 D Speaker Music Player: Rohit Music player audio stream type set
12-19 19:25:42.857 463 463 D Speaker Music Player: Rohit Music player started
12-19 19:25:42.857 463 463 D Speaker Music Player: Rohit retrieved timer
12-19 19:25:42.857 463 463 D Speaker Music Player: Rohit future music set to timer
12-19 19:25:42.857 463 463 D Speaker Music Player: Rohit updated song progress
正如你所看到的,“Rohit inside onPrepared,现在开始播放音乐播放器”从未准备好。
虽然从url我可以看到MediaPlayer能够正确获取songName,但是在我的代码中使用mp.getDuration()时我的日志中出现以下错误: -
12-19 19:25:42.857 463 463 V MediaPlayer: getDuration_l<br>
12-19 19:25:42.857 463 463 E MediaPlayer: Attempt to call getDuration without a valid mediaplayer
12-19 19:25:42.857 463 463 V MediaPlayer: message received msg=100, ext1=-38, ext2=0
12-19 19:25:42.857 3237 1397 V GenericSource: [Flag] set 0x8 -> mFlags = 0x8
12-19 19:25:42.857 3237 1398 V GenericSource: onPrepareAsync<br>
12-19 19:25:42.857 6089 6096 I art : Ignoring second debugger -- accepting and dropping
12-19 19:25:42.857 463 463 E MediaPlayer: error (-38, 0)<br>
12-19 19:25:42.857 463 463 V MediaPlayer: callback application
12-19 19:25:42.857 3438 3502 V BroadcastQueue: [background] Process cur broadcast BroadcastRecord{62bf8b6 u0 com.samsung.android.providers.context.log.action.USE_APP_FEATURE_SURVEY qIdx=4}, state= (APP_RECEIVE) DELIVERED for app ProcessRecord{98bbb0 6103:com.samsung.android.providers.context/u0a8}
12-19 19:25:42.857 463 463 V MediaPlayer: back from callback
12-19 19:25:42.857 463 463 V MediaPlayer-JNI: getDuration: 0 (msec)
有谁可以帮助我知道,MediaPlayer无法准备的可能原因是什么?
答案 0 :(得分:0)
在没有等待MediaPlayer
准备的情况下,您可以获得持续时间。在准备之前,您不必使用MediaPlayer
。因此,必须在prepareAsync
回调:
prepareListener
下面的代码
mp.reset();
mp.setDataSource(url);
mp.setAudioStreamType(AudioManager.STREAM_MUSIC);
mp.setOnPreparedListener(new OnPreparedListener() {
@Override
public void onPrepared(MediaPlayer mp) {
mp.start();
//any further actions on mediaPlayer here
}
});
mp.prepareAsync();