我正在编写一个接一个播放文件的音乐播放器,但是当第一个文件结束时,它停止了。我尝试了一些TAG,发现问题是" OnCompletion"音频完成后不会运行。我读了一些与我的案例相似的问题/答案,但都没有。我是Java新手,所以我很困惑。任何人都会指出我的错误。这是我的代码:
public class MusicService extends Service implements MediaPlayer.OnCompletionListener{
private static MediaPlayer player = new MediaPlayer();
private ArrayList<Song> songs = new ArrayList<Song>();
private int songPos;
private boolean shuffle=false;
private Random rand;
public static final String TAG ="MyMessage";
public MusicService() {
}
public void onCreate() {
super.onCreate();
songPos = 0;
rand = new Random();
player.setOnCompletionListener(this);
Log.i(TAG,"create");
}
@Override
public void onCompletion(MediaPlayer mp) {
Log.i(TAG, "complete");
mp.reset();
if (shuffle) {
int checkSong = songPos;
while (checkSong == songPos)
songPos = rand.nextInt(songs.size());
playSong();
} else {
if (songPos == (songs.size() - 1))
songPos = 0;
else songPos++;
playSong();
}
}
public void onDestroy() {
super.onDestroy();
}
public void setList (ArrayList<Song> theSongs){
songs=theSongs;
}
public class MusicBinder extends Binder{
MusicService getService(){
return MusicService.this;
}
}
private final IBinder musicBind=new MusicBinder();
public IBinder onBind(Intent intent) {
return musicBind;
}
public boolean onUnbind(Intent intent){
return false;
}
public void setSong (int songIndex){
songPos=songIndex;
}
public void clickPlay (){
Log.i(TAG,"clickplay");
if(shuffle){
songPos=rand.nextInt(songs.size());
clickLV();
}
else
clickLV();
}
public void playSong(){
Song playSong = songs.get(songPos);
long currentSong = playSong.getID();
Uri trackUri = ContentUris.withAppendedId(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,currentSong);
player= MediaPlayer.create(getApplicationContext(),trackUri);
player.start();
}
public void clickLV(){
player.stop();
player.release();
playSong();
}
public void clickNext(){
player.stop();
if(shuffle){
int checkSong = songPos;
while (checkSong==songPos)
songPos=rand.nextInt(songs.size());
playSong();
}
else{
if(songPos==(songs.size()-1))
songPos=0;
else songPos++;
playSong();
}
}
public void clickPre(){
player.stop();
if(shuffle){
int checkSong = songPos;
while (checkSong==songPos)
songPos=rand.nextInt(songs.size());
playSong();
}
else{
if(songPos==0)
songPos=(songs.size()-1);
else songPos--;
playSong();
}
}
public void clickBF(){
player.seekTo(player.getCurrentPosition()-5000);
}
public void clickFF(){
player.seekTo(player.getCurrentPosition()+5000);
}
public void setShuffle(){
if(shuffle) shuffle=false;
else shuffle=true;
和Logcat:
========================================
03-13 07:14:10.722 273-10470/? D/FFMPEG: android source close
03-13 07:14:10.722 273-10470/? D/FFmpegExtractor: SniffFFMPEG failed to sniff this source
03-13 07:14:10.725 10298-10298/sample.musicplayersample3 E/MediaPlayer: Should have subtitle controller already set
03-13 07:14:10.726 273-273/? D/NuPlayerDriver: start(0xef6e3c40)
03-13 07:14:10.727 273-10469/? I/GenericSource: start
03-13 07:14:10.737 10298-10298/sample.musicplayersample3 E/MediaPlayer: Should have subtitle controller already set
03-13 07:14:10.758 273-10476/? D/AudioSink: bufferCount (8) is too small and increased to 12
03-13 07:14:12.609 273-644/? D/NuPlayerDriver: seekTo(0xef6e3c40) 242512 ms
03-13 07:14:12.627 273-10469/? I/NuPlayer: audio discontinuity (formatChange=0, time=1)
03-13 07:14:25.669 273-10470/? E/MP3Extractor: Unable to resync. Signalling end of stream.
03-13 07:14:27.245 273-10470/? E/MP3Extractor: Unable to resync. Signalling end of stream.
03-13 07:14:28.240 273-616/? I/AudioFlinger: BUFFER TIMEOUT: remove(4097) from active list on thread 0xf380100
只有第一个文件才能获得这些行&#34;无法重新同步。信号流结束。&#34;