【发布时间】:2016-12-19 14:11:37
【问题描述】:
我正在开发一个应用程序,该应用程序使用 HTTP 连接 URL 将一部 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 无法准备的可能原因是什么?
【问题讨论】:
标签: android media-player android-mediaplayer