简体   繁体   English

如何让RadioButton播放音乐

[英]How to get RadioButton to play music

So I have a class that will play some songs via the MediaPlayer. 因此,我有一堂课,将通过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? 我有以下代码,当选择单选按钮时应该播放一首歌曲,但是这不起作用,有人可以告诉我为什么吗?

I do not get any errors, the music just does not play. 我没有收到任何错误,只是音乐无法播放。

Code from the OnCheckedChange Method: 来自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;

Other code for the songs: 歌曲的其他代码:

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

}

} }

I'd advise against creating so many MediaPlayer s . 我建议不要创建太多MediaPlayer They may very well be the cause of your troubles: When you instance too many of them they don't work well. 它们很可能是您造成麻烦的原因:当您实例化过多实例时,它们将无法正常工作。

And you should take a look at the lifecycle of MediaPlayer as well. 您也应该看看MediaPlayer生命周期

In it you'll find one way to remove your multiple instances of MediaPlayers. 在其中,您将找到一种删除MediaPlayers的多个实例的方法。 You could, for instance change your playing to: 例如,您可以将播放方式更改为:

  Boolean fsong1, fsong2, rsong1, rsong2, psong1, psong2, nwsong1, nwsong2, pisong1, pisong2;
  Boolean isPlaying=false;

//lots of more code //更多代码

  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;

Warning: I'm not giving you the whole change you'd have to apply! 警告:我没有为您提供您必须申请的全部更改!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM