鴻蒙HarmonyOS視頻播放的實現(xiàn)
一、介紹
本篇Codelab將實現(xiàn)的內(nèi)容
HarmonyOS是面向全場景多終端的分布式操作系統(tǒng),使得應(yīng)用程序的開發(fā)打破了智能終端互通的性能和數(shù)據(jù)壁壘,業(yè)務(wù)邏輯原子化開發(fā),適配多端。通過一個簡單應(yīng)用開發(fā),體驗HarmonyOS的視頻播放能力
您將建立什么
在這個Codelab中,你將創(chuàng)建Demo Project,并將Demo編譯成Hap,此示例應(yīng)用程序展示了如何播放視頻。
您將會學(xué)到什么
如何創(chuàng)建一個HarmonyOS Demo Project如何構(gòu)建一個Hap并且將其部署到智慧屏真機通過此示例應(yīng)用體驗如何播放本地或者在線視頻
二、您需要什么
1. 硬件要求
- 操作系統(tǒng):Windows10 64位
- 內(nèi)存:8G及以上。
- 硬盤:100G及以上。
- 分辨率:1280*800及以上
2. 軟件要求
- 需手動下載安裝,詳細步驟請參考《DevEco Studio使用指南》2.1.2
- JDK:DevEco Studio自動安裝。
- Node.js:請手動下載安裝,詳細步驟請參考《DevEco Studio使用指南》2.1.3 下載和安裝Node.js。
- HarmonyOS SDK:待DevEco Studio安裝完成后,利用DevEco Studio來加載HarmonyOS SDK。詳細步驟請參考《DevEco Studio使用指南》2.1.6 加載HarmonyOS SDK。
- Maven庫依賴包:如需手動拷貝和配置,詳細步驟請參考《DevEco Studio使用指南》2.3 離線方式配置Maven庫。
3. 需要的知識點
Java基礎(chǔ)開發(fā)能力。
三、能力接入準(zhǔn)備
實現(xiàn)HarmonyOS應(yīng)用開發(fā),需要完成以下準(zhǔn)備工作:
1.環(huán)境準(zhǔn)備。
2.環(huán)境搭建。
3.創(chuàng)建項目
4.申請調(diào)試證書
5.應(yīng)用開發(fā)
具體操作,請按照《DevEco Studio使用指南》中詳細說明來完成。
提示:需要通過注冊成開發(fā)者才能完成集成準(zhǔn)備中的操作。
四、代碼片段
1. 布局:
創(chuàng)建播放視頻的Ability
public class VedioPlayAbilitySlice extends AbilitySlice implements SurfaceOps.Callback
布局截圖:
布局代碼:
//設(shè)置頁面背景透明 WindowManager windowManager = WindowManager.getInstance(); Window window = windowManager.getTopWindow().get(); window.setTransparent(true); //頁面父布局 DependentLayout myLayout = new DependentLayout(this); DependentLayout.LayoutConfig params = new DependentLayout.LayoutConfig(MATCH_PARENT, MATCH_PARENT); myLayout.setLayoutConfig(params); //顯示視頻的自定義videoView DependentLayout.LayoutConfig lpVideo = new DependentLayout.LayoutConfig(MATCH_PARENT, MATCH_PARENT); videoView = new VideoView(this, this); videoView.setHandler(handler); myLayout.addComponent(videoView, lpVideo); DependentLayout rlParent = new DependentLayout(this); DependentLayout.LayoutConfig lpParent = new DependentLayout.LayoutConfig(MATCH_PARENT, WRAP_CONTENT); lpParent.addRule(DependentLayout.ALIGN_PARENT_BOTTOM); lpParent.leftMargin = ConvertUtils.dp2Px(40); lpParent.rightMargin = ConvertUtils.dp2Px(40); lpParent.bottomMargin = ConvertUtils.dp2Px(40); myLayout.addComponent(rlParent, lpParent); //顯示播放暫停按鈕 playBtn = new Image(this); DependentLayout.LayoutConfig lpPlayBtn = new DependentLayout.LayoutConfig(ConvertUtils.dp2Px(40), ConvertUtils.dp2Px(40)); lpPlayBtn.addRule(DependentLayout.ALIGN_PARENT_RIGHT); playBtn.setLayoutConfig(lpPlayBtn); playBtn.setId(1112); playBtn.setPixelMap(ResourceTable.Media_pause); playBtn.setScaleType(Image.ScaleType.SCALE_TO_FULL); playBtn.invalidate(); playBtn.setClickedListener(new Component.ClickedListener() { @Override public void onClick(Component component) { if (videoView.getPlayState() == VideoView.STATE_PLAYING) { videoView.pause(); playBtn.setPixelMap(ResourceTable.Media_play); } else { videoView.start(); playBtn.setPixelMap(ResourceTable.Media_pause); } } }); rlParent.addComponent(playBtn); //顯示當(dāng)前視頻播放時間 timePlay = new Text(this); DependentLayout.LayoutConfig lpTimePlay = new DependentLayout.LayoutConfig(WRAP_CONTENT, WRAP_CONTENT); lpTimePlay.addRule(DependentLayout.CENTER_VERTICAL); timePlay.setLayoutConfig(lpTimePlay); timePlay.setId(1111); timePlay.setText("00:00/00:00"); timePlay.setTextSize(40); timePlay.setTextColor(Color.WHITE); rlParent.addComponent(timePlay); // 顯示播放進度條 roundProgressBar = new ProgressBar(this); roundProgressBar.setProgressWidth(ConvertUtils.dp2Px(5)); roundProgressBar.setProgressColor(Color.RED); roundProgressBar.setMax(100); roundProgressBar.setProgress(0); DependentLayout.LayoutConfig lpProgressBar = new DependentLayout.LayoutConfig(MATCH_PARENT, ConvertUtils.dp2Px(40)); lpProgressBar.addRule(DependentLayout.RIGHT_OF, timePlay.getId()); lpProgressBar.leftMargin = ConvertUtils.dp2Px(20); lpProgressBar.rightMargin = ConvertUtils.dp2Px(60); rlParent.addComponent(roundProgressBar, lpProgressBar);
2. 自定義VideoView
繼承父類SurfaceProvider:
public class VideoView extends SurfaceProvider implements Player.IPlayerCallback
初始化:
public VideoView(Context context, SurfaceOps.Callback callback) { super(context); player = new Player(getContext()); player.setPlayerCallback(this); Optional<SurfaceOps> surfaceHolderOptional = getSurfaceOps(); SurfaceOps surfaceHolder = surfaceHolderOptional.get(); surfaceHolder.addCallback(callback); setZOrderOnTop(false); state = STATE_INIT; }
播放視頻
如果播放的是在線視頻,需要申請網(wǎng)絡(luò)權(quán)限:
"reqPermissions": [ { "name": "harmonyos.permission.INTERNET" } ] public void playAssets(String fileName, boolean isLooping, SurfaceOps holder) { try { //播放本地視頻: //player.setSource(getContext().getResourceManager().getRawFileEntry(fileName).openRawFileDescriptor()); //播放在線視頻: player.setSource(new Source("https://media.w3.org/2010/05/sintel/trailer.mp4")); player.setVideoSurface(holder.getSurface()); player.enableSingleLooping(isLooping); player.enableScreenOn(true); player.prepare(); initPlayViewSize(); player.play(); if (state != STATE_INIT) { player.rewindTo(currentTime * 1000); } state = STATE_PLAYING; handler.sendEvent(InnerEvent.get(MESSAGE_UPDATE_PLAY_TIME)); } catch (Exception e) { e.printStackTrace(); Log.hiLog(e.toString()); } } private void initPlayViewSize() { int videoWidth = player.getVideoWidth(); int videoHeight = player.getVideoHeight(); Log.hiLog("videoWidth:" + videoWidth + ", videoHeight:" + videoHeight); if (videoWidth < videoHeight) { double scale = screenHeight * 1.f / videoHeight; double currHeight = videoHeight * scale; double currWidth = videoWidth * scale; setHeight(((int) currHeight)); setWidth(((int) currWidth)); } else { double scale = screenWidth * 1.f / videoWidth; double currHeight = videoHeight * scale; double currWidth = videoWidth * scale; setHeight(((int) currHeight)); setWidth(((int) currWidth)); } }
暫停:
public void pause() { if (state == STATE_PLAYING) { player.pause(); state = STATE_PAUSE; } }
在AbilitySlice啟動自動播放:
@Override public void surfaceCreated(SurfaceOps surfaceOps) { Log.hiLog("surfaceCreated"); videoView.playAssets("resources/rawfile/VID_20200613_204240.mp4", true, surfaceOps); } @Override public void surfaceDestroyed(SurfaceOps surfaceOps) { Log.hiLog("surfaceDestroyed"); videoView.stop(); }
3. 響應(yīng)遙控器點擊
@Override public boolean onKeyUp(int keyCode, KeyEvent keyEvent) { switch (keyCode) { case KeyEvent.KEY_DPAD_CENTER: case KeyEvent.KEY_ENTER: playBtn.performClick(); return true; default: break; } return false; }
4. 編譯運行該應(yīng)用
通過hdc連接大屏設(shè)備
先查看智慧屏IP:
大屏設(shè)置->"網(wǎng)絡(luò)與連接"->"網(wǎng)絡(luò)"->"有線網(wǎng)絡(luò)"
在cmd或者IDE的Terminal輸入命令:
hdc tconn 192.168.3.9:5555
運行hap
五、恭喜你
干得好,你已經(jīng)成功完成了HarmonyOS應(yīng)用開發(fā)E2E體驗,學(xué)到了:
1.如何創(chuàng)建一個HarmonyOS Demo Project
2.如何構(gòu)建一個Hap并且將其部署到真機
3.在HarmonyOS上如何使用HarmonyOS的視頻播放的能力
到此這篇關(guān)于鴻蒙HarmonyOS視頻播放的實現(xiàn)的文章就介紹到這了,更多相關(guān)HarmonyOS視頻播放內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- DevEco Studio 2.0開發(fā)鴻蒙HarmonyOS應(yīng)用初體驗全面測評(推薦)
- 鴻蒙開發(fā)之處理圖片位圖操作的方法詳解(HarmonyOS鴻蒙開發(fā)基礎(chǔ)知識)
- 鴻蒙開發(fā)之Button按鈕類型及如何通過代碼設(shè)置(HarmonyOS鴻蒙開發(fā)基礎(chǔ)知識)
- 鴻蒙HarmonyOS 分布式任務(wù)調(diào)度的實現(xiàn)
- HarmonyOS鴻蒙基本控件的實現(xiàn)
- HarmonyOS鴻蒙實現(xiàn)HelloWorld應(yīng)用開發(fā)E2E體驗
- 鴻蒙HarmonyOS App開發(fā)造輪子之自定義圓形圖片組件的實例代碼
相關(guān)文章
Windows Docker 安裝 Gitlab Volume權(quán)限問題解決方案
這篇文章主要介紹了Windows Docker 安裝 Gitlab Volume權(quán)限問題解決方案,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2019-09-09Windows服務(wù)器修改遠程桌面默認(rèn)端口過程圖示
這篇文章主要介紹了Windows服務(wù)器修改遠程桌面默認(rèn)端口過程圖示,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2019-11-11win10設(shè)定計劃任務(wù)時提示所指定的賬戶名稱無效問題解析
這篇文章主要介紹了win10設(shè)定計劃任務(wù)時提示所指定的賬戶名稱無效問題解析,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2019-09-09鴻蒙HarmonyOS 分布式任務(wù)調(diào)度的實現(xiàn)
這篇文章主要介紹了鴻蒙HarmonyOS 分布式任務(wù)調(diào)度的實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-09-09Win10 系統(tǒng)下快速搭建mxnet框架cpu版本
這篇文章主要介紹了Win10 系統(tǒng)下快速搭建mxnet框架cpu版本,本文圖文并茂給大家介紹的非常詳細,具有一定的參考借鑒價值,需要的朋友可以參考下2019-10-10鴻蒙HarmonyO開發(fā)實戰(zhàn)-Web組件中的Cookie及數(shù)據(jù)存儲
Cookie是一種存儲在用戶計算機上的小文本文件,用于在用戶訪問網(wǎng)站時存儲和提取信息,它由網(wǎng)站服務(wù)器發(fā)送到用戶的瀏覽器,并存儲在用戶的計算機上,每當(dāng)用戶訪問該網(wǎng)站時,瀏覽器將發(fā)送該Cookie回服務(wù)器,以用于識別用戶和存儲用戶的首選項和其他信息2024-06-06