เล่นเพลงด้วย MediaPlayer และควบคุมด้วย MediaController

ใช้ MediaController ควบคุมการเล่นเพลง

กำหนด id เป็น main_layout

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/main_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:padding="16dp"
    tools:context=".MainActivity">

</LinearLayout>

MainActivity.java

package com.phaisarn.mediacontrolapp;

import android.media.MediaPlayer;
import android.os.Handler;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.MotionEvent;
import android.widget.MediaController;

public class MainActivity extends AppCompatActivity
        implements MediaController.MediaPlayerControl {

    private MediaPlayer mPlayer;
    private MediaController mController;
    private Handler mHandler = new Handler();

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

        mController = new MediaController(this);
        mController.setMediaPlayer(this);
        mController.setAnchorView(findViewById(R.id.main_layout));
        mController.setEnabled(true);

        mPlayer = MediaPlayer.create(MainActivity.this, R.raw.s1);
        mPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
            @Override
            public void onPrepared(MediaPlayer mp) {
                mHandler.post(new Runnable() {
                    @Override
                    public void run() {
                        mController.show(3000);
                        mPlayer.start();
                    }
                });
            }
        });
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        //return super.onTouchEvent(event);
        mController.show(3000);
        return false;
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        mController.hide();
        mPlayer.stop();
        mPlayer.release();
        mPlayer = null;
    }

    public void start() {
        mPlayer.start();
    }

    @Override
    public void pause() {
        mPlayer.pause();
    }

    @Override
    public int getDuration() {
        //return 0;
        return mPlayer.getDuration();
    }

    @Override
    public int getCurrentPosition() {
        //return 0;
        return mPlayer.getCurrentPosition();
    }

    @Override
    public void seekTo(int pos) {
        mPlayer.seekTo(pos);
    }

    @Override
    public boolean isPlaying() {
        //return false;
        return mPlayer.isPlaying();
    }

    @Override
    public int getBufferPercentage() {
        return 0;
    }

    @Override
    public boolean canPause() {
        return true;
    }

    @Override
    public boolean canSeekBackward() {
        return true;
    }

    @Override
    public boolean canSeekForward() {
        return true;
    }

    @Override
    public int getAudioSessionId() {
        return 0;
    }
}