我正试图在歌曲结束时自动将播放器切换到下一首曲目,它会自动跟随铃声必须重现。并且应该在texеview中输出当前时间曲调 setOnCompletionListener - 不起作用,当电流结束时它不会进入下一首曲目
//变量
int[] soundsRawResIds = new int[]{R.raw.belarus, R.raw.russian, R.raw.japan,
R.raw.litva, R.raw.england, R.raw.finlandia, R.raw.france};
private final Handler handler = new Handler();
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.activity_main);
initViews();
currenttime=(TextView)findViewById(R.id.timeSound);
buttonPlayStop = (Button) findViewById(R.id.ButtonPlayStop);
}
private void initViews() {
mediaPlayer = MediaPlayer.create(this, soundsRawResIds[0]);
seekBar = (SeekBar) findViewById(R.id.seekBar);
seekBar.setMax(mediaPlayer.getDuration());
seekBar.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
seekChange(v);
return false;
}
});
}
private void seekChange(View v){
if(mediaPlayer.isPlaying()){
seekBar = (SeekBar)v;
mediaPlayer.seekTo(seekBar.getProgress());
}
}
public void playAndStop(View v){
if (buttonPlayStop.getText() == getString(R.string.play_str)) {
buttonPlayStop.setText(getString(R.string.pause_str));
mediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer mp) {
index++;
mp.reset();
if (index < soundsRawResIds.length) {
try {
AssetFileDescriptor afd = getResources().openRawResourceFd(soundsRawResIds[index]);
if (afd != null) {
mp.setDataSource(afd.getFileDescriptor(), afd.getStartOffset(), afd.getLength());
afd.close();
mp.prepare();
}
} catch (Exception ex) {
// report a crash
}
} else {
// done with media player
mp.release();
mp = null;
}
}
});
mediaPlayer.start();
startPlayProgressUpdater();
}else {
buttonPlayStop.setText(getString(R.string.play_str));
mediaPlayer.pause();
}
}
public void startPlayProgressUpdater() {
seekBar.setProgress(mediaPlayer.getCurrentPosition());
if (mediaPlayer.isPlaying()) {
Runnable notification = new Runnable() {
public void run() {
startPlayProgressUpdater();
if(seekBar.getProgress()==100){
for(int index=0;index<soundsRawResIds.length;index++){
index++;
}
}
}
};
handler.postDelayed(notification,1000);
}else{
mediaPlayer.pause();
buttonPlayStop.setText(getString(R.string.play_str));
seekBar.setProgress(0);
}
}
}
我的XML代码:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<SeekBar
android:layout_width="fill_parent"
android:progress="0"
android:max="100"
android:layout_height="wrap_content"
android:id="@+id/seekBar"/>
<Button
android:text="@string/play_str"
android:textSize="15pt"
android:textStyle="bold"
android:onClick="playAndStop"
android:id="@+id/ButtonPlayStop"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginRight="-3dp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/timeSound"
android:textSize="30sp"
android:text="00:00"
/>
</LinearLayout>
答案 0 :(得分:1)
将onCompletionListener设置为mediaPlayer。
mediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer mp) {
//custom method to play next song
playNextSong();
}
});
从数组简单示例中播放。
private String[] songPaths;
private int playingPostion=0;
public void play(){
if(mediaPlayer==null){
mediaPlayer=new MediaPlayer();
}else{
mediaPlayer.reset();
}
//your array contains resource files.First convert them to a uri before submitting to the mediaPlayer
String uri = "android.resource://" + getPackageName() + "/"+songPaths[playingPosition];
mediaPlayer.setDataSource(Uri.parse(uri));
mediaPlayer.prepare();
mediaPlayer.start();
//prepare your textviews and seekbar
//prepare the player to play next song from array after completing current song
if(playingPosition==0){
if(songPaths.length>1){
playingPosition++;
}
}else if((playingPosition+1)==songPaths.length){
playingPosition=0;
}else{
playingPostion++;
}
mediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer mp) {
play();
//do anything else you desire
}
});
}
以上代码仅作为示例。使用你的线程并正确尝试catch语句。