I am working on an app which plays a song present on one android phone onto another using HTTP connection url. This is my code to initialize and start 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");
And these are the corresponding logs printed (filtered with "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
As you can see, "Rohit inside onPrepared, Now starting Music Player" is never prepared.
Although from url I can see that MediaPlayer is able to fetch songName properly, but I am getting the following error in my logs while using mp.getDuration() in my code:-
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)
Can anyone please help me know, what are the possible reasons for this unability of MediaPlayer to prepare?
You are triyng to get duration without waiting for MediaPlayer
to prepare. You don't have to use MediaPlayer
until it prepared. So code below prepareAsync
must be invoked in prepareListener
callback:
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();