Search code examples
javaandroidandroid-music-player

How To Play MediaPlayer's Next Song without reaching the End of the Current song


i have a music player app and it cuts which part of a track should be played. my question is how do i play the next song even if the current mediaplayer track hasn't reached the end of song. I also tried calling the onCompletion :

mplayer.stop();

        Log.v("PLAYER", "Audio track Stopped");
        checkTrackStatus();
        songHandler.removeCallbacks(uxUpdater);
        if(fis!=null)fis=null;

        if((currentTrack+1) < songs.size())
        {

            Log.v("Move to next track", songs.get(currentTrack+1).get("songTitle"));
            //mplayer.release();
            mplayer=null;
            currentTrack++;
            playSong(currentTrack);

        }else
            Toast.makeText(getApplication(), "All tracks played", Toast.LENGTH_LONG).show();

PLAY SONG METHOD:

public void playSong(final int songIndex)
{
        File toPlay = new File(songs.get(songIndex).get("songPath"));

        lblTrackText.setText("Playing ["+(songIndex+1)+"/"+songs.size()+"]");
        lblTrackTitle.setText(songs.get(songIndex).get("songTitle"));
        //lblTrackTitle.setSelected(true);


        Log.v("PLAY SONG : ", toPlay.getPath());
        if(toPlay.exists())
        {
            //playAudio(toPlay);

            try{

                mplayer = new MediaPlayer(this);
                //mplayer.setOnCompletionListener(this);
                fis=new FileInputStream(new File(songs.get(songIndex).get("songPath"))); //ORIGINAL 'p'
                mplayer.setDataSource(fis.getFD());//need to add permission in manifest to play .m4a files "ACCESS_NETWORK_STATE"

                mplayer.prepare();




                //final String s =p;
                mplayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {

                    //getTotalTrackDuration

                    @Override
                    public void onPrepared(MediaPlayer mp) {
                        mplayer.seekTo(Integer.parseInt(songs.get(songIndex).get("songStart")));
                        mplayer.start();
                        //lblTotal.setText(timeConverter(mplayer.getDuration()));
                        setTrackNo();

                        //Log.v("NOW PLAYING ", s);
                        updateProgress();
                    }
                });

        } catch (Exception e) {
            Log.e("AUDIO PLAYER", "error: " + e.getMessage(), e);
        }

        }
        else
            Toast.makeText(getApplication(), songs.get(songIndex).get("songTitle")+" does not exist or is already deleted", Toast.LENGTH_LONG).show();


}

i always get an error saying

Error(1,-541478725) Fatal Signal 11 (SIGSEGV) at 0x00002ad8 (code=1)

i'm using vitamio media player by the way.

Any help would be great..thanks


Solution

  • The error you get indicates that there is a bug in native code, which must lie in the vitamino media player. The error you are receiving seems to be a memory bug, which makes me think that resources are not freed and the device runs out of memory.

    Try adding a call:

    fis.close()
    

    before your code:

    if(fis!=null)fis=null;
    

    to make sure that you are closing your stream to the previous song properly.

    From this answer your problem could also be that you are loading a lot of images and sounds and causing the app to run out of memory. It also seems that different devices with different versions of android can handle this differently, meaning that the app could work on one phone but not another.