当用户进入此活动时,会播放一首歌曲。当他离开这个活动时,一切正常,但是当用户再次进入这个活动时,会播放一首新歌而不会停止旧歌。当用户再次访问此活动时,我如何停止第一首歌曲?
公共类 MediaPlay1 扩展 Activity {
private SeekBar seekvolume, seekcontrol;
private TextView txtcrnt, txtremain, txtcrntbig,txttitle;
private ImageButton btnpre, btnnext, btnplay, btnrepeat;
private AudioManager audioManager;
private MediaPlayer mediaPlayer;
private String kg1;
private String MixName1;
private Button btnback, btnplayer;
String MixReplace;
private boolean isDownloading = true;
Handler handler = new Handler();
private boolean isRepeat=false;
private int i = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.media3);
mediaPlayer = new MediaPlayer();
initViews();
if(kg1 != null && MixName1 != null)
{
Intent i1 = getIntent();
kg1 = i1.getStringExtra("Kggg1");
MixName1 = i1.getStringExtra("mixname1");
MixReplace=MixName1.replace(".mp3", " ");
}else{
}
}
private void initViews() {
audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
txtcrnt = (TextView) findViewById(R.id.txtcurnt);
txtremain = (TextView) findViewById(R.id.txtremain);
txtcrntbig = (TextView) findViewById(R.id.txtcrntbig);
txttitle=(TextView) findViewById(R.id.title);
btnpre = (ImageButton) findViewById(R.id.btnpre);
btnnext = (ImageButton) findViewById(R.id.btnnext);
btnplay = (ImageButton) findViewById(R.id.btnplay);
btnrepeat=(ImageButton) findViewById(R.id.btnrepeat);
btnback = (Button) findViewById(R.id.btnlout);
btnplayer = (Button) findViewById(R.id.btnedit);
btnback.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
((MyMixes)getParent()).goBack();
}
});
btnplayer.setClickable(true);
btnplayer.setEnabled(false);
btnplayer.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
}
});
if(btnrepeat.isPressed())
{
mediaPlayer.setLooping(true);
}
txttitle.setText(MixReplace);
seekcontrol=(SeekBar) findViewById(R.id.seekbarcontrol);
seekcontrol.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
public void onStopTrackingTouch(SeekBar seekBar) {
}
public void onStartTrackingTouch(SeekBar seekBar) {
}
public void onProgressChanged(SeekBar seekBar, int progress,
boolean fromUser) {
if(fromUser){
mediaPlayer.seekTo(progress);
seekcontrol.setProgress(progress);
}
}
});
btnplay.setOnClickListener(new OnClickListener() {
public void onClick(View paramView) {
btnClick();
}
});
new Thread(new Runnable() {
@Override
public void run() {
try {
Thread.sleep(3000);
btnplay.setClickable(false);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
});
btnplay.setClickable(true);
btnnext.setOnClickListener(new OnClickListener() {
public void onClick(View paramView) {
}
});
btnpre.setOnClickListener(new OnClickListener() {
public void onClick(View paramView) {
}
});
seekvolume = (SeekBar) findViewById(R.id.seekbarvolume);
new Thread()
{
public void run()
{
while (isDownloading)
{
if (Data.filenames != null&& Data.filenames.size() >= (getIntent().getExtras().getInt("index") + 1) && Data.filenames.get(getIntent().getExtras().getInt("index")) != null)
{
try
{
String path = Data.filenames.get(getIntent().getExtras().getInt("index"));
mediaPlayer.setDataSource(path);
mediaPlayer.prepare();
mediaPlayer.start();
startPlayProgressUpdater();
mediaPlayer.setOnCompletionListener(new OnCompletionListener()
{
@Override
public void onCompletion(MediaPlayer mp)
{
mediaPlayer.seekTo(0);
}
});
mediaPlayer.setOnErrorListener(new OnErrorListener() {
@Override
public boolean onError(MediaPlayer mp, int what, int extra) {
return false;
}
});
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
} finally {
isDownloading = false;
}
if (mediaPlayer != null)
seekcontrol.setMax(mediaPlayer.getDuration());
int maxvolume = audioManager
.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
int curntvolume = audioManager
.getStreamVolume(AudioManager.STREAM_MUSIC);
seekvolume.setMax(maxvolume);
seekvolume.setProgress(curntvolume);
seekvolume
.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
public void onStopTrackingTouch(
SeekBar paramSeekBar) {
}
public void onStartTrackingTouch(
SeekBar paramSeekBar) {
}
public void onProgressChanged(
SeekBar paramSeekBar, int progress,
boolean paramBoolean) {
audioManager.setStreamVolume(
AudioManager.STREAM_MUSIC,
progress, 0);
}
});
seekcontrol.setMax(mediaPlayer.getDuration());
seekcontrol.setOnTouchListener(new OnTouchListener() {
public boolean onTouch(View v,
MotionEvent paramMotionEvent) {
seekChange(v);
return false;
}
});
}
}
};
}.start();
}
protected void btnClick() {
i++;
i = i % 2;
startSong(i);
}
private void startSong(int i) {
if (i == 0) {
System.out.println("11111" + i);
btnplay.setImageResource(R.drawable.pause);
try {
System.out.println("start try chech------");
mediaPlayer.start();
startPlayProgressUpdater();
} catch (Exception e) {
mediaPlayer.pause();
}
}
if (i == 1) {
btnplay.setImageResource(R.drawable.play);
mediaPlayer.pause();
System.out.println("00000" + i);
}
}
private Handler my_handler = new Handler() {
public void handleMessage(Message msg) {
seekcontrol.setProgress(msg.arg1);
super.handleMessage(msg);
}
};
public void startPlayProgressUpdater()
{
Message objmessage = my_handler.obtainMessage();
boolean isPlaying = mediaPlayer.isPlaying();
if (isPlaying) {
objmessage.obj = mediaPlayer.getCurrentPosition();
Runnable notification = new Runnable() {
public void run() {
startPlayProgressUpdater();
int curtimeplay = mediaPlayer.getCurrentPosition();
int curntseconds = (int) (curtimeplay / 1000) % 60;
int curntminutes = (int) ((curtimeplay / (1000 * 60)) % 60);
txtcrnt.setText(curntminutes + ":" + curntseconds);
txtcrntbig.setText(curntminutes + ":" + curntseconds);
int duratplay = mediaPlayer.getDuration();
int totaldurationseconds = (int) (duratplay / 1000) % 60;
int totaldurationminutes = (int) ((duratplay / (1000 * 60)) % 60);
int remianingtime = duratplay - curtimeplay;
int remianingtimeseconds = (int) (remianingtime / 1000) % 60;
int remianingtimeminutes = (int) ((remianingtime / (1000 * 60)) % 60);
txtremain.setText(remianingtimeminutes + ":" + remianingtimeseconds);
}
};
handler.postDelayed(notification, 1000);
}
else
{
mediaPlayer.pause();
btnplay.setImageResource(R.drawable.play);
}
objmessage.arg1 = mediaPlayer.getCurrentPosition();
my_handler.sendMessage(objmessage);
}
protected void seekChange(View v) {
if (mediaPlayer.isPlaying()) {
SeekBar sb = (SeekBar) v;
mediaPlayer.seekTo(sb.getProgress());
}
}
@Override
public void onBackPressed() {
((MyMixes)getParent()).goBack();
}
}