鸿蒙OS渲染/媒体/视频 原创

BLUESKYHOST
发布于 2021-3-29 10:25
1.8w浏览
1收藏

效果图

鸿蒙OS渲染/媒体/视频-鸿蒙开发者社区

不要复制粘贴把文件素材改成自己的ID也要改成自己的,RpxUtil.rpx 是我自己写的UI适配插件用来适配各种屏幕的。

 

第一步在需要显示视频的地方添加视频容器组件

<SurfaceProvider
                    ohos:id="$+id:money_list_media_player"
                    ohos:width="match_parent">

                </SurfaceProvider>
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.

编码实现

第二步 创建播放器

全局定义一个播放器

private static Player player;

 player=new Player(getContext());
  • 1.

第三步创建视频相关的回调方法

//播放处死哈 
private void playLocalFile(Surface surface) {

        try {
            RawFileDescriptor filDescriptor = getResourceManager().getRawFileEntry("resources/rawfile/mediamp4.mp4").openRawFileDescriptor();
            Source source = new Source(filDescriptor.getFileDescriptor(),filDescriptor.getStartPosition(),filDescriptor.getFileSize());
            player.setSource(source);
            player.setVideoSurface(surface);
            player.setPlayerCallback(new VideoPlayerCallback());
            player.prepare();
            sfProvider.setTop(0);
//            player.play();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
//视频加载回调
    private class VideoSurfaceCallback implements SurfaceOps.Callback {
        @Override
        public void surfaceCreated(SurfaceOps surfaceOps) {
            HiLog.info(logLabel,"surfaceCreated() called.");
            if (sfProvider.getSurfaceOps().isPresent()) {
                Surface surface = sfProvider.getSurfaceOps().get().getSurface();
                playLocalFile(surface);
            }
        }
        @Override
        public void surfaceChanged(SurfaceOps surfaceOps, int i, int i1, int i2) {
            HiLog.info(logLabel,"surfaceChanged() called.");
        }
        @Override
        public void surfaceDestroyed(SurfaceOps surfaceOps) {
            HiLog.info(logLabel,"surfaceDestroyed() called.");
        }
    }


//视频播放回调
    private class VideoPlayerCallback implements Player.IPlayerCallback {
        @Override
        public void onPrepared() {
            HiLog.info(logLabel,"onPrepared");
        }
        @Override
        public void onMessage(int i, int i1) {
            HiLog.info(logLabel,"onMessage");
        }
        @Override
        public void onError(int i, int i1) {
            HiLog.info(logLabel,"onError: i=" + i + ", i1=" + i1);
        }
        @Override
        public void onResolutionChanged(int i, int i1) {
            HiLog.info(logLabel,"onResolutionChanged");
        }
        @Override
        public void onPlayBackComplete() {
            HiLog.info(logLabel,"onPlayBackComplete");
            if (player != null) {
                player.stop();
                player = null;
            }
        }
        @Override
        public void onRewindToComplete() {
            HiLog.info(logLabel,"onRewindToComplete");
        }
        @Override
        public void onBufferingChange(int i) {
            HiLog.info(logLabel,"onBufferingChange");
        }
        @Override
        public void onNewTimedMetaData(Player.MediaTimedMetaData mediaTimedMetaData) {
            HiLog.info(logLabel,"onNewTimedMetaData");
        }
        @Override
        public void onMediaTimeIncontinuity(Player.MediaTimeInfo mediaTimeInfo) {
            HiLog.info(logLabel,"onMediaTimeIncontinuity");
        }

    }
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.
  • 43.
  • 44.
  • 45.
  • 46.
  • 47.
  • 48.
  • 49.
  • 50.
  • 51.
  • 52.
  • 53.
  • 54.
  • 55.
  • 56.
  • 57.
  • 58.
  • 59.
  • 60.
  • 61.
  • 62.
  • 63.
  • 64.
  • 65.
  • 66.
  • 67.
  • 68.
  • 69.
  • 70.
  • 71.
  • 72.
  • 73.
  • 74.
  • 75.
  • 76.
  • 77.
  • 78.
  • 79.
  • 80.
  • 81.

第四步调用视频渲染

//这个是全局的跟上面的播放器一样
 private SurfaceProvider sfProvider;

sfProvider =(SurfaceProvider) findComponentById(ResourceTable.Id_money_list_media_player);
sfProvider.getSurfaceOps().get().addCallback(new VideoSurfaceCallback());
sfProvider.pinToZTop(true);
sfProvider.setWidth(ComponentContainer.LayoutConfig.MATCH_PARENT);
sfProvider.setHeight((int) RpxUtil.rpx(350,getContext()));
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.

©著作权归作者所有,如需转载,请注明出处,否则将追究法律责任
已于2021-4-13 15:24:00修改
5
收藏 1
回复
举报
5
2
1
2条回复
按时间正序
/
按时间倒序
SummerRic
SummerRic

666

1
回复
2021-4-13 15:24:14
wx60a368ad83d76
wx60a368ad83d76

视频播放了,画面不动是什么情况

回复
2021-5-24 10:46:53


回复
    相关推荐