2017-09-10 4 views
1

나는이 질문에 많은 시간을 보냈다. 그러나 나는 문제가있다. 내 음악 플레이어가 시작되면 첫 번째 트랙을 건너 뛰고 두 번째 트랙 재생이 자동으로 시작됩니다.미디어 플레이어에서 다음 노래를 자동으로 재생하는 방법은 무엇입니까?

정상적인 음악 지불 자처럼 행동하고 싶습니다.

public class MusicService extends Service { 

private final MediaPlayer mp = new MediaPlayer(); 

private final IBinder localBinder = new LocalBinder(); 

ArrayList<Song> songs = new ArrayList<>(); 

boolean firstAttempt = true; 



public MusicService() { 
} 

@Override 
public IBinder onBind(Intent intent) { 
    return localBinder; 

} 

public class LocalBinder extends Binder{ 
    MusicService getService(){ 
     return MusicService.this; 
    } 
} 

@Override 
public void onCreate() { 
    super.onCreate(); 
    setSongsList(); 
    initMediaPlayer(); 
} 

void initMediaPlayer(){ 
    mp.setAudioStreamType(AudioManager.STREAM_MUSIC); 
    mp.setOnCompletionListener(new MediaPlayer.OnCompletionListener() { 
     @Override 
     public void onCompletion(MediaPlayer mediaPlayer) { 
      try { 

       if(firstAttempt){ 
        firstAttempt = false; 
       }else{ 
        nextSong(); 
        playSong(); 
       } 

      } catch (Exception e) { 
       e.printStackTrace(); 
      } 
     } 
    }); 
} 


void setSongsList(){ 

    String selection = MediaStore.Audio.Media.IS_MUSIC + " != 0"; 
    String sortOrder = MediaStore.MediaColumns.DISPLAY_NAME+""; 

    String[] projection = { 
      MediaStore.Audio.Media.ARTIST, 
      MediaStore.Audio.Media.TITLE, 
      MediaStore.Audio.Media.DATA, 
      MediaStore.Audio.Media.DURATION 
    }; 


    Cursor cursor = getApplicationContext().getContentResolver().query(
      MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, 
      projection, 
      selection, 
      null, 
      sortOrder); 

    while(cursor.moveToNext()){ 
     songs.add(new Song(cursor.getString(1), cursor.getString(0), cursor.getString(3), cursor.getString(2))); 
    } 
} 

Song getPlayingSong(){ 
    return songs.get(nowPlaying); 
} 



//--------Song handlers start here-------- 

int nowPlaying = 0; 
int seekLength = 0; 

void playSong(int index) throws Exception { 
    if(index != nowPlaying){ 
     seekLength = 0; 
    } 
    nowPlaying = index; 
    playSong(); 
} 

void playSong() throws Exception { 
    mp.reset(); 
    Uri path = Uri.parse(songs.get(nowPlaying).path); 
    mp.setDataSource(String.valueOf(path)); 
    mp.prepare(); 
    mp.seekTo(seekLength); 
    mp.start(); 
} 

void pauseSong(){ 
    mp.pause(); 
    seekLength = mp.getCurrentPosition(); 
} 


void nextSong() throws Exception { 
    nowPlaying = nowPlaying+1; 
    if (nowPlaying == songs.size()){ 
     nowPlaying = 0; 
    } 
    seekLength = 0; 
    if(mp.isPlaying()){ 
     playSong(); 
    } 
} 

void prevSong() throws Exception { 
    nowPlaying = nowPlaying-1; 
    if(nowPlaying < 0){ 
     nowPlaying = songs.size()-1; 
    } 
    seekLength = 0; 
    if(mp.isPlaying()){ 
     playSong(); 
    } 
} 

void queueSong(int index){ 

} 




//-------------Returning MediaPlayer details------------- 

boolean isPlaying(){ 
    return mp.isPlaying(); 
} 

int getDuration(){ 
    return mp.getDuration(); 
} 


int getCurrentPosition(){ 
    return mp.getCurrentPosition(); 
} 


void seekTo(int length){ 
    seekLength = length; 
    mp.seekTo(length); 
} 


//------------Returning Song details---------- 

Song getSongByIndex(int index){ 
    return songs.get(index); 
} 

}

나는 이미 재생중인 경우 다음 곡을 재생합니다. 내 음악 플레이어는 응용 프로그램을 열 자마자 두 번째 노래를 재생하기 시작합니다.

+0

nextSong()을 게시하십시오. 및 playSong(); fucntions 너무. –

+0

나는 그들을 추가했다. 당신이 지금 보아도 좋다. –

+0

이 코드를 어디서 호출하는지 전체 코드를 게시해라. –

답변

0

정확하게 여기에 보관하려는 내용을 모르지만 둘 모두를 호출하는 대신 playSong();에만 전화를 시도 할 수 있습니다.

void initMediaPlayer(){ 
mp.setAudioStreamType(AudioManager.STREAM_MUSIC); 
mp.setOnCompletionListener(new MediaPlayer.OnCompletionListener() { 
    @Override 
    public void onCompletion(MediaPlayer mediaPlayer) { 
     try { 

      playSong(); 

     } catch (Exception e) { 
      e.printStackTrace(); 
     } 
    } 
}); 
} 
+0

이미 재생중인 경우 다음 노래를 재생하고 싶습니다. 내 음악 플레이어는 응용 프로그램을 열 자마자 두 번째 노래를 재생하기 시작합니다. –

0
void initMediaPlayer() { 
     mp.setAudioStreamType(AudioManager.STREAM_MUSIC); 
     mp.setOnCompletionListener(new MediaPlayer.OnCompletionListener() { 
      @Override 
      public void onCompletion(MediaPlayer mediaPlayer) { 
       try { 

        nextSong(); 
       } catch (Exception e) { 
        e.printStackTrace(); 
       } 
      } 
     }); 
     try { 
      playSong(); 
     } catch (Exception e) { 
      e.printStackTrace(); 
     } 
    } 

내 옆에 그와의 연주 벌금을 시도하십시오.