I guess this is more related to Java fundamentals and OOP, but I have seen some patterns followed by usual Android classes. Google is often deprecating old functions in favour of better functions, and I am thinking of doing something similar with my custom View.
I want to build a video player in Android that should be easy to create and should be returning a callback on several events. For that I want the user to use my functions instead of Built in video view's function. So I am applying several approaches to prevent user from using those built-in functions:
@Deprecated
notation to show the function name with a strike , eg getVolume()
@Override @deprecated
public void getWidth(){
throw new UnsupportedOperationException("useMyVideoPlayer#getVideoWidth()");
}
init()
or their respective alternatives. Here is the code( check the comment on the code line : super.setOnErrorListener(errorListener);
)
public class MyVideoPlayer extends VideoView {
public static final String TAG = "MyVP>>";
private MyPlayerCurrentPlaybackState currentPlaybackState;
private MediaController mediaController;
@Nullable
private MyVideoPlayerListener playerListener;
public MyVideoPlayer(Context context) {
super(context);
init();
}
public MyVideoPlayer(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
public MyVideoPlayer(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init();
}
public MyVideoPlayer(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);
init();
}
public void init() {
currentPlaybackState = MyPlayerCurrentPlaybackState.STOPPED;
mediaController = new MediaController(this.getContext());
mediaController.setAnchorView(this);
MediaPlayer.OnCompletionListener onCompletionListener = new MediaPlayer.OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer mp) {
Log.e(TAG, "internalListener:onCompletion: called");
mp.reset();
mp.setDisplay(MyVideoPlayer.this.getHolder());
currentPlaybackState = MyPlayerCurrentPlaybackState.STOPPED;
if (playerListener != null) {
playerListener.onCompleted(mp);
} else {
Log.e(TAG, "onCompletionListener:onCompletion: playerListener is null");
}
}
};
super.setOnCompletionListener(onCompletionListener);
MediaPlayer.OnErrorListener errorListener = new MediaPlayer.OnErrorListener() {
@Override
public boolean onError(MediaPlayer mp, int what, int extra) {
currentPlaybackState = MyPlayerCurrentPlaybackState.STOPPED;
if (playerListener != null) {
playerListener.onError(what, extra);
} else {
Log.e(TAG, "errorListener:onError: playerListener is null");
}
return true;// indicates we handled error
}
};
super.setOnErrorListener(errorListener);// <---setting error listener *inside* the view only and making setOnErrorListener(...) deprecated so that user won't use it
MediaPlayer.OnPreparedListener preparedListener = new MediaPlayer.OnPreparedListener() {
@Override
public void onPrepared(MediaPlayer mp) {
//play(); // or should we call mp.start?
if (playerListener != null) {
playerListener.onPlayerPrepared(MyVideoPlayer.this);
} else {
Log.e(TAG, "preparedListener:onPrepared: player listener is null");
}
}
};
super.setOnPreparedListener(preparedListener);
}
@Nullable
public MyVideoPlayerListener getMyPlayerListener() {
return playerListener;
}
public void setMyPlayerListener(@NonNull MyVideoPlayerListener playerListener) {
this.playerListener = playerListener;
}
//---------following methods throw exception, do not use-----vvvvvvvvvvvvvvvvvvvvvvvvvvvvvv
@Override @Deprecated
public void setOnCompletionListener(@Nullable MediaPlayer.OnCompletionListener implementorsListener) {
//update: user is now not adding the on completion listener at all. he/she will only use our methods now.
throw new UnsupportedOperationException("Use MyVideoPlayer#setMyPlayerListener(...) ");
}
@Override @Deprecated
public void setOnErrorListener(MediaPlayer.OnErrorListener implementorsListener) {
//update: user is now not adding the on completion listener at all. he/she will only use our methods now.
throw new UnsupportedOperationException("Use MyVideoPlayer#setMyPlayerListener(...) ");
}
@Override @Deprecated
public int getDuration() {
throw new UnsupportedOperationException("Use MyVideoPlayer#gettotalDuration(...) ");
}
@Deprecated
public void start() {
// did because it doesn't look cool
throw new UnsupportedOperationException("Use MyVideoPlayer#play() ");
}
@Override @Deprecated
public void stopPlayback() {
// did because it doesn't look cool
throw new UnsupportedOperationException("Use MyVideoPlayer#stop() ");
}
//----------------------------------------------------------------------------------------------
@Override @Deprecated
public void setOnPreparedListener(MediaPlayer.OnPreparedListener implementorsListener) {
throw new UnsupportedOperationException("use MyVideoPlayer#onPlayerPrepared()");
}
public void play() {
super.start();
if (playerListener != null) {
playerListener.onPlay();
} else {
Log.e(TAG, "play: player listener is null");
}
currentPlaybackState = MyPlayerCurrentPlaybackState.PLAYING;
}
@Override
public void pause() {
// didn't throwed any exception because its already cool
super.pause();
currentPlaybackState = MyPlayerCurrentPlaybackState.PAUSED;
if (playerListener != null) {
playerListener.onPause();
} else {
Log.e(TAG, "play: player listener is null");
}
}
@Override
public void resume() {
// didn't throwed any exception because its already cool
super.start();
if (playerListener != null) {
playerListener.onResume();
} else {
Log.e(TAG, "play: player listener is null");
}
currentPlaybackState = MyPlayerCurrentPlaybackState.PLAYING;
}
public void stop() {
if (currentPlaybackState != MyPlayerCurrentPlaybackState.STOPPED) {
super.stopPlayback();
if (playerListener != null) {
playerListener.onStop();
} else {
Log.e(TAG, "play: player listener is null");
}
currentPlaybackState = MyPlayerCurrentPlaybackState.STOPPED;
}
}
public int gettotalDuration() {
return currentPlaybackState ==
MyPlayerCurrentPlaybackState.STOPPED ? 0 : super.getDuration();
}
//returns current video volume in range 0-100
public int getVolume() {
// Get the system's audio service and get media volume from it.
AudioManager audioManager =
(AudioManager) getContext().getSystemService(Context.AUDIO_SERVICE);
if (audioManager != null) {
double volume = audioManager.getStreamVolume(AudioManager.STREAM_MUSIC);
double max = audioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
if (max <= 0) {
return 0;
}
// Return a range from 0-100.
return (int) ((volume / max) * 100.0f);
}
return 0;
}
public enum MyPlayerCurrentPlaybackState {
STOPPED, PAUSED, PLAYING
}
public interface MyVideoPlayerListener {
void onPlay();
void onPause();
void onResume();
void onCompleted(MediaPlayer mp);
void onError( int what, int extra);
void onPlayerPrepared(MyVideoPlayer myVideoPlayer);
void onStop();
}
/* must implement features
public interface VideoPlayer {
void play();
void pause();
void resume();
int getCurrentPosition();
void seekTo(int videoPosition);
int getDuration();
int getVolume();
void stopPlayback();
void disablePlaybackControls();
void enablePlaybackControls();
void setVideoPath(String videoUrl);
void addPlayerCallback(PlayerCallback callback);
void removePlayerCallback(PlayerCallback callback);
}
*
* */
}
I hope the above example shows my intentions: I want user to NOT USE built in methods like start()
, setOnErrorListener(...)
, etc, but the library would either handle those functions itself (and give a callback to user) or has defined some other functions that I want the user to use, instead of built in functions (for eg I want user to call custom views' play()
function instead of already present start()
function coming from the parent via inheritance.)
But when I call the code via these lines:
MyVideoPlayer mvp = findViewById(R.id.mvp_main);
mvp.setVideoURI(Uri.parse(MyTags.CONTENT_URL));
mvp.setMyPlayerListener(new MyVideoPlayer.MyVideoPlayerListener() {
@Override
public void onPlay() {
Log.e(TAG, "onPlay: Video is now playing" );
}
@Override
public void onPause() {
Log.e(TAG, "onPause: Video Paused" );
}
@Override
public void onResume() {
Log.e(TAG, "onResume: video resumed" );
}
@Override
public void onCompleted(MediaPlayer mp) {
Log.e(TAG, "onCompleted: video playback completed" );
}
@Override
public void onError(int what, int extra) {
Log.e(TAG, "onError: error happenned: what:"+what+",extra:"+extra );
}
@Override
public void onPlayerPrepared(MyVideoPlayer myVideoPlayer) {
Log.e(TAG, "onPlayerPrepared: video is prepared,plau video" );
myVideoPlayer.play();
}
@Override
public void onStop() {
Log.e(TAG, "onStop: media playback stopped" );
}
});
i.e when the user uses my library in their app, it would give an exception whens calling those library defined functions like mvp.play()
saying java.lang.UnsupportedOperationException: Use MyVideoPlayer#play()
, indicating my overridden start()
function is being called instead of super.start()
. Why? Also, am I using the @Deprecated
annotation correctly (that is just for the sake of showing a strikethrough warning) or does this annotation make some unwanted changes as well?
you can apply proxy/decoration pattern:
write you own Videoplayer to implement and extend those classes that the original Vidoeview implements. here is the a pseudo code which could give you a idea:
//extends and implements the same interface and parent as the VideoView did, o that they will have the same methods to work properly.
public class MyVideoViewer extends SurfaceView implements
MediaController.MediaPlayerControl{
//private modifier to prevent others from directly calling the system's player
private VideoView view;
//you can just delete start method, so users using your player cannot even see this method.
/**void start(){
}*/
public void init(){
// do your things. can necessary method from videoplayer
view.setOnCompletionListener(listener) etc.
}
public void play()
{
view.start();
}
//indirectly call all necessary methods to make sure the system's player work properly.
@override
public void draw(arg1,arg2){
view.draw(arg1,arg2);
}
}
by doing, your player only exposes the methods you want it to expose, hide all the unnecessary methods behind your users. still it can work properly, because underneath, there is a proper system Videoview working for your player.