gpt4 book ai didi

java - 如何让RadioButton播放音乐

转载 作者:行者123 更新时间:2023-11-29 09:05:23 24 4
gpt4 key购买 nike

所以我有一个类将通过 MediaPlayer 播放一些歌曲。 I have the following code that When a Radiobutton is selected should play a song, However this does not work, could anyone tell me why?

我没有收到任何错误,只是音乐没有播放。

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);

完整代码:

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);
}

}

最佳答案

I'd advise against creating so many MediaPlayers .它们很可能是您遇到麻烦的原因:当您实例化太多它们时,它们就无法正常工作。

你应该看看 lifecycle of MediaPlayer以及。

您将在其中找到一种删除多个 MediaPlayer 实例的方法。例如,您可以将演奏更改为:

  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;

警告:我不会为您提供您必须应用的全部更改!

关于java - 如何让RadioButton播放音乐,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15342298/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com