2

所以我有一门课,可以通过 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);
    }   

}

}

4

1 回答 1

0

我建议不要创建这么多MediaPlayers。它们很可能是你的麻烦的原因:当你实例化太多它们时,它们就不能很好地工作。

你也应该看看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;

警告:我不会给你你必须应用的全部改变!

于 2013-03-11T16:03:17.950 回答