Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Attempt to call getDuration without a valid mediaplayer in media player on android

Tags:

android

I am playing a song from sd card with seek bar. while getting the duration of the song I get Attempt to call getDuration without a valid mediaplayer and illegal state exception. the filename and path is valid one.I tried a long time I could not get a solution. and how to move seek bar while playing song.please help me. my code:

Button play,pause,stop;
SeekBar seek;
MediaPlayer mediaPlayer;
SurfaceView sv;
boolean isPlaying = false;

    play.setOnClickListener(new View.OnClickListener() 
    {   
        public void onClick(View v) 
        {
            playsong(filename); 
        }
    });


private void playsong(String filename2) {
    try{
        Log.e("filename2",filename2);
        mediaPlayer = new  MediaPlayer();
        mediaPlayer.setDataSource(filename2);
        seek.setMax(mediaPlayer.getDuration());
        mediaPlayer.prepare();
        mediaPlayer.start();
        myHandler.post(runn);
        isPlaying = true;
        mediaPlayer.setOnCompletionListener(this);          
    }
    catch(Exception ex){
        Log.e("sdcard-err2:",""+ex);
    }

}

private Handler myHandler = new Handler();
    final Runnable runn = new Runnable()
    {
        public void run() 
        {
            if (mediaPlayer != null) 
            {
                if (isPlaying ) 
                {
                    try 
                    {
                       int currentPosition = mediaPlayer.getCurrentPosition();

                       seek.setProgress(currentPosition);

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

                    sv.postDelayed(runn, 150);
                }
            }
        }

    };  

my log cat errors:

05-17 09:46:43.578: ERROR/filename2(1670): /sdcard/The Dance Theme.mp3
05-17 09:46:43.594: ERROR/MediaPlayer(1670): Attempt to call getDuration without a valid mediaplayer
05-17 09:46:43.594: ERROR/MediaPlayer(1670): error (-38, 0)
05-17 09:46:43.594: ERROR/MediaPlayer(1670): prepareAsync called in state 0
05-17 09:46:43.594: ERROR/sdcard-err2:(1670): java.lang.IllegalStateException
05-17 09:46:43.594: ERROR/MediaPlayer(1670): Error (-38,0)
like image 285
M.A.Murali Avatar asked May 17 '11 04:05

M.A.Murali


People also ask

How do I know if media player is paused on Android?

There is no API to check if MediaPlayer is paused or not. So please use any Boolean variable to check and toggle it when you paused using any button .

How do I close Android media player?

You'll need to either stop playback and kill the app that started them; swipe left on the notification (which doesn't always work); or stop playback, long-press the expanded controls, and select "Close this media session," which is a little awkward — why not just an X in the corner?


1 Answers

You might be calling getDuration before the file is fully loaded. See if the solution to this question works for you.

like image 175
E.Z. Hart Avatar answered Sep 28 '22 05:09

E.Z. Hart