0

I'm new to android app development and it's my first app. I'm building a music player, I have following method

public void  playSong(int songIndex){
    // Play song

        mContext = getApplicationContext();
        AudioManager am = (AudioManager) mContext.getSystemService(Context.AUDIO_SERVICE);

        OnAudioFocusChangeListener afChangeListener = new OnAudioFocusChangeListener() {
            public void onAudioFocusChange(int focusChange) {
                if (focusChange == AudioManager.AUDIOFOCUS_LOSS_TRANSIENT){
                    mp.pause();
                } else if (focusChange == AudioManager.AUDIOFOCUS_GAIN) {
                    mp.start();
                } else if (focusChange == AudioManager.AUDIOFOCUS_LOSS) {
                    mp.stop();
                }
            }
        };

        // Request audio focus for playback
        int result = am.requestAudioFocus(afChangeListener,
                                         // Use the music stream.
                                         AudioManager.STREAM_MUSIC,
                                         // Request permanent focus.
                                         AudioManager.AUDIOFOCUS_GAIN);

        if (result == AudioManager.AUDIOFOCUS_REQUEST_GRANTED) {
            // Start playback.
            try {
                mp.reset();
                mp.setDataSource(songsList.get(songIndex).get("songPath"));
                mp.prepare();
                mp.start();
                String songTitle = songsList.get(songIndex).get("songTitle");
                songTitleLabel.setText(songTitle);

                // Changing Button Image to pause image
                btnPlay.setImageResource(R.drawable.btn_pause);

                // set Progress bar values
                songProgressBar.setProgress(0);
                songProgressBar.setMax(100);

                // Updating progress bar
                updateProgressBar();    
            }
            catch (IllegalArgumentException e) {
                e.printStackTrace();
            } catch (IllegalStateException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

}

when the song completes it should start playing the next song but it doesn't. Then I played switched to default media player and switch back to my app and it plays the app. I guess it has to do with the audio focus. How to determine if it already has focus or release the focus on completion. Thanks!!

lightsaber
  • 1,481
  • 18
  • 37

1 Answers1

0

Have you tried this

              @Override
    public void onCompletion(MediaPlayer arg0) {

        // check for repeat is ON or OFF
        if(isRepeat){
            // repeat is on play same song again
            //playSong(currentSongIndex);
             songPlayOnThread(currentSongIndex);
        } else if(isShuffle){
            // shuffle is on - play a random song
            Random rand = new Random();
            currentSongIndex = rand.nextInt((songsList.size() - 1) - 0 + 1) + 0;
            //playSong(currentSongIndex);
             playsong(currentSongIndex);
        } else{
            // no repeat or shuffle ON - play next song
            if(currentSongIndex < (songsList.size() - 1)){
                String result=playSong(currentSongIndex + 1);
                if(result!="success")
                {
                //  mp.stop();
                //  currentSongIndex=0;
                }
                currentSongIndex = currentSongIndex + 1;
            }else{
                // play first song
            //  playSong(0);
                currentSongIndex = 0;
            }
        }
    }
MR. Kumar
  • 661
  • 8
  • 25