V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
fhj
V2EX  ›  Android

请问为什么 SurfaceView 可以播放视频而 GLSurfaceView 不可以

  •  
  •   fhj · 2023-04-03 19:44:12 +08:00 · 8387 次点击
    这是一个创建于 632 天前的主题,其中的信息可能已经有所发展或是发生改变。

    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 不可以

    4 条回复    2023-04-06 23:47:57 +08:00
    cxtrinityy
        1
    cxtrinityy  
       2023-04-03 20:18:09 +08:00 via Android
    看这名字就是 OpenGL 的专用类吧,和一般 Surfaceview 用法不同吧
    fhj
        2
    fhj  
    OP
       2023-04-03 20:51:01 +08:00
    @cxtrinityy GLSurfaceView 是 SurfaceView 的子类,就多了个 GL 环境
    psklf
        3
    psklf  
       2023-04-04 15:11:53 +08:00
    GLSurfaceView 没有 setRenderer 啊 你的 renderer 在哪里?
    fhj
        4
    fhj  
    OP
       2023-04-06 23:47:57 +08:00 via Android
    @psklf 添加了也不行
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   实用小工具   ·   3080 人在线   最高记录 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 21ms · UTC 13:29 · PVG 21:29 · LAX 05:29 · JFK 08:29
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.