Search code examples
androidandroid-mediaplayerseekbarrunnable

Media player seekbar issue


I'm trying to create a media player that will update a seekbar telling the current position at the song being played. I've tried this code but it seems that something wrong. Can someone help me solve this error?

This is my code

public class AlbumDetails extends Activity implements OnClickListener {

    private ImageView play, forward, backward;
    private MediaPlayer mediaPlayer;
    private boolean playing = false;
    private ProgressDialog dialog;
    private String mp3link;
    private SeekBar seekbar;
    private Handler handler;
    private int mediaPos;
    private int mediaMax;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.playeer);
        initWidgets();

    }

    private void initWidgets() {
        play = (ImageView) findViewById(R.id.playeer_play);
        play.setOnClickListener(this);
        seekbar = (SeekBar) findViewById(R.id.playeer_seek);
        forward = (ImageView) findViewById(R.id.playeer_forward);
        backward = (ImageView) findViewById(R.id.playeer_back);
        mediaPlayer = new MediaPlayer();
        mp3link = getIntent().getStringExtra("mp3link");
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
        case R.id.playeer_play:
            playFunction();
            break;
        }
    }

    private void playFunction() {
        if (!playing) {
            try {
                dialog = ProgressDialog
                        .show(AlbumDetails.this,
                                "",
                                getString(com.darkovski.quran.R.string.buffering),
                                true);
                dialog.setCancelable(true);
                dialog.show();
                mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
                mediaPlayer.setDataSource(mp3link);
                mediaPlayer.setOnPreparedListener(new OnPreparedListener() {

                    @Override
                    public void onPrepared(MediaPlayer mp) {
                        play.setBackgroundResource(R.drawable.pause);
                        playing = true;
                        //this is new
                        mediaPos = mp.getCurrentPosition();
                        mediaMax = mp.getDuration();

                        seekbar.setMax(mediaMax);
                        seekbar.setProgress(mediaPos);
                        //this line is the error
                        handler.removeCallbacks(moveSeekBarThread);
                        handler.postDelayed(moveSeekBarThread, 100);

                        mp.start();
                        dialog.dismiss();
                    }
                });
                mediaPlayer.prepareAsync();

            } catch (Exception e) {
                e.printStackTrace();
                dialog.dismiss();
            }
        } else {
            play.setBackgroundResource(R.drawable.play_play);
            mediaPlayer.stop();
            mediaPlayer.release();
            playing = false;
        }
    }

    @Override
    public void onBackPressed() {
        super.onBackPressed();
        if (mediaPlayer.isPlaying())
            mediaPlayer.stop();
        mediaPlayer.release();
    }

    private Runnable moveSeekBarThread = new Runnable() {

        public void run() {
            if (mediaPlayer.isPlaying()) {
                int mediaPos_new = mediaPlayer.getCurrentPosition();
                int mediaMax_new = mediaPlayer.getDuration();
                seekbar.setMax(mediaMax_new);
                seekbar.setProgress(mediaPos_new);

                handler.postDelayed(this, 100); // Looping the thread after 0.1
                                                // second
                                                // seconds
            }

        }
    };
}

And im getting this from logcat:

12-07 15:38:27.148: E/AndroidRuntime(2835): FATAL EXCEPTION: main
12-07 15:38:27.148: E/AndroidRuntime(2835): java.lang.NullPointerException
12-07 15:38:27.148: E/AndroidRuntime(2835):     at com.darkovski.quran.AlbumDetails$2.onPrepared(AlbumDetails.java:78)
12-07 15:38:27.148: E/AndroidRuntime(2835):     at android.media.MediaPlayer$EventHandler.handleMessage(MediaPlayer.java:1523)
12-07 15:38:27.148: E/AndroidRuntime(2835):     at android.os.Handler.dispatchMessage(Handler.java:99)
12-07 15:38:27.148: E/AndroidRuntime(2835):     at android.os.Looper.loop(Looper.java:137)
12-07 15:38:27.148: E/AndroidRuntime(2835):     at android.app.ActivityThread.main(ActivityThread.java:4441)
12-07 15:38:27.148: E/AndroidRuntime(2835):     at java.lang.reflect.Method.invokeNative(Native Method)
12-07 15:38:27.148: E/AndroidRuntime(2835):     at java.lang.reflect.Method.invoke(Method.java:511)
12-07 15:38:27.148: E/AndroidRuntime(2835):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
12-07 15:38:27.148: E/AndroidRuntime(2835):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
12-07 15:38:27.148: E/AndroidRuntime(2835):     at dalvik.system.NativeStart.main(Native Method)

Can some one tell me where is the problem and how can I solve this?


Solution

  • Your handler is null, as you have not initialized that data member to anything.