Search code examples
javaandroidandroid-mediaplayer

how to stop old song when new song is playing with Service


I am try to create a music app. I am using this code to stop the song when another song will play but this code does not work . Both songs play together i want to play one by one or if any other song will clicked the stop the previous song and play that song. Any one can help me.

MainActivity:

private void clickSong() {
        mListBaiHat.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {    
                String local = mPath.get(i);
                mserviceMusic.setLocalSongRunging(i);   

                try {
                    mserviceMusic.setLocalSong(local);
                    mClickStart.setBackgroundResource(R.drawable.ic_media_pause_light);    

                } catch (IOException e) {
                    e.printStackTrace();
                }    

                mHinhAlbum.setImageResource(arrayList.get(i).getHinhAlbum());
                mCLickTenBaiHat.setText(arrayList.get(i).getTenBaiHat());
                mCLickCasy.setText(arrayList.get(i).getTheloai());    

                    MediaMetadataRetriever retriever = null;
                if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.GINGERBREAD_MR1) {
                    retriever = new MediaMetadataRetriever();
                    retriever.setDataSource(mPath.get(i));

                    String tenAlbum = retriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_ARTIST);
                    String ten = retriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_TITLE);

                    mCLickTenBaiHat.setText(ten);
                    mCLickCasy.setText(tenAlbum);

                }

                bundle = new Bundle();
                bundle.putInt("vitri", i);
                bundle.putStringArrayList("tenbai", mPath);


            }
        });
    }

Service:

public void setLocalSongRunging(int vitri){
    mLocaltionSong = vitri;
}

public void setLocalSong(String localsong) throws IOException {
    mediaPlayer = new MediaPlayer();
    mediaPlayer.setDataSource(localsong);
    mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);

    if (mediaPlayer.isPlaying()) {
        mediaPlayer.stop();            
        mediaPlayer.release();
        mediaPlayer=null;

    }else {
        mediaPlayer.prepare();
        mediaPlayer.start();
    }

}

Solution

  • You are creating a new MediaPlayer each time. So your code to stop and release wont work (since different instance of mediaplayer are being used) change your code like this in setLocalSong(String localsong).

       public void setLocalSong(String localsong) throws IOException {
        if(mediaPlayer != null){
            mediaPlayer.stop();            
            mediaPlayer.release();
            mediaPlayer=null;
        }
        mediaPlayer = new MediaPlayer();
        mediaPlayer.setDataSource(localsong);
        mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
        mediaPlayer.prepare();
        mediaPlayer.start();
    
    }