大家好, 我在Android上制作了一个应用程序,在其中我使用了三个单选按钮和两个按钮“播放”和“停止”。 我的问题是,当我播放歌曲时,它正常播放,但当我点击停止按钮然后再点击播放按钮时,歌曲不会播放, 该怎么办?如果有人知道,请帮帮我。 以下是我编写的代码:
package com.mediaplayer;
import java.io.IOException;
import android.app.Activity;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;
public class MediaplayerActivity extends Activity implements OnCheckedChangeListener {
MediaPlayer song1,song2,song3;
int whatsong = 0;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
RadioGroup rgMusic = (RadioGroup) findViewById(R.id.radioGroup1);
song1 = MediaPlayer.create(MediaplayerActivity.this, R.raw.fluet);
song2 = MediaPlayer.create(MediaplayerActivity.this, R.raw.mogra_na_phool);
song3 = MediaPlayer.create(MediaplayerActivity.this, R.raw.airtel);
Button bPlay = (Button) findViewById(R.id.bPlay);
Button bStop = (Button) findViewById(R.id.bStop);
rgMusic.setOnCheckedChangeListener(this);
bPlay.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(song1.isPlaying())
{
song1.pause();
song1.seekTo(0);
}
if(song2.isPlaying())
{
song2.pause();
song2.seekTo(0);
}
if(song3.isPlaying())
{
song3.pause();
song3.seekTo(0);
}
switch (whatsong) {
case 1:
try {
song1.prepare();
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
song1.start();
break;
case 2:
try {
song2.prepare();
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
song2.start();
break;
case 3:
try {
song3.prepare();
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
song3.start();
break;
}
}
});
bStop.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
switch (whatsong) {
case 1:
song1.stop();
break;
case 2:
song2.stop();
break;
case 3:
song3.stop();
break;
}
}
});
}
@Override
public void onCheckedChanged(RadioGroup group, int rbId) {
switch (rbId) {
case R.id.rbMusic1:
whatsong = 1;
break;
case R.id.rbMusic2:
whatsong = 2;
break;
case R.id.rbMusic3:
whatsong = 3;
break;
}
}
}