我正在建立一个音乐播放器,通过音乐服务在线播放。
曲目列表在onServiceConnected
中设置,歌曲播放成功。
但是,当我点击prev
和next
按钮时,会加载下一首曲目,并在一段时间后加载第一首曲目。下一首曲目的URL
不是问题,因为它在点击播放时播放正常。
此外,mediaplayer
能够成功播放onCompletionListener中的下一首曲目,使用相同的代码播放下一个按钮。
以下是我的活动中播放下一个按钮的代码段。
public void playNext() {
musicService.playNext();
if (playbackPaused)
playbackPaused = false;
setSelected();
if (musicService != null && musicBound && musicService.isPlaying()) {
Runnable notification = new Runnable() {
public void run() {
primarySeekBarProgressUpdater();
}
};
handler.postDelayed(notification, 0);
}
}
以下是我的音乐服务中接下来播放的代码段
public void playNext() {
if (shuffle) {
int newSong = position;
while (newSong == position) {
newSong = random.nextInt(tracks.size());
}
position = newSong;
} else {
if (position == tracks.size() - 1) {
position = 0;
} else {
position++;
}
}
playSong();
}
playSong()函数如下
public void playSong() {
if (player != null) {
if (isPaused) {
startPlayer();
isPaused = false;
} else {
player.reset();
Track track = tracks.get(position);
try {
player.setDataSource(track.getUrl());
Log.e("My App", "Music Service: Tracks Size: " + tracks.size());
Log.e("My App", "Music Service: Track Position: " + position);
Log.e("My App", "Music Service: Track Name: " + track.getTitle());
Log.e("My App", "Music Service: Player Data Source: " + track.getUrl());
} catch (Exception e) {
Log.e("My App", "Music Player Service: Error setting data source", e);
}
player.prepareAsync();
progressBar.setVisibility(View.VISIBLE);
}
}
}
以下是第一次成功播放曲目时的日志
E/My App: Music Service: Tracks Size: 2
E/My App: Music Service: Track Position: 0
E/My App: Music Service: Track Name: Omo Pastor
E/My App: Music Service: Player Data Source: https://www.360nobs.com/?
dl_id=2535.mp3
D/My App: MusicPlayerActivity: Track Position(Play Song): 0
D/My App: MusicPlayerActivity: Track Position: 0
及以下是单击下一个按钮时的日志。
E/My App: Music Service: Tracks Size: 2
E/My App: Music Service: Track Position: 1
E/My App: Music Service: Track Name: Bad Gang
E/My App: Music Service: Player Data Source: https://www.360nobs.com/?
dl_id=15552.mp3
D/My App: MusicPlayerActivity: Track Position: 1
V/MediaHTTPService: makeHTTPConnection: CookieHandler
(java.net.CookieManager@ae50224) exists.
V/MediaHTTPService: makeHTTPConnection(android.media.MediaHTTPService@67c56f3): cookieHandler: java.net.CookieManager@ae50224 Cookies: null
E/MediaPlayerNative: Attempt to call getDuration in wrong state: mPlayer=0x8f11e7c0, mCurrentState=4
E/MediaPlayerNative: error (-38, 0)
E/MediaPlayerNative: Attempt to call getDuration in wrong state: mPlayer=0x8f11e7c0, mCurrentState=0
E/MediaPlayerNative: error (-38, 0)
D/skia: --- SkAndroidCodec::NewFromStream returned null
I/chatty: uid=10082(com.capsella_io.ordiox) glide-disk-cach identical 6 lines
D/skia: --- SkAndroidCodec::NewFromStream returned null
D/skia: --- SkAndroidCodec::NewFromStream returned null
I/chatty: uid=10082(com.capsella_io.ordiox) glide-disk-cach identical 6 lines
D/skia: --- SkAndroidCodec::NewFromStream returned null
E/MediaPlayer: Error (-38,0)
V/MediaPlayer: resetDrmState: mDrmInfo=null mDrmProvisioningThread=null mPrepareDrmInProgress=false mActiveDrmScheme=false
V/MediaPlayer: cleanDrmObj: mDrmObj=null mDrmSessionId=null
E/MediaPlayerNative: error (-38, 0)
V/MediaPlayer: resetDrmState: mDrmInfo=null mDrmProvisioningThread=null mPrepareDrmInProgress=false mActiveDrmScheme=false
V/MediaPlayer: cleanDrmObj: mDrmObj=null mDrmSessionId=null
X: Music Service: Shuffle: false
V/MediaPlayer: resetDrmState: mDrmInfo=null mDrmProvisioningThread=null mPrepareDrmInProgress=false mActiveDrmScheme=false
V/MediaPlayer: cleanDrmObj: mDrmObj=null mDrmSessionId=null
V/MediaHTTPService: MediaHTTPService(android.media.MediaHTTPService@4efee29): Cookies: null
E/My App: Music Service: Tracks Size: 2
E/My App: Music Service: Track Position: 0
E/My App: Music Service: Track Name: Omo Pastor
E/My App: Music Service: Player Data Source: https://www.360nobs.com/?dl_id=2535.mp3