Hi,

I want to enable the MediaController for the MediaPlayer.

For this I have written the code like this:

public class AudioPlayerNew extends Activity implements
MediaPlayerControl,MediaPlayer.OnPreparedListener,MediaPlayer.OnBufferingUpdateListener
{
         MediaController mediaController;
         MediaPlayer mMediaPlayer;
         private String path = "/sdcard/Kaarulo Shikarukelle.mp3";
         int percent = 1;
         boolean playerState = true;

        public void onCreate(Bundle icicle)
        {
                super.onCreate(icicle);
                setContentView(R.layout.sdcard_media_view);
                mediaController = (MediaController) findViewById
(R.id.MediaController01);
                playAudio(path);
                mediaController.setAnchorView( findViewById
(R.layout.sdcard_media_view));
                mediaController.setMediaPlayer(this);
                mediaController.bringToFront();
                mediaController.setEnabled(true);
        }

        private void playAudio(String media)
        {
                                try
                               {
                                mMediaPlayer = new MediaPlayer();
                                         mMediaPlayer.setDataSource(path);
                                           mMediaPlayer.prepare();
 
mMediaPlayer.setOnSeekCompleteListener(this);
 
mMediaPlayer.setOnPreparedListener(this);
 
mMediaPlayer.setOnCompletionListener(this);
 
mMediaPlayer.setOnErrorListener(this);
                       }
                       catch (Exception e)
                              {
                            Log.e(TAG, "error: " + e.getMessage(), e);
                              }

              }

@Override
    protected void onDestroy() {
        super.onDestroy();
        // TODO Auto-generated method stub
        dismissDialog();
        if(null != mMediaPlayer)
        {
                mMediaPlayer.release();
                mMediaPlayer = null;
        }


                wakeLock.release();
    }

        @Override
        public int getBufferPercentage() {
                System.out.println("MediaController.getBufferPercentage()
called!!!");
                // TODO Auto-generated method stub
                return percent;
        }

        @Override
        public int getCurrentPosition() {
                System.out.println("MediaController.getCurrentPosition()
called!!!");
                // TODO Auto-generated method stub
                return mMediaPlayer.getCurrentPosition();
        }

        @Override
        public int getDuration() {
                System.out.println("MediaController.getDuration() called!!!");
                // TODO Auto-generated method stub
                return mMediaPlayer.getDuration();
        }

        @Override
        public boolean isPlaying()
        {
                //System.out.println("MediaController.isPlaying() called!!!");
                // TODO Auto-generated method stub
                return playerState;
        }

        @Override
        public void pause()
        {
                //System.out.println("MediaController.pause() called!!!");
                // TODO Auto-generated method stub
                if(null != mMediaPlayer)
                {
                        mMediaPlayer.pause();
                }
                playerState = false;
        }

        @Override
        public void seekTo(int pos)
        {
                // TODO Auto-generated method stub
                //System.out.println("MediaController.seekTo() called!!!");
                if(mMediaPlayer != null)
                {
                        mMediaPlayer.seekTo(pos);
                }
        }

        @Override
        public void start()
        {
                //System.out.println("MediaController.start() called!!!");
                // TODO Auto-generated method stub
                if(null != mMediaPlayer )//&& !mMediaPlayer.isPlaying())
                {
                        mMediaPlayer.start();
                }
                playerState = true;
        }



        @Override
        public void onPrepared(MediaPlayer arg0) {
                // TODO Auto-generated method stub
                dismissDialog();
                mMediaPlayer.start();
        }



        @Override
        public void onCompletion(MediaPlayer mp) {
                // TODO Auto-generated method stub
                Log.i("VideoViewDemo ->"," onCompletion() 
calledooooochhhhhhhh");
                setProgressBarIndeterminateVisibility(false);
                finish();
        }



        @Override
        public void onBufferingUpdate(MediaPlayer mp, int arg1) {

                System.out.println("Buffer percentage :"+arg1);
                // TODO Auto-generated method stub
                percent = arg1;
        }



        @Override
        public boolean onError(MediaPlayer mp, int what, int extra) {
                isVideoFilePlayedProperly = false;

                // TODO Auto-generated method stub
                Log.i("VideoViewDemo ->"," onError() calledooooochhhhhhhh");
                Log.i("onERROR:","arg1:"+what);
                Log.i("onERROR:","arg2:"+extra);
                return true;
        }

}

I added MediPlayerController to the layout and able to view the same.

And I am able to play/pause through these controls.

But the app is not updating the buffer (seek bar movement).

When only I move the trackball, then I am able to see the track bar
increment/decrement according to the trackbar.

Now I have two problems:

1. seek bar is not incrementing according to the mediaplayer.

2. I am not able to exit the app when I press the 'Back' key, only
when the song completes then the app got exited.

I am playing mp3 songs from sdcard.

Can anyone please help me.

Thanks,
Manoj.


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to android-developers@googlegroups.com
To unsubscribe from this group, send email to
android-developers-unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to