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 不可以
这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。
V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。
V2EX is a community of developers, designers and creative people.