gpt4 book ai didi

java - 阻止用户调用父函数

转载 作者:行者123 更新时间:2023-12-02 01:16:16 25 4
gpt4 key购买 nike

更新(24/10/19):我稍微修改了问题的语言。投反对票的人,如果仍然不清楚,请通过评论公开告诉我,这样我就可以进一步完善它(我只是看到最后的投票说不清楚,既没有模组名称也没有原因)。请注意,英语不是我的母语,我真的需要这方面的帮助。

<小时/>

所以我猜这与 java 基础知识和 OOP 更相关,但我已经看到了常见 Android 类遵循的一些模式。 Google 经常弃用旧功能,转而支持更好的功能,我正在考虑对我的自定义 View 做类似的事情。

我想在 android 中构建一个视频播放器,它应该很容易创建,并且应该返回多个事件的回调。为此,我希望用户使用我的功能而不是内置视频 View 的功能。因此,我正在应用几种方法来阻止用户使用这些内置函数:

  • 我使用 @Deprecated 表示法来显示带有 Strike 的函数名称,例如 getVolume()
  • 我已经覆盖了那些我不希望用户使用的函数,并用如下错误替换了它们的实现:
@Override @deprecated
public void getWidth(){
throw new UnsupportedOperationException("useMyVideoPlayer#getVideoWidth()");
}
  • 我自己在 init() 或其各自的替代方案中实现这些函数。这是代码(检查代码行上的注释: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);

}
*
* */


}


我希望上面的示例显示了我的 Intent :我希望用户不使用内置方法,例如 start()setOnErrorListener(...) 等,但库要么自己处理这些函数(并向用户提供回调),要么定义了一些我希望用户使用的其他函数,而不是内置函数(例如我希望用户调用自定义 View 的 play() 函数,而不是通过 inheritance. 来自父级的已存在的 start() 函数)

但是当我通过这些行调用代码时:


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" );

}
});




即,当用户在他们的应用程序中使用我的库时,在调用这些库定义的函数(例如 mvp.play() )时会给出异常 java.lang.UnsupportedOperationException: Use MyVideoPlayer #play() ,表示正在调用我重写的 start() 函数,而不是 super.start() 。为什么?另外,我是否正确使用了 @Deprecated 注释(这只是为了显示删除线警告)或者该注释是否也进行了一些不需要的更改?

最佳答案

您可以应用代理/装饰模式:

编写您自己的 Videoplayer 来实现和扩展原始 Vidoeview 实现的那些类。这是一个伪代码,可以给您一个想法:

     //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);
}
}

通过这样做,您的播放器仅公开您希望其公开的方法,将所有不必要的方法隐藏在用户后面。它仍然可以正常工作,因为在下面,有一个适合您的播放器的正确系统Videoview。

关于java - 阻止用户调用父函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58504487/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com