Search code examples
androidspinnerandroid-mediaplayer

how to stop current music and play new one


I'm trying to create a spinner dialog that contains 3 songs. When the user chooses a song, the current song will stop and the new song will start.

enter image description here

My code:

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

Solution

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

    }