public class MySurfaceView extends GLSurfaceView implements SurfaceHolder.Callback2 {
private IMediaPlayer mIjkMediaPlayer;
public MySurfaceView(Context context) {
this(context,null);
}
public MySurfaceView(Context context, AttributeSet attrs) {
super(context, attrs);
this.getHolder().addCallback(this);
}
@Override
public void surfaceRedrawNeeded(@NonNull SurfaceHolder holder) {
}
@Override
public void surfaceCreated(@NonNull SurfaceHolder holder) {
IjkMediaPlayer.loadLibrariesOnce(null);
mIjkMediaPlayer = new IjkMediaPlayer();
IjkMediaPlayer.native_setLogLevel(IjkMediaPlayer.IJK_LOG_DEBUG);
((IjkMediaPlayer)mIjkMediaPlayer).setOption(IjkMediaPlayer.OPT_CATEGORY_PLAYER, "mediacodec", 1);
((IjkMediaPlayer)mIjkMediaPlayer).setOption(IjkMediaPlayer.OPT_CATEGORY_PLAYER, "start-on-prepared", 1);
MyLog.printLog("当前类:MySurfaceView,当前方法:surfaceCreated,当前线程:"+ Thread.currentThread().getName()+",信息:");
try {
mIjkMediaPlayer.setDataSource("http://v26-web.douyinvod.com/9bc1da753b72cf58198a5ac6b33f5c5f/642ac71d/video/tos/cn/tos-cn-ve-15c001-alinc2/oUnw3NQsIhdSv2DA3DyAnzwBEUZhDDkNgAnfce/?a=6383&ch=26&cr=3&dr=0&lr=all&cd=0%7C0%7C0%7C3&cv=1&br=4099&bt=4099&cs=0&ds=4&ft=bvTKJbQQqUYqfJEZao0OW_EklpPiXkHIzMVJERXpwrbPD-I&mime_type=video_mp4&qs=0&rc=Ozs7Nmk5aDs6Njs1OWVkNEBpM3c7cmc6ZnJpajMzNGkzM0A0Ml4yLjMzX2AxNGNeNi8vYSNqM15ncjQwa2dgLS1kLTBzcw%3D%3D&l=20230403193118DD1B97F459BD1305F3B1&btag=8000&testst=1680521484498");
} catch (IOException e) {
}
mIjkMediaPlayer.setDisplay(holder);
mIjkMediaPlayer.prepareAsync();
((IjkMediaPlayer)mIjkMediaPlayer).setOnAllListener(new IMediaPlayer.OnAllListener() {
@Override
public void onPrepared(IMediaPlayer iMediaPlayer) {
}
@Override
public void onCompletion(IMediaPlayer iMediaPlayer) {
}
@Override
public void onBufferingUpdate(IMediaPlayer iMediaPlayer, int i) {
}
@Override
public void onSeekComplete(IMediaPlayer iMediaPlayer) {
}
@Override
public void onVideoSizeChanged(IMediaPlayer iMediaPlayer, int width, int height, int i2, int i3) {
ConstraintLayout.LayoutParams layoutParams = new ConstraintLayout.LayoutParams(MySurfaceView.this.getLayoutParams());
layoutParams.height = iMediaPlayer.getVideoHeight();
layoutParams.width = iMediaPlayer.getVideoWidth();
requestLayout();
}
@Override
public void onError(IMediaPlayer iMediaPlayer, int i, int i1) {
}
@Override
public void onInfo(IMediaPlayer iMediaPlayer, int i, int i1) {
}
@Override
public void onTimedText(IMediaPlayer iMediaPlayer, IjkTimedText ijkTimedText) {
}
@Override
public void onServerCmd(IMediaPlayer iMediaPlayer, IjkServerCmd ijkServerCmd) {
}
});
}
@Override
public void surfaceChanged(@NonNull SurfaceHolder holder, int format, int width, int height) {
}
@Override
public void surfaceDestroyed(@NonNull SurfaceHolder holder) {
}
}
==================================分割线==============================================
public class MySurfaceView extends SurfaceView implements SurfaceHolder.Callback2 {
private IMediaPlayer mIjkMediaPlayer;
public MySurfaceView(Context context) {
this(context,null);
}
public MySurfaceView(Context context, AttributeSet attrs) {
this(context, attrs,0);
}
public MySurfaceView(Context context, AttributeSet attrs, int defStyleAttr) {
this(context, attrs, defStyleAttr,0);
}
public MySurfaceView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);
this.getHolder().addCallback(this);
}
@Override
public void surfaceRedrawNeeded(@NonNull SurfaceHolder holder) {
}
@Override
public void surfaceCreated(@NonNull SurfaceHolder holder) {
IjkMediaPlayer.loadLibrariesOnce(null);
mIjkMediaPlayer = new IjkMediaPlayer();
IjkMediaPlayer.native_setLogLevel(IjkMediaPlayer.IJK_LOG_DEBUG);
((IjkMediaPlayer)mIjkMediaPlayer).setOption(IjkMediaPlayer.OPT_CATEGORY_PLAYER, "mediacodec", 1);
((IjkMediaPlayer)mIjkMediaPlayer).setOption(IjkMediaPlayer.OPT_CATEGORY_PLAYER, "start-on-prepared", 1);
MyLog.printLog("当前类:MySurfaceView,当前方法:surfaceCreated,当前线程:"+ Thread.currentThread().getName()+",信息:");
try {
mIjkMediaPlayer.setDataSource("http://v26-web.douyinvod.com/9bc1da753b72cf58198a5ac6b33f5c5f/642ac71d/video/tos/cn/tos-cn-ve-15c001-alinc2/oUnw3NQsIhdSv2DA3DyAnzwBEUZhDDkNgAnfce/?a=6383&ch=26&cr=3&dr=0&lr=all&cd=0%7C0%7C0%7C3&cv=1&br=4099&bt=4099&cs=0&ds=4&ft=bvTKJbQQqUYqfJEZao0OW_EklpPiXkHIzMVJERXpwrbPD-I&mime_type=video_mp4&qs=0&rc=Ozs7Nmk5aDs6Njs1OWVkNEBpM3c7cmc6ZnJpajMzNGkzM0A0Ml4yLjMzX2AxNGNeNi8vYSNqM15ncjQwa2dgLS1kLTBzcw%3D%3D&l=20230403193118DD1B97F459BD1305F3B1&btag=8000&testst=1680521484498");
} catch (IOException e) {
}
mIjkMediaPlayer.setDisplay(holder);
mIjkMediaPlayer.prepareAsync();
((IjkMediaPlayer)mIjkMediaPlayer).setOnAllListener(new IMediaPlayer.OnAllListener() {
@Override
public void onPrepared(IMediaPlayer iMediaPlayer) {
}
@Override
public void onCompletion(IMediaPlayer iMediaPlayer) {
}
@Override
public void onBufferingUpdate(IMediaPlayer iMediaPlayer, int i) {
}
@Override
public void onSeekComplete(IMediaPlayer iMediaPlayer) {
}
@Override
public void onVideoSizeChanged(IMediaPlayer iMediaPlayer, int width, int height, int i2, int i3) {
ConstraintLayout.LayoutParams layoutParams = new ConstraintLayout.LayoutParams(MySurfaceView.this.getLayoutParams());
layoutParams.height = iMediaPlayer.getVideoHeight();
layoutParams.width = iMediaPlayer.getVideoWidth();
requestLayout();
}
@Override
public void onError(IMediaPlayer iMediaPlayer, int i, int i1) {
}
@Override
public void onInfo(IMediaPlayer iMediaPlayer, int i, int i1) {
}
@Override
public void onTimedText(IMediaPlayer iMediaPlayer, IjkTimedText ijkTimedText) {
}
@Override
public void onServerCmd(IMediaPlayer iMediaPlayer, IjkServerCmd ijkServerCmd) {
}
});
}
@Override
public void surfaceChanged(@NonNull SurfaceHolder holder, int format, int width, int height) {
}
@Override
public void surfaceDestroyed(@NonNull SurfaceHolder holder) {
}
}
SurfaceView 可以播放视频而 GLSurfaceView 不可以
1
cxtrinityy 2023-04-03 20:18:09 +08:00 via Android
看这名字就是 OpenGL 的专用类吧,和一般 Surfaceview 用法不同吧
|
2
fhj OP @cxtrinityy GLSurfaceView 是 SurfaceView 的子类,就多了个 GL 环境
|
3
psklf 2023-04-04 15:11:53 +08:00
GLSurfaceView 没有 setRenderer 啊 你的 renderer 在哪里?
|