所以我有一个课程将通过MediaPlayer播放一些歌曲。我有以下代码,当选择Radiobutton时应播放一首歌,但是这不起作用,有人可以告诉我为什么吗?
我没有任何错误,音乐根本没有播放。
OnCheckedChange方法的代码:
break;
case R.id.rFolk1: //setting up sub radiogroup buttons
if(fsong1.isPlaying() == false)
fsong1.start();
break;
case R.id.rFolk2: //setting up sub radiogroup buttons
if(fsong2.isPlaying() == false)
fsong2.start();
break;
歌曲的其他代码:
fsong1 = MediaPlayer.create(this, R.raw.folk1);
fsong2 = MediaPlayer.create(this, R.raw.folk2);
Fullcode:
public class Music extends Activity implements OnCheckedChangeListener, OnClickListener{
Button playpause;
RadioGroup selectionList, Folk, Rock, Pop, NewWave, Pipe;//define the radiogroup
RadioButton folk1, folk2, rock1, rock2, pop1, pop2, newwave1, newwave2, pipe1, pipe2; //define radiobuttons
MediaPlayer fsong1, fsong2, rsong1, rsong2, psong1, psong2, nwsong1, nwsong2, pisong1, pisong2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//set fullscreen
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN );
setContentView(R.layout.music); //set layout
initialize(); //call this method
}
public void initialize(){
// set up the radiogroups
selectionList = (RadioGroup) findViewById(R.id.rgMusic);
Folk = (RadioGroup) findViewById(R.id.rgFolk);
Rock = (RadioGroup) findViewById(R.id.rgRock);
Pop = (RadioGroup) findViewById(R.id.rgPop);
Pipe = (RadioGroup) findViewById(R.id.rgPipe);
folk1 = (RadioButton) findViewById(R.id.rFolk1);
folk2 = (RadioButton) findViewById(R.id.rFolk2);
rock1 = (RadioButton) findViewById(R.id.rRock1);
rock2 = (RadioButton) findViewById(R.id.rRock2);
pipe1 = (RadioButton) findViewById(R.id.rPipe1);
pipe2 = (RadioButton) findViewById(R.id.rPipe2);
pop1 = (RadioButton) findViewById(R.id.rPop1);
pop2 = (RadioButton) findViewById(R.id.rPop2);
newwave1 = (RadioButton) findViewById(R.id.rNewWave1);
newwave2 = (RadioButton) findViewById(R.id.rNewWave2);
NewWave = (RadioGroup) findViewById(R.id.rgNewWave);
//settting up on check changed
selectionList.setOnCheckedChangeListener(this);
playpause = (Button) findViewById(R.id.bPlayPause);
playpause.setOnClickListener(this);
fsong1 = MediaPlayer.create(this, R.raw.folk1);
fsong2 = MediaPlayer.create(this, R.raw.folk2);
}
@Override
public void onCheckedChanged(RadioGroup arg0, int arg1) {
//case statement for onCheckChange to open a new class/layout
//
//This also hides radiogroups and shows others
switch(arg1){
case R.id.rFolk:
Folk.setVisibility(View.VISIBLE); //shows rg for folk
//hides all the rest of the radiogroups if visible
if(Pipe.getVisibility() == View.VISIBLE){
Pipe.setVisibility(View.GONE);
}
if(Rock.getVisibility() == View.VISIBLE){
Rock.setVisibility(View.GONE);
}
if(Pop.getVisibility() == View.VISIBLE){
Pop.setVisibility(View.GONE);
}
if(NewWave.getVisibility() == View.VISIBLE){
NewWave.setVisibility(View.GONE);
}
break;
case R.id.rPipe:
Pipe.setVisibility(View.VISIBLE);//shows rg for pipe
//hides all the rest of the radiogroups if visible
if(Folk.getVisibility() == View.VISIBLE){
Folk.setVisibility(View.GONE);
}
if(Rock.getVisibility() == View.VISIBLE){
Rock.setVisibility(View.GONE);
}
if(Pop.getVisibility() == View.VISIBLE){
Pop.setVisibility(View.GONE);
}
if(NewWave.getVisibility() == View.VISIBLE){
NewWave.setVisibility(View.GONE);
}
break;
case R.id.rRock:
Rock.setVisibility(View.VISIBLE);//shows rg for rock
//hides all the rest of the radiogroups if visible
if(Folk.getVisibility() == View.VISIBLE){
Folk.setVisibility(View.GONE);
}
if(Pipe.getVisibility() == View.VISIBLE){
Pipe.setVisibility(View.GONE);
}
if(Pop.getVisibility() == View.VISIBLE){
Pop.setVisibility(View.GONE);
}
if(NewWave.getVisibility() == View.VISIBLE){
NewWave.setVisibility(View.GONE);
}
break;
case R.id.rPop:
Pop.setVisibility(View.VISIBLE);//shows rg for pop
//hides all the rest of the radiogroups if visible
if(Folk.getVisibility() == View.VISIBLE){
Folk.setVisibility(View.GONE);
}
if(Pipe.getVisibility() == View.VISIBLE){
Pipe.setVisibility(View.GONE);
}
if(Rock.getVisibility() == View.VISIBLE){
Rock.setVisibility(View.GONE);
}
if(NewWave.getVisibility() == View.VISIBLE){
NewWave.setVisibility(View.GONE);
}
break;
case R.id.rNewWave:
NewWave.setVisibility(View.VISIBLE);//shows rg for newwave
//hides all the rest of the radiogroups if visible
if(Folk.getVisibility() == View.VISIBLE){
Folk.setVisibility(View.GONE);
}
if(Pipe.getVisibility() == View.VISIBLE){
Pipe.setVisibility(View.GONE);
}
if(Rock.getVisibility() == View.VISIBLE){
Rock.setVisibility(View.GONE);
}
if(Pop.getVisibility() == View.VISIBLE){
Pop.setVisibility(View.GONE);
}
break;
case R.id.rFolk1: //setting up sub radiogroup buttons
if(fsong1.isPlaying() == false){
fsong1.start();
}
break;
case R.id.rFolk2: //setting up sub radiogroup buttons
if(fsong2.isPlaying() == false){
fsong2.start();
}
}
}
@Override
public void onClick(View view) {
// setting the onclick listener for the buttons for play/pause stop
// check for already playing
if(fsong1.isPlaying()){
fsong1.pause();
// Changing button image to play button
playpause.setBackgroundResource(R.drawable.play_button);
}else{
// Resume song
fsong1.start();
// Changing button image to pause button
playpause.setBackgroundResource(R.drawable.pause_button);
}
}
}
答案 0 :(得分:0)
I'd advise against creating so many MediaPlayer
s 。它们很可能是你麻烦的原因:当你实例太多时,它们就不能正常工作。
你也应该看看lifecycle of MediaPlayer。
在其中,您将找到一种方法来删除多个MediaPlayers实例。例如,您可以将演奏更改为:
Boolean fsong1, fsong2, rsong1, rsong2, psong1, psong2, nwsong1, nwsong2, pisong1, pisong2;
Boolean isPlaying=false;
//更多代码
case R.id.rFolk1: //setting up sub radiogroup buttons
if ((isPlaying) && !fsong1){
mediaPlayer.stop();
mediaPlayer.release();
mediaPlayer = null;
}
if (!fsong1){
//reset all other types of songs!
fsong1=true;
isPlaying = true;
mediaPlayer=MediaPlayer.create(this, R.raw.folk1);
mediaPlayer.start();
}
break;
警告:我不会给你你必须申请的全部改变!