我正在尝试创建一个音乐播放器安卓应用程序。我几乎完成了工作,现在我遇到了一个问题。即,下一个按钮和上一个按钮仅在第一次使用它们的功能,然后它重复该歌曲。此外,在完成当前歌曲后,下一首歌不会自动唱歌。任何人都可以告诉我我做错了什么?这是我一直在尝试的代码。在此先感谢。
CODE:
package com.example.jamshi.application3;
import android.content.Intent;
import android.media.MediaPlayer;
import android.net.Uri;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.SeekBar;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
public class Player extends ActionBarActivity implements View.OnClickListener {
static MediaPlayer mp;
ArrayList<File> mySongs;
Uri u;
int position;
Thread updateSeekBar;
SeekBar sb;
Button btnPlay,btnFF,btnFB,btnNext,btnPrev;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_player);
btnPlay = (Button)findViewById(R.id.btnPlay);
btnFF = (Button)findViewById(R.id.btnFF);
btnFB = (Button)findViewById(R.id.btnFB);
btnNext = (Button)findViewById(R.id.btnNext);
btnPrev = (Button)findViewById(R.id.btnPrev);
btnPlay.setOnClickListener(this);
btnFF.setOnClickListener(this);
btnFB.setOnClickListener(this);
btnNext.setOnClickListener(this);
btnPrev.setOnClickListener(this);
sb = (SeekBar) findViewById(R.id.seekBar);
updateSeekBar = new Thread(){
@Override
public void run() {
int totalDuration = mp.getDuration();
int currentPosition = 0;
while(currentPosition < totalDuration){
try {
sleep(500);
currentPosition = mp.getCurrentPosition();
sb.setProgress(currentPosition);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
//super.run();
}
};
if (mp!=null){
mp.stop();
mp.release();
}
Intent i = getIntent();
Bundle b = i.getExtras();
ArrayList<File> mySongs =(ArrayList) b.getParcelableArrayList("songList");
position = b.getInt("pos",0);
u = Uri.parse(mySongs.get(position).toString());
mp = MediaPlayer.create(getApplicationContext(), u);
mp.start();
sb.setMax(mp.getDuration());
updateSeekBar.start();
sb.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
mp.seekTo(seekBar.getProgress());
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_player, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
@Override
public void onClick(View v) {
int id = v.getId();
switch (id){
case R.id.btnPlay:
if (mp!=null && mp.isPlaying()){
btnPlay.setText(">");
super.onPause();
mp.pause();
}else {
btnPlay.setText("||");
mp.start();
}
break;
case R.id.btnFF:
mp.seekTo(mp.getCurrentPosition()+5000);
break;
case R.id.btnFB:
mp.seekTo(mp.getCurrentPosition()-5000);
break;
case R.id.btnNext:
mp.stop();
mp.release();
position = (position+1)%mySongs.size();
u = Uri.parse(mySongs.get(position).toString());
mp = MediaPlayer.create(getApplicationContext(),u);
mp.start();
sb.setMax(mp.getDuration());
break;
case R.id.btnPrev:
mp.stop();
mp.release();
// position = (position-1<0)? mySongs.size()-1: position-1;
if(position-1 < 0){
position = mySongs.size()-1;
}else{
position = position-1;
}
u = Uri.parse(mySongs.get(position).toString());
mp = MediaPlayer.create(getApplicationContext(),u);
mp.start();
sb.setMax(mp.getDuration());
break;
}
}
}