HarmonyOS技术社区 · 2021年04月27日

鸿蒙 视频播放开发(基础文,适合新人)

目录:
1、player初始化
2、设置source
3、渲染到SurfaceProvider上
4、player的各种监听回调
5、播放,暂停等
6、时间的更新

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

鸿蒙 视频播放开发(基础文,适合新人)

<span class="size" style="font-size:18px">关键步骤 1 player初始化</span>

` player  <span class="token operator">=</span>  <span class="token keyword">new</span>  <span class="token class-name">Player</span><span class="token punctuation">(</span><span class="token function">getContext</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>`




<span class="size" style="font-size:18px">   2,设置 Source; 视频播放的源(可以是本地,也可以是网络)</span>

<span class="size" style="font-size:18px">     目前只研究到放文件在工程里</span>

` <span class="token keyword">private</span>  <span class="token class-name">String</span>  url  <span class="token operator">=</span>  <span class="token string">"entry/resources/base/media/gubeishuizhen.mp4"</span><span class="token punctuation">;</span>


<span class="token class-name">RawFileDescriptor</span>  fd  <span class="token operator">=</span>  <span class="token keyword">null</span><span class="token punctuation">;</span>
         <span class="token keyword">try</span>  <span class="token punctuation">{</span>
            fd  <span class="token operator">=</span>  <span class="token function">getContext</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">getResourceManager</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">getRawFileEntry</span><span class="token punctuation">(</span>url<span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">openRawFileDescriptor</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
             <span class="token class-name">Source</span>  source  <span class="token operator">=</span>  <span class="token keyword">new</span>  <span class="token class-name">Source</span><span class="token punctuation">(</span>fd<span class="token punctuation">.</span><span class="token function">getFileDescriptor</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">,</span>  fd<span class="token punctuation">.</span><span class="token function">getStartPosition</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">,</span>  fd<span class="token punctuation">.</span><span class="token function">getFileSize</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
            player<span class="token punctuation">.</span><span class="token function">setSource</span><span class="token punctuation">(</span>source<span class="token punctuation">)</span><span class="token punctuation">;</span>
         <span class="token punctuation">}</span>  <span class="token keyword">catch</span>  <span class="token punctuation">(</span><span class="token class-name">IOException</span>  e<span class="token punctuation">)</span>  <span class="token punctuation">{</span>
             <span class="token class-name">Log</span><span class="token punctuation">.</span><span class="token function">e</span><span class="token punctuation">(</span>TAG<span class="token punctuation">,</span>  <span class="token string">"Exception %s"</span><span class="token punctuation">,</span>  e<span class="token punctuation">.</span><span class="token function">toString</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
         <span class="token punctuation">}</span>`




<span class="size" style="font-size:18px">3,渲染到SurfaceProvider上</span>

` surfaceProvider  <span class="token operator">=</span>  <span class="token punctuation">(</span><span class="token class-name">SurfaceProvider</span><span class="token punctuation">)</span>  <span class="token function">findComponentById</span><span class="token punctuation">(</span><span class="token class-name">ResourceTable<span class="token punctuation">.</span>Id\_surface\_provider</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        surfaceProvider<span class="token punctuation">.</span><span class="token function">pinToZTop</span><span class="token punctuation">(</span><span class="token boolean">true</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment">// 不设置这个 画面不显示;</span>

        surfaceProvider<span class="token punctuation">.</span><span class="token function">getSurfaceOps</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">get</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">addCallback</span><span class="token punctuation">(</span><span class="token keyword">new</span>  <span class="token class-name">SurfaceOps<span class="token punctuation">.</span>Callback</span><span class="token punctuation">(</span><span class="token punctuation">)</span>  <span class="token punctuation">{</span>
             <span class="token annotation punctuation">@Override</span>
             <span class="token keyword">public</span>  <span class="token keyword">void</span>  <span class="token function">surfaceCreated</span><span class="token punctuation">(</span><span class="token class-name">SurfaceOps</span>  surfaceOps<span class="token punctuation">)</span>  <span class="token punctuation">{</span>
                 <span class="token class-name">Log</span><span class="token punctuation">.</span><span class="token function">d</span><span class="token punctuation">(</span>TAG<span class="token punctuation">,</span>  <span class="token string">"surfaceCreated"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
                player<span class="token punctuation">.</span><span class="token function">setVideoSurface</span><span class="token punctuation">(</span>surfaceOps<span class="token punctuation">.</span><span class="token function">getSurface</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
                player<span class="token punctuation">.</span><span class="token function">prepare</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
             <span class="token punctuation">}</span>

             <span class="token annotation punctuation">@Override</span>
             <span class="token keyword">public</span>  <span class="token keyword">void</span>  <span class="token function">surfaceChanged</span><span class="token punctuation">(</span><span class="token class-name">SurfaceOps</span>  holder<span class="token punctuation">,</span>  <span class="token keyword">int</span>  format<span class="token punctuation">,</span>  <span class="token keyword">int</span>  width<span class="token punctuation">,</span>  <span class="token keyword">int</span>  height<span class="token punctuation">)</span>  <span class="token punctuation">{</span>
                 <span class="token class-name">Log</span><span class="token punctuation">.</span><span class="token function">d</span><span class="token punctuation">(</span>TAG<span class="token punctuation">,</span>  <span class="token string">"surfaceChanged format:%d ;width:%d ;height:%d"</span><span class="token punctuation">,</span>  format<span class="token punctuation">,</span>  width<span class="token punctuation">,</span>  height<span class="token punctuation">)</span><span class="token punctuation">;</span>
             <span class="token punctuation">}</span>

             <span class="token annotation punctuation">@Override</span>
             <span class="token keyword">public</span>  <span class="token keyword">void</span>  <span class="token function">surfaceDestroyed</span><span class="token punctuation">(</span><span class="token class-name">SurfaceOps</span>  surfaceOps<span class="token punctuation">)</span>  <span class="token punctuation">{</span>
                 <span class="token class-name">Log</span><span class="token punctuation">.</span><span class="token function">d</span><span class="token punctuation">(</span>TAG<span class="token punctuation">,</span>  <span class="token string">"surfaceDestroyed"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
             <span class="token punctuation">}</span>
         <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>`




<span class="size" style="font-size:18px">4,player的各种监听回调;</span>

 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");
            }
        });






<span class="size" style="font-size:18px">5,播放,暂停等</span>

  @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;
        }
    }






<span class="size" style="font-size:18px">6,时间的更新,用的EventHandler</span>

        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

21_9.jpg

推荐阅读
关注数
3019
内容数
446
华为鸿蒙相关技术,活动及资讯,欢迎关注及加入创作
目录
极术微信服务号
关注极术微信号
实时接收点赞提醒和评论通知
安谋科技学堂公众号
关注安谋科技学堂
实时获取安谋科技及 Arm 教学资源
安谋科技招聘公众号
关注安谋科技招聘
实时获取安谋科技中国职位信息