java - 如何让RadioButton播放音乐

标签 java android

所以我有一个类将通过 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/

相关文章:

java - 默认覆盖处理程序

java - 如何刷新 Log4J2 中的异步记录器(使用中断器)

java - ScheduledExecutorService 可抛出丢失

android - 根据编译器、Android 的说法,FragmentTransaction 对象的实例化是不可能的

android - 如何解决 "make: *** No rule to make target ` out/target/product/generic/root/file_contexts', `snod' 需要。停止。”

java - 如何确保映射的总字节数(所有键和值长度的总和)保持在限制范围内?

java - 列表中方法的组合

android - android编程中如何将EditText中的特殊字改成特殊颜色?

Android - 如何使用线性布局放置按钮和微调器?

android - 在android中创建一个进度条,在进度条和反向进度条中改变它的颜色