目录:

1、player初始化

2、设置source

3、渲染到SurfaceProvider上

4、player的各种监听回调

5、播放,暂停等

6、时间的更新

官方指导地址:https://developer.harmonyos.com/cn/docs/documentation/doc-guides/media-video-player-0000000000044178

关键步骤 1 player初始化

 player = new Player(getContext());
 

   2,设置 Source; 视频播放的源(可以是本地,也可以是网络)

     目前只研究到放文件在工程里

    private String url = "entry/resources/base/media/gubeishuizhen.mp4";


RawFileDescriptor fd = null;
        try {
            fd = getContext().getResourceManager().getRawFileEntry(url).openRawFileDescriptor();
            Source source = new Source(fd.getFileDescriptor(), fd.getStartPosition(), fd.getFileSize());
            player.setSource(source);
        } catch (IOException e) {
            Log.e(TAG, "Exception %s", e.toString());
        }
 

3,渲染到SurfaceProvider上

 surfaceProvider = (SurfaceProvider) findComponentById(ResourceTable.Id_surface_provider);
        surfaceProvider.pinToZTop(true);// 不设置这个 画面不显示;

        surfaceProvider.getSurfaceOps().get().addCallback(new SurfaceOps.Callback() {
            @Override
            public void surfaceCreated(SurfaceOps surfaceOps) {
                Log.d(TAG, "surfaceCreated");
                player.setVideoSurface(surfaceOps.getSurface());
                player.prepare();
            }

            @Override
            public void surfaceChanged(SurfaceOps holder, int format, int width, int height) {
                Log.d(TAG, "surfaceChanged format:%d ;width:%d  ;height:%d", format, width, height);
            }

            @Override
            public void surfaceDestroyed(SurfaceOps surfaceOps) {
                Log.d(TAG, "surfaceDestroyed");
            }
        });

4,player的各种监听回调;

 player.setPlayerCallback(new Player.IPlayerCallback() {
            @Override
            public void onPrepared() {
                Log.d(TAG, "onPrepared");
            }

            @Override
            public void onMessage(int type, int extra) {
                Log.d(TAG, "onMessage type:%d ;extra:%d", type, extra);
            }

            @Override
            public void onError(int errorType, int errorCode) {
                Log.d(TAG, "onError errorType:%d ;errorCode:%d", errorType, errorCode);
            }

            @Override
            public void onResolutionChanged(int width, int height) {
                Log.d(TAG, "onResolutionChanged width:%d ;height:%d", width, height);
            }

            @Override
            public void onPlayBackComplete() {
                Log.d(TAG, "onPlayBackComplete");
            }

            @Override
            public void onRewindToComplete() {
                Log.d(TAG, "onRewindToComplete");
            }

            @Override
            public void onBufferingChange(int percent) {
                Log.d(TAG, "onBufferingChange percent:%d", percent);
            }

            @Override
            public void onNewTimedMetaData(Player.MediaTimedMetaData mediaTimedMetaData) {
                Log.d(TAG, "onNewTimedMetaData");
            }

            @Override
            public void onMediaTimeIncontinuity(Player.MediaTimeInfo mediaTimeInfo) {
                Log.d(TAG, "onMediaTimeIncontinuity");
            }
        });
 

5,播放,暂停等

  @Override
    public void onClick(Component component) {
        switch (component.getId()) {
            case ResourceTable.Id_play_btn:
                if (player != null) {
                    player.play();
                }
                break;
            case ResourceTable.Id_pause_btn:
                if (player != null) {
                    player.pause();
                }
                break;
        }
    }
 

6,时间的更新,用的EventHandler

        EventRunner runner = EventRunner.create(true);
        timeHandler = new TimeHandler(runner);
        sendTimeHandler();

     private class TimeHandler extends EventHandler {

        public TimeHandler(EventRunner runner) throws IllegalArgumentException {
            super(runner);
        }

        @Override
        protected void processEvent(InnerEvent event) {
            super.processEvent(event);
            if (event.eventId == 0) {
                if (player != null && player.isNowPlaying()) {
                    int duration = player.getDuration();
                    int currentTime = player.getCurrentTime();
                    getUITaskDispatcher().syncDispatch(new Runnable() {
                        @Override
                        public void run() {
                            progressBar.setProgressValue(currentTime);
                            progressBar.setMaxValue(duration);
                            durationText.setText(DateUtils.msToString(duration));
                            currentTimeText.setText(DateUtils.msToString(currentTime));
                        }
                    });
                }
            }
            sendTimeHandler();
        }
    }

    private void sendTimeHandler() {
        timeHandler.sendEvent(
            0, 1000, EventHandler.Priority.IMMEDIATE);
    }

作者:陈浩南xxx

想了解更多内容,请访问51CTO和华为合作共建的鸿蒙社区:https://harmonyos.51cto.com

05-07 10:19