Add Mediacontroller To Mediaplayer

Posted on

This instance exhibit how to add MediaController to simple MediaPlayer inwards previous example.

package com.blogspot.android_er.androidmediaplayer;  import; import; import android.os.Bundle; import android.os.Handler; import; import android.view.MotionEvent; import android.view.SurfaceHolder; import android.view.SurfaceView; import android.view.View; import android.widget.MediaController; import android.widget.Toast;  import;  populace degree MainActivity extends AppCompatActivity         implements SurfaceHolder.Callback, MediaPlayer.OnPreparedListener,         MediaController.MediaPlayerControl {      soul SurfaceView surfaceView;     soul SurfaceHolder surfaceHolder;     soul MediaPlayer mediaPlayer;      private MediaController mediaController;     soul Handler handler = novel Handler();      String videoSource =             "";      @Override     protected void onCreate(Bundle savedInstanceState) {         super.onCreate(savedInstanceState);         setContentView(R.layout.activity_main);          surfaceView = (SurfaceView)findViewById(;         surfaceHolder = surfaceView.getHolder();         surfaceHolder.addCallback(this);          surfaceView.setOnTouchListener(new View.OnTouchListener() {             @Override             populace boolean onTouch(View v, MotionEvent event) {                 if(mediaController != null){           ;                 }                 supply false;             }         });      }      @Override     populace void surfaceCreated(SurfaceHolder holder) {          Toast.makeText(MainActivity.this,                 "surfaceCreated()", Toast.LENGTH_LONG).show();         mediaPlayer = novel MediaPlayer();         mediaPlayer.setDisplay(surfaceHolder);         mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);         mediaPlayer.setOnPreparedListener(this);         endeavor {             mediaPlayer.setDataSource(videoSource);             mediaPlayer.prepare();              mediaController = novel MediaController(this);          } grab (IOException e) {             e.printStackTrace();             Toast.makeText(MainActivity.this,                     "something wrong!\n" + e.toString(),                     Toast.LENGTH_LONG).show();         }     }      @Override     populace void surfaceChanged(SurfaceHolder holder,                                int format, int width, int height) {      }      @Override     populace void surfaceDestroyed(SurfaceHolder holder) {      }      @Override     populace void onPrepared(MediaPlayer mp) {         mediaPlayer.start();         Toast.makeText(MainActivity.this,                 "onPrepared()", Toast.LENGTH_LONG).show();          mediaController.setMediaPlayer(this);         mediaController.setAnchorView(surfaceView); Runnable() {              populace void run() {                 mediaController.setEnabled(true);       ;             }         });     }      @Override     populace void start() {         mediaPlayer.start();     }      @Override     populace void pause() {         mediaPlayer.pause();     }      @Override     populace int getDuration() {         supply mediaPlayer.getDuration();     }      @Override     populace int getCurrentPosition() {         supply mediaPlayer.getCurrentPosition();     }      @Override     populace void seekTo(int pos) {         mediaPlayer.seekTo(pos);     }      @Override     populace boolean isPlaying() {         supply mediaPlayer.isPlaying();     }      @Override     populace int getBufferPercentage() {         supply 0;     }      @Override     populace boolean canPause() {         supply true;     }      @Override     populace boolean canSeekBackward() {         supply true;     }      @Override     populace boolean canSeekForward() {         supply true;     }      @Override     populace int getAudioSessionId() {         supply mediaPlayer.getAudioSessionId();     } }  

For layout in addition to permission, scream to the previous post service “MediaPlayer instance to play video from Internet“.