如何阻止当前音乐并播放新音乐

时间:2017-09-02 16:37:35

标签: android spinner android-mediaplayer

我正在尝试创建一个包含3首歌曲的微调器对话框。当用户选择一首歌曲时,当前歌曲将停止,新歌曲将开始播放。

enter image description here

我的代码:

         MPLUS = MediaPlayer.create(getApplicationContext(), R.raw.vaporv2);
        SAINTONE = MediaPlayer.create(getApplicationContext(), R.raw.saint_pepsi);
        SAINTTWO = MediaPlayer.create(getApplicationContext(), R.raw.saint_pepsi2);
     final AlertDialog.Builder mBuilder = new AlertDialog.Builder(MainActivity.this, R.style.MyDialogTheme);
        View mView = getLayoutInflater().inflate(R.layout.dialog_spinner,null);
        mBuilder.setTitle("Music.mp3");
        mBuilder.setIcon(R.drawable.play);
        final Spinner mSpinner = (Spinner)mView.findViewById(R.id.spinner);
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(MainActivity.this,android.R.layout.simple_spinner_item,getResources().getStringArray(R.array.Music));
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        mSpinner.setAdapter(adapter);
        mBuilder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                if(!mSpinner.getSelectedItem().toString().equals("Choose Your Music . . .")){
                    if(mSpinner.getSelectedItem().toString().equals("MACINTOSH PLUS - リサフランク420")){
                        Toast.makeText(MainActivity.this,""+mSpinner.getSelectedItem().toString(),Toast.LENGTH_LONG).show();
                    }else  if(mSpinner.getSelectedItem().toString().equals("SAINT PEPSI - ENJOY YOURSELF")){
                        Toast.makeText(MainActivity.this,""+mSpinner.getSelectedItem().toString(),Toast.LENGTH_LONG).show();

                    }else if (mSpinner.getSelectedItem().toString().equals("SAINT PEPSI - PRIVATE CALLER")){
                    Toast.makeText(MainActivity.this,""+mSpinner.getSelectedItem().toString(),Toast.LENGTH_LONG).show();
                    }
                }
            }
        });
        mBuilder.setNegativeButton("CLOSE",null);
        mBuilder.setCancelable(false);
        mBuilder.setView(mView);
        AlertDialog  dialog  = mBuilder.create();
        dialog.show();

1 个答案:

答案 0 :(得分:1)

public class MainActivity extends AppCompatActivity {
   MediaPlayer mPlayer;
   List<Integer> musicList;
   -----
   @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    ------
    list = new ArrayList<>();
    list.add(R.raw.vaporv2);
    list.add(R.raw.saint_pepsi);
    list.add(R.raw.saint_pepsi2);
    ------
     mBuilder.setPositiveButton("OK", new DialogInterface.OnClickListener() 
     {
        @Override
        public void onClick(DialogInterface dialog, int which) {
           if(which>0){ 
                MainActivity.this.play(which);
           }     
         }    
    });

  }
    public void Play(int which){
       if (mPlayer != null){
           mPlayer.stop();
           mPlayer.release();
           mPlayer = null;
      }
  mPlayer =   MediaPlayer.create(MainActivity.this, list.get(which));
  mediaPlayer.start();
} 

}