MediaPlayer.OnPreparedListener не работает
Для воспроизведения музыки с удаленного сервера все работает, когда я использую audioPlayer.prepare();
все работает, но я играю песню с удаленного сервера, поэтому я должен использовать
audioPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
@Override
public void onPrepared(MediaPlayer mp) {
mp.start();
}
});
audioPlayer.prepareAsync();
Но когда я использую это, музыка не играет, также я замечаю, что MediaPlayer.OnPreparedListener не вызывает.
Код:
public boolean playAudio(SongDetail mSongDetail) {
if (mSongDetail == null) {
return false;
}
if ((audioTrackPlayer != null || audioPlayer != null) && MusicPreferance.playingSongDetail != null && mSongDetail.getId() == MusicPreferance.playingSongDetail.getId()) {
if (isPaused) {
resumeAudio(mSongDetail);
}
return true;
}
if (audioTrackPlayer != null) {
MusicPlayerService.setIgnoreAudioFocus();
}
cleanupPlayer(!playMusicAgain, false);
playMusicAgain = false;
File file = null;
try {
audioPlayer = new MediaPlayer();
audioPlayer.setAudioStreamType(useFrontSpeaker ? AudioManager.STREAM_VOICE_CALL : AudioManager.STREAM_MUSIC);
audioPlayer.setDataSource("http://admin.kobyzbook.kz/Audios/2016915164335.mp3");
audioPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer mediaPlayer) {
MusicPreferance.playingSongDetail.audioProgress = 0.0f;
MusicPreferance.playingSongDetail.audioProgressSec = 0;
if (!MusicPreferance.playlist.isEmpty() && MusicPreferance.playlist.size() > 1) {
playNextSong(true);
} else {
cleanupPlayer(true, true);
}
}
});
/* audioPlayer.prepare();
audioPlayer.start();*/
audioPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
@Override
public void onPrepared(MediaPlayer mp) {
mp.start();
}
});
audioPlayer.prepareAsync();
startProgressTimer();
} catch (Exception e) {
if (audioPlayer != null) {
audioPlayer.release();
audioPlayer = null;
isPaused = false;
MusicPreferance.playingSongDetail = null;
}
return false;
}
Журнал
09-20 18:56:40.757 7788-7788/com.dmplayer E/MediaPlayer: Attempt to call getDuration without a valid mediaplayer
09-20 18:56:40.757 7788-7788/com.dmplayer E/MediaPlayer: error (-38, 0)
09-20 18:56:40.763 7788-7788/com.dmplayer E/MPlugin: Unsupported class: com.mediatek.common.telephony.IOnlyOwnerSimSupport
09-20 18:56:40.769 7788-7806/com.dmplayer I/System.out: [OkHttp] sendRequest>>
09-20 18:56:40.770 7788-7806/com.dmplayer I/System.out: [OkHttp] sendRequest<<
09-20 18:56:40.827 7788-7788/com.dmplayer E/MediaPlayer: Attempt to call getDuration without a valid mediaplayer
09-20 18:56:40.828 7788-7788/com.dmplayer E/MediaPlayer: error (-38, 0)
09-20 18:56:40.829 7788-7788/com.dmplayer E/MediaPlayer: Attempt to call getDuration without a valid mediaplayer
09-20 18:56:40.830 7788-7788/com.dmplayer E/MediaPlayer: error (-38, 0)
09-20 18:56:40.874 7788-7788/com.dmplayer E/MediaPlayer: Error (-38,0)
09-20 18:56:40.874 7788-7788/com.dmplayer E/MediaPlayer: stop called in state 0
09-20 18:56:40.874 7788-7788/com.dmplayer E/MediaPlayer: error (-38, 0)
09-20 18:56:40.875 7788-7788/com.dmplayer I/System.out: [CDS]close[47061]
09-20 18:56:40.875 7788-7788/com.dmplayer I/NetworkManagementSocketTagger: untagSocket(51)
09-20 18:56:40.897 7788-7788/com.dmplayer W/MediaPlayer: mediaplayer went away with unhandled events
09-20 18:56:40.897 7788-7788/com.dmplayer W/MediaPlayer: mediaplayer went away with unhandled events
09-20 18:56:40.924 7788-7788/com.dmplayer W/MediaPlayer: mediaplayer went away with unhandled events
1 ответ
Что работает для меня ниже одного. Я также подготовил приложение, где я должен воспроизводить видео с сервера
private class Player extends AsyncTask<String, Void, Boolean> {
@Override
protected void onPreExecute() {
super.onPreExecute();
checkPreConditions();
}
@Override
protected Boolean doInBackground(String... params) {
try {
mediaPlayer.setDataSource(params[0]);
mediaPlayer.prepareAsync();
mediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
@Override
public void onPrepared(MediaPlayer mp) {
mp.start();
audioTimeInSec = mp.getDuration() / 1000;
// Log.e("Time", mp.getDuration() + " :)");
}
});
// Log.e("player", "in bg");
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(Boolean aBoolean) {
super.onPostExecute(aBoolean);
// Log.e("player", "in post");
if (TestViewFragmentNew.this.isDetached() || mediaPlayer == null) return;
mediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer mp) {
mp.reset();
mp.release();
});
}
}
Проверьте метод PreCondition
private void checkPreConditions(){
if (mediaPlayer != null) {
if (mediaPlayer.isPlaying()) {
mediaPlayer.stop();
}
mediaPlayer.release();
mediaPlayer= null;
}
}
Где вы хотите использовать его, используйте как ниже:
String URL = "http://admin.kobyzbook.kz/Audios/2016915164335.mp3;
mediaPlayer = new MediaPlayer();
mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
playerService = new Player(objj);
playerService.execute(URL);