I am trying to use MediaController.MediaPlayerControl in order to display a MediaController at the bottom of my Custom View but I can't get it to work. It's crashing at the ctrl.show(); with the following Exception whenever I try to load this view:
E/AndroidRuntime( 3007): Caused by: android.view.WindowManager$BadTokenException: Unable to add window -- token null is not valid; is your activity running?
here is my code.
public class MediaPlayerView extends ImageView implements MediaPlayerControl {
private MediaPlayer mp;
private MediaController ctrl;
private Uri data;
private Context mContext;
public MediaPlayerView(Context context, Uri data) {
super(context);
this.mContext = context;
this.data = data;
init();
}
public void init() {
mp = new MediaPlayer();
try {
mp.setDataSource(mContext, data);
mp.prepare();
} catch(IOException e) {
e.printStackTrace();
}
ctrl = new MediaController(mContext);
ctrl.setMediaPlayer(this);
ctrl.setAnchorView(this);
ctrl.setEnabled(true);
ctrl.show();
}
public boolean canPause() {
return true;
}
public boolean canSeekBackward() {
return false;
}
public boolean canSeekForward() {
return false;
}
public int getBufferPercentage() {
return (mp.getCurrentPosition()*100)/mp.getDuration();
}
public int getCurrentPosition() {
return mp.getCurrentPosition();
}
public int getDuration() {
return mp.getDuration();
}
public boolean isPlaying() {
return mp.isPlaying();
}
public void pause() {
mp.pause();
}
public void seekTo(int pos) {
mp.seekTo(pos);
}
public void start() {
mp.start();
}
}
Any help greatly appreciated!