2017-10-16 4 views
-1

안녕하세요 모두 내가 노래를 재생할 수있는 미디어 플레이어가 있습니다. 노래를 끝내면 재생 버튼 토글이 멈 춥니 다. 버튼을 다시 클릭하여 노래를 재생할 때마다 다시 듣고 싶습니다. 시간과 나는 그것을 수행하는 방법을 모르겠다. 노래가 재생 버튼을 끝내면 버튼을 멈추게 토글하므로 미디어 플레이어를 재설정하고 데이터 소스를 다시 설정하는 법을 알고 있으므로 코드를 점검하여 알려주십시오. 어디에 두어야합니까?안드로이드 미디어 플레이어 재생 노래 다시

이것은 내 코드로 도움을 주시고 감사드립니다.

public class MusicPlayerActivity extends AppCompatActivity implements Runnable, 
     SeekBar.OnSeekBarChangeListener { 

ImageView playpause; 
SeekBar seekBar; 
ImageView download; 
MediaPlayer mp = null; 
int len = 0; 
boolean isPlaying = false; 


public MusicPlayerActivity() throws IOException { 
} 


@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_music_player); 
    final String url = getIntent().getExtras().getString("musicurl"); 
    playpause = (ImageView)findViewById(R.id.imageView); 
    seekBar = (SeekBar)findViewById(R.id.seekBar); 
    playpause.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View view) { 

      if(!isPlaying){ 
       playpause.setImageResource(R.drawable.pause); 
       mp.pause(); 
       len = mp.getCurrentPosition(); 
       seekBar.setEnabled(false); 


      }else{ 
       playpause.setImageResource(R.drawable.play); 
       mp.seekTo(len); 
       mp.start(); 
       seekBar.setEnabled(true); 

      } 
      isPlaying = !isPlaying; 



     } 
    }); 

    mp = new MediaPlayer(); 
    try { 
     mp.setDataSource(url); 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } 
    try { 
     mp.prepare(); 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } 
    //if(mp.isPlaying()) mp.stop(); mp.release(); 
    mp.setOnCompletionListener(new MediaPlayer.OnCompletionListener() { 
     @Override 
     public void onCompletion(MediaPlayer mediaPlayer) { 
      mediaPlayer.reset(); 
      seekBar.setProgress(0); 
      playpause.setImageResource(R.drawable.pause); 

     } 
    }); 
    mp.start(); 
    seekBar.setMax(mp.getDuration()); 

    new Thread(this).start(); 
    seekBar.setOnSeekBarChangeListener(this); 


    // Toast.makeText(this, mp.getDuration(), Toast.LENGTH_SHORT).show(); 


} 
@Override 
public void onProgressChanged(SeekBar seekBar, int progress, 
           boolean fromUser) { 
    try { 
     if (mp.isPlaying() || mp != null) { 
      if (fromUser) 
       mp.seekTo(progress); 
     } else if (mp == null) { 
      Toast.makeText(getApplicationContext(), "Media is not running", 
        Toast.LENGTH_SHORT).show(); 
      seekBar.setProgress(0); 
     } 
     //if(progress==seekBar.getMax()) seekBar.setProgress(0); 

    } catch (Exception e) { 
     Log.e("seek bar", "" + e); 
     seekBar.setEnabled(false); 

    } 

} 

@Override 
public void onStartTrackingTouch(SeekBar seekBar) { 

} 

@Override 
public void onStopTrackingTouch(SeekBar seekBar) { 

} 

public void run() { 
    int currentPosition = mp.getCurrentPosition(); 
    int total = mp.getDuration(); 

    while (mp != null && currentPosition < total) { 
     try { 
      Thread.sleep(1000); 
      currentPosition = mp.getCurrentPosition(); 


     } catch (InterruptedException e) { 
      return; 
     } catch (Exception e) { 
      return; 
     } 
     seekBar.setProgress(currentPosition); 


    } 
    seekBar.setProgress(0); 


} 
@Override 
public boolean onOptionsItemSelected(MenuItem item) { 
    switch (item.getItemId()) { 
     case android.R.id.home: 
      mp.stop(); 
      startActivity(new Intent(this,MainActivity.class)); 
      break; 
    } 
    return true; 
} 
@Override 
public void onBackPressed() { 
    Intent mainActivity = new Intent(Intent.ACTION_MAIN); 
    mainActivity.addCategory(Intent.CATEGORY_HOME); 
    mainActivity.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
    startActivity(mainActivity); 
} 
} 
+0

버튼을 다시 클릭하여 노래를 다시 재생 하시겠습니까? 또는 노래를 반복하기 만하면됩니다. – samirk433

+0

노래를 다시 재생하고 싶습니다. –

답변

0

이 미디어 플레이어 완료 청취자입니다 :

mp.setOnCompletionListener(new MediaPlayer.OnCompletionListener() { 
    @Override 
    public void onCompletion(MediaPlayer mediaPlayer) { 
     mediaPlayer.reset(); 
     seekBar.setProgress(0); 
     playpause.setImageResource(R.drawable.pause); 

    } 
}); 


자, 아래, 노래를 다시 시작이 리스너에. 그리고 데이터 소스를 재설정해야한다면 다음과 같이하십시오.

mp.setOnCompletionListener(new MediaPlayer.OnCompletionListener() { 
    @Override 
    public void onCompletion(MediaPlayer mediaPlayer) { 


    // if data-source setting is required: 
    // 
    // try { 
    //  mp.setDataSource(url); 
    //  mp.prepare(); 
    // } catch (IOException e) { 
    //  e.printStackTrace(); 
    // } 
     mediaPlayer.start(); 

    } 
});