video-playback.md 16.8 KB
Newer Older
W
wusongqing 已提交
1
# 视频播放开发指导
2

W
wusongqing 已提交
3
## 场景介绍
4

W
wusongqing 已提交
5
视频播放的主要工作是将视频数据转码并输出到设备进行播放,同时管理播放任务。本文将对视频播放全流程、视频切换、视频循环播放等场景开发进行介绍说明。
6

W
wusongqing 已提交
7
**图1** 视频播放状态机
8

W
wusongqing 已提交
9
![zh-ch_image_video_state_machine](figures/zh-ch_image_video_state_machine.png)
10

Z
zengyawen 已提交
11

12

W
wusongqing 已提交
13
**图2** 视频播放零层图
14

W
wusongqing 已提交
15
![zh-ch_image_video_player](figures/zh-ch_image_video_player.png)
16

W
wusongqing 已提交
17
*注意:视频播放需要显示、音频、编解码等硬件能力。
18

W
wusongqing 已提交
19 20 21
1. 三方应用从Xcomponent组件获取surfaceID。
2. 三方应用把surfaceID传递给VideoPlayer JS。
3. 媒体服务把帧数据flush给surface buffer。
22

23 24 25 26 27 28 29 30 31 32 33 34 35
## 兼容性说明

推荐使用视频软件主流的播放格式和主流分辨率,不建议开发者自制非常或者异常码流,以免产生无法播放、卡住、花屏等兼容性问题。若发生此类问题不会影响系统,退出码流播放即可。

主流的播放格式和主流分辨率如下:

| 视频容器规格 |                     规格描述                      |               分辨率               |
| :----------: | :-----------------------------------------------: | :--------------------------------: |
|     mp4      | 视频格式:H264/MPEG2/MPEG4/H263 音频格式:AAC/MP3 | 主流分辨率,如1080P/720P/480P/270P |
|     mkv      | 视频格式:H264/MPEG2/MPEG4/H263 音频格式:AAC/MP3 | 主流分辨率,如1080P/720P/480P/270P |
|      ts      |   视频格式:H264/MPEG2/MPEG4 音频格式:AAC/MP3    | 主流分辨率,如1080P/720P/480P/270P |
|     webm     |          视频格式:VP8 音频格式:VORBIS           | 主流分辨率,如1080P/720P/480P/270P |

W
wusongqing 已提交
36
## 开发步骤
37

W
wusongqing 已提交
38
详细API含义可参考:[js-apis-media.md](../reference/apis/js-apis-media.md)
39

W
wusongqing 已提交
40
### 全流程场景
41

W
wusongqing 已提交
42
包含流程:创建实例,设置url,设置SurfaceId,准备播放视频,播放视频,暂停播放,获取轨道信息,跳转播放位置,设置音量,设置倍速,结束播放,重置,释放资源等流程。
43

W
wusongqing 已提交
44
VideoPlayer支持的url媒体源输入类型可参考:[url属性说明](../reference/apis/js-apis-media.md#videoplayer_属性)
45

46 47
Xcomponent创建方法可参考:[Xcomponent创建方法](#Xcomponent创建方法)

48
```js
49 50 51
import media from '@ohos.multimedia.media'
import fileIO from '@ohos.fileio'

W
wusongqing 已提交
52 53
let videoPlayer = undefined; // 用于保存createVideoPlayer创建的对象
let surfaceID = undefined; // 用于保存Xcomponent接口返回的surfaceID
54

55 56 57 58 59 60
// 调用Xcomponent的接口用于获取surfaceID,并保存在surfaceID变量中,该接口由XComponent组件默认加载,非主动调用
LoadXcomponent() {
	surfaceID = this.$element('Xcomponent').getXComponentSurfaceId();
    console.info('LoadXcomponent surfaceID is' + surfaceID);
}

W
wusongqing 已提交
61
// 函数调用发生错误时用于上报错误信息
62 63 64 65 66 67
function failureCallback(error) {
    console.info(`error happened,error Name is ${error.name}`);
    console.info(`error happened,error Code is ${error.code}`);
    console.info(`error happened,error Message is ${error.message}`);
}

W
wusongqing 已提交
68
// 当函数调用发生异常时用于上报错误信息
69 70 71 72 73 74
function catchCallback(error) {
    console.info(`catch error happened,error Name is ${error.name}`);
    console.info(`catch error happened,error Code is ${error.code}`);
    console.info(`catch error happened,error Message is ${error.message}`);
}

W
wusongqing 已提交
75
// 用于打印视频轨道信息
76 77 78 79 80 81 82 83
function printfDescription(obj) {
    for (let item in obj) {
        let property = obj[item];
        console.info('key is ' + item);
        console.info('value is ' + property);
    }
}

W
wusongqing 已提交
84
// 调用createVideoPlayer接口返回videoPlayer实例对象
85 86 87 88 89 90 91 92 93
await media.createVideoPlayer().then((video) => {
    if (typeof (video) != 'undefined') {
        console.info('createVideoPlayer success!');
        videoPlayer = video;
    } else {
        console.info('createVideoPlayer fail!');
    }
}, failureCallback).catch(catchCallback);

94 95 96 97 98 99 100 101 102 103 104
// 用户选择视频设置fd(本地播放)
let fdPath = 'fd://'
let path = 'data/accounts/account_0/appdata/ohos.xxx.xxx.xxx/01.mp4';
await fileIO.open(path).then(fdNumber) => {
   fdPath = fdPath + '' + fdNumber;
   console.info('open fd sucess fd is' + fdPath);
}, (err) => {
   console.info('open fd failed err is' + err);
}),catch((err) => {
   console.info('open fd failed err is' + err);
});
W
wusongqing 已提交
105

106
videoPlayer.url = fdPath;
W
wusongqing 已提交
107 108

// 设置surfaceID用于显示视频画面
109 110 111 112
await videoPlayer.setDisplaySurface(surfaceID).then(() => {
    console.info('setDisplaySurface success');
}, failureCallback).catch(catchCallback);

W
wusongqing 已提交
113
// 调用prepare完成播放前准备工作
114 115 116 117
await videoPlayer.prepare().then(() => {
    console.info('prepare success');
}, failureCallback).catch(catchCallback);

W
wusongqing 已提交
118
// 调用play接口正式开始播放
119 120 121 122
await videoPlayer.play().then(() => {
    console.info('play success');
}, failureCallback).catch(catchCallback);

W
wusongqing 已提交
123
// 暂停播放
124 125 126 127
await videoPlayer.pause().then(() => {
    console.info('pause success');
}, failureCallback).catch(catchCallback);

W
wusongqing 已提交
128
// 通过promise回调方式获取视频轨道信息
129 130 131 132 133 134 135 136 137 138 139 140 141
let arrayDescription;
await videoPlayer.getTrackDescription().then((arrlist) => {
    if (typeof (arrlist) != 'undefined') {
        arrayDescription = arrlist;
    } else {
        console.log('video getTrackDescription fail');
    }
}, failureCallback).catch(catchCallback);

for (let i = 0; i < arrayDescription.length; i++) {
    printfDescription(arrayDescription[i]);
}

W
wusongqing 已提交
142
// 跳转播放时间到50s位置,具体入参意义请参考接口文档
143
let seekTime = 50000;
144
await videoPlayer.seek(seekTime, media.SeekMode._NEXT_SYNC).then((seekDoneTime) => {
145 146 147
    console.info('seek success');
}, failureCallback).catch(catchCallback);

W
wusongqing 已提交
148
// 音量设置接口,具体入参意义请参考接口文档
149 150 151 152 153
let volume = 0.5;
await videoPlayer.setVolume(volume).then(() => {
    console.info('setVolume success');
}, failureCallback).catch(catchCallback);

W
wusongqing 已提交
154
// 倍速设置接口,具体入参意义请参考接口文档
155
let speed = media.PlaybackRateMode.SPEED_FORWARD_2_00_X;
156 157 158 159
await videoPlayer.setSpeed(speed).then(() => {
    console.info('setSpeed success');
}, failureCallback).catch(catchCallback);

W
wusongqing 已提交
160
// 结束播放
161 162 163 164
await videoPlayer.stop().then(() => {
    console.info('stop success');
}, failureCallback).catch(catchCallback);

W
wusongqing 已提交
165
// 重置播放配置
166 167 168 169
await videoPlayer.reset().then(() => {
    console.info('reset success');
}, failureCallback).catch(catchCallback);

W
wusongqing 已提交
170
// 释放播放资源
171 172 173 174
await videoPlayer.release().then(() => {
    console.info('release success');
}, failureCallback).catch(catchCallback);

W
wusongqing 已提交
175
// 相关对象置undefined
176 177 178 179
videoPlayer = undefined;
surfaceID = undefined;
```

W
wusongqing 已提交
180
### 正常播放场景
181 182

```js
183 184 185
import media from '@ohos.multimedia.media'
import fileIO from '@ohos.fileio'

W
wusongqing 已提交
186 187
let videoPlayer = undefined; // 用于保存createVideoPlayer创建的对象
let surfaceID = undefined; // 用于保存Xcomponent接口返回的surfaceID
188

189 190 191 192 193 194
// 调用Xcomponent的接口用于获取surfaceID,并保存在surfaceID变量中,该接口由XComponent组件默认加载,非主动调用
LoadXcomponent() {
	surfaceID = this.$element('Xcomponent').getXComponentSurfaceId();
    console.info('LoadXcomponent surfaceID is' + surfaceID);
}

W
wusongqing 已提交
195
// 函数调用发生错误时用于上报错误信息
196 197 198 199 200 201
function failureCallback(error) {
    console.info(`error happened,error Name is ${error.name}`);
    console.info(`error happened,error Code is ${error.code}`);
    console.info(`error happened,error Message is ${error.message}`);
}

W
wusongqing 已提交
202
// 当函数调用发生异常时用于上报错误信息
203 204 205 206 207 208
function catchCallback(error) {
    console.info(`catch error happened,error Name is ${error.name}`);
    console.info(`catch error happened,error Code is ${error.code}`);
    console.info(`catch error happened,error Message is ${error.message}`);
}

W
wusongqing 已提交
209
// 设置'playbackCompleted'事件回调,播放完成触发
210 211 212 213 214 215 216 217 218 219 220 221 222
function SetCallBack(videoPlayer) {
	videoPlayer.on('playbackCompleted', () => {
        console.info('video play finish');
        
        await videoPlayer.release().then(() => {
    		console.info('release success');
		}, failureCallback).catch(catchCallback);

		videoPlayer = undefined;
        surfaceID = undefined;
    });
}

W
wusongqing 已提交
223
// 调用createVideoPlayer接口返回videoPlayer实例对象
224 225 226 227 228 229 230 231 232
await media.createVideoPlayer().then((video) => {
    if (typeof (video) != 'undefined') {
        console.info('createVideoPlayer success!');
        videoPlayer = video;
    } else {
        console.info('createVideoPlayer fail!');
    }
}, failureCallback).catch(catchCallback);

W
wusongqing 已提交
233
// 设置事件回调
234 235
SetCallBack(videoPlayer);

236 237 238 239 240 241 242 243 244 245 246
// 用户选择视频设置fd(本地播放)
let fdPath = 'fd://'
let path = 'data/accounts/account_0/appdata/ohos.xxx.xxx.xxx/01.mp4';
await fileIO.open(path).then(fdNumber) => {
   fdPath = fdPath + '' + fdNumber;
   console.info('open fd sucess fd is' + fdPath);
}, (err) => {
   console.info('open fd failed err is' + err);
}),catch((err) => {
   console.info('open fd failed err is' + err);
});
W
wusongqing 已提交
247

248
videoPlayer.url = fdPath;
W
wusongqing 已提交
249 250

// 设置surfaceID用于显示视频画面
251 252 253 254
await videoPlayer.setDisplaySurface(surfaceID).then(() => {
    console.info('setDisplaySurface success');
}, failureCallback).catch(catchCallback);

W
wusongqing 已提交
255
// 调用prepare完成播放前准备工作
256 257 258 259
await videoPlayer.prepare().then(() => {
    console.info('prepare success');
}, failureCallback).catch(catchCallback);

W
wusongqing 已提交
260
// 调用play接口正式开始播放
261 262 263 264 265
await videoPlayer.play().then(() => {
    console.info('play success');
}, failureCallback).catch(catchCallback);
```

W
wusongqing 已提交
266
### 切视频场景
267 268

```js
269 270 271
import media from '@ohos.multimedia.media'
import fileIO from '@ohos.fileio'

W
wusongqing 已提交
272 273
let videoPlayer = undefined; // 用于保存createVideoPlayer创建的对象
let surfaceID = undefined; // 用于保存Xcomponent接口返回的surfaceID
274

275 276 277 278 279 280
// 调用Xcomponent的接口用于获取surfaceID,并保存在surfaceID变量中,该接口由XComponent组件默认加载,非主动调用
LoadXcomponent() {
	surfaceID = this.$element('Xcomponent').getXComponentSurfaceId();
    console.info('LoadXcomponent surfaceID is' + surfaceID);
}

W
wusongqing 已提交
281
// 函数调用发生错误时用于上报错误信息
282 283 284 285 286 287
function failureCallback(error) {
    console.info(`error happened,error Name is ${error.name}`);
    console.info(`error happened,error Code is ${error.code}`);
    console.info(`error happened,error Message is ${error.message}`);
}

W
wusongqing 已提交
288
// 当函数调用发生异常时用于上报错误信息
289 290 291 292 293 294
function catchCallback(error) {
    console.info(`catch error happened,error Name is ${error.name}`);
    console.info(`catch error happened,error Code is ${error.code}`);
    console.info(`catch error happened,error Message is ${error.message}`);
}

W
wusongqing 已提交
295
// 设置'playbackCompleted'事件回调,播放完成触发
296 297 298 299 300 301 302 303 304 305 306 307 308
function SetCallBack(videoPlayer) {
	videoPlayer.on('playbackCompleted', () => {
        console.info('video play finish');
        
        await videoPlayer.release().then(() => {
    		console.info('release success');
		}, failureCallback).catch(catchCallback);

		videoPlayer = undefined;
        surfaceID = undefined;
    });
}

W
wusongqing 已提交
309
// 调用createVideoPlayer接口返回videoPlayer实例对象
310 311 312 313 314 315 316 317 318
await media.createVideoPlayer().then((video) => {
    if (typeof (video) != 'undefined') {
        console.info('createVideoPlayer success!');
        videoPlayer = video;
    } else {
        console.info('createVideoPlayer fail!');
    }
}, failureCallback).catch(catchCallback);

W
wusongqing 已提交
319
// 设置事件回调
320 321
SetCallBack(videoPlayer);

322 323 324 325 326 327 328 329 330 331 332
// 用户选择视频设置fd(本地播放)
let fdPath = 'fd://'
let path = 'data/accounts/account_0/appdata/ohos.xxx.xxx.xxx/01.mp4';
await fileIO.open(path).then(fdNumber) => {
   fdPath = fdPath + '' + fdNumber;
   console.info('open fd sucess fd is' + fdPath);
}, (err) => {
   console.info('open fd failed err is' + err);
}),catch((err) => {
   console.info('open fd failed err is' + err);
});
W
wusongqing 已提交
333

334
videoPlayer.url = fdPath;
W
wusongqing 已提交
335 336

// 设置surfaceID用于显示视频画面
337 338 339 340
await videoPlayer.setDisplaySurface(surfaceID).then(() => {
    console.info('setDisplaySurface success');
}, failureCallback).catch(catchCallback);

W
wusongqing 已提交
341
// 调用prepare完成播放前准备工作
342 343 344 345
await videoPlayer.prepare().then(() => {
    console.info('prepare success');
}, failureCallback).catch(catchCallback);

W
wusongqing 已提交
346
// 调用play接口正式开始播放
347 348 349 350
await videoPlayer.play().then(() => {
    console.info('play success');
}, failureCallback).catch(catchCallback);

W
wusongqing 已提交
351 352
// 播放一段时间后,下发切视频指令
// 重置播放配置
353 354 355 356
await videoPlayer.reset().then(() => {
    console.info('reset success');
}, failureCallback).catch(catchCallback);

357 358 359 360 361 362 363 364 365 366 367 368 369
// 用户选择视频设置fd(本地播放)
let fdNextPath = 'fd://'
let nextPath = 'data/accounts/account_0/appdata/ohos.xxx.xxx.xxx/02.mp4';
await fileIO.open(nextPath).then(fdNumber) => {
   fdNextPath = fdNextPath + '' + fdNumber;
   console.info('open fd sucess fd is' + fdNextPath);
}, (err) => {
   console.info('open fd failed err is' + err);
}),catch((err) => {
   console.info('open fd failed err is' + err);
});

videoPlayer.url = fdNextPath;
W
wusongqing 已提交
370 371

// 设置surfaceID用于显示视频画面
372 373 374 375
await videoPlayer.setDisplaySurface(surfaceID).then(() => {
    console.info('setDisplaySurface success');
}, failureCallback).catch(catchCallback);

W
wusongqing 已提交
376
// 调用prepare完成播放前准备工作
377 378 379 380
await videoPlayer.prepare().then(() => {
    console.info('prepare success');
}, failureCallback).catch(catchCallback);

W
wusongqing 已提交
381
// 调用play接口正式开始播放
382 383 384 385 386
await videoPlayer.play().then(() => {
    console.info('play success');
}, failureCallback).catch(catchCallback);
```

W
wusongqing 已提交
387
### 单个视频循环场景
388 389

```js
390 391 392
import media from '@ohos.multimedia.media'
import fileIO from '@ohos.fileio'

W
wusongqing 已提交
393 394
let videoPlayer = undefined; // 用于保存createVideoPlayer创建的对象
let surfaceID = undefined; // 用于保存Xcomponent接口返回的surfaceID
395

396 397 398 399 400 401
// 调用Xcomponent的接口用于获取surfaceID,并保存在surfaceID变量中,该接口由XComponent组件默认加载,非主动调用
LoadXcomponent() {
	surfaceID = this.$element('Xcomponent').getXComponentSurfaceId();
    console.info('LoadXcomponent surfaceID is' + surfaceID);
}

W
wusongqing 已提交
402
// 函数调用发生错误时用于上报错误信息
403 404 405 406 407 408
function failureCallback(error) {
    console.info(`error happened,error Name is ${error.name}`);
    console.info(`error happened,error Code is ${error.code}`);
    console.info(`error happened,error Message is ${error.message}`);
}

W
wusongqing 已提交
409
// 当函数调用发生异常时用于上报错误信息
410 411 412 413 414 415
function catchCallback(error) {
    console.info(`catch error happened,error Name is ${error.name}`);
    console.info(`catch error happened,error Code is ${error.code}`);
    console.info(`catch error happened,error Message is ${error.message}`);
}

W
wusongqing 已提交
416
// 设置'playbackCompleted'事件回调,播放完成触发
417 418 419 420 421 422 423 424 425 426 427 428 429
function SetCallBack(videoPlayer) {
	videoPlayer.on('playbackCompleted', () => {
        console.info('video play finish');
        
        await videoPlayer.release().then(() => {
    		console.info('release success');
		}, failureCallback).catch(catchCallback);

		videoPlayer = undefined;
        surfaceID = undefined;
    });
}

W
wusongqing 已提交
430
// 调用createVideoPlayer接口返回videoPlayer实例对象
431 432 433 434 435 436 437 438 439
await media.createVideoPlayer().then((video) => {
    if (typeof (video) != 'undefined') {
        console.info('createVideoPlayer success!');
        videoPlayer = video;
    } else {
        console.info('createVideoPlayer fail!');
    }
}, failureCallback).catch(catchCallback);

W
wusongqing 已提交
440
// 设置事件回调
441 442
SetCallBack(videoPlayer);

443 444 445 446 447 448 449 450 451 452 453
// 用户选择视频设置fd(本地播放)
let fdPath = 'fd://'
let path = 'data/accounts/account_0/appdata/ohos.xxx.xxx.xxx/01.mp4';
await fileIO.open(path).then(fdNumber) => {
   fdPath = fdPath + '' + fdNumber;
   console.info('open fd sucess fd is' + fdPath);
}, (err) => {
   console.info('open fd failed err is' + err);
}),catch((err) => {
   console.info('open fd failed err is' + err);
});
W
wusongqing 已提交
454

455
videoPlayer.url = fdPath;
W
wusongqing 已提交
456 457

// 设置surfaceID用于显示视频画面
458 459 460 461
await videoPlayer.setDisplaySurface(surfaceID).then(() => {
    console.info('setDisplaySurface success');
}, failureCallback).catch(catchCallback);

W
wusongqing 已提交
462
// 调用prepare完成播放前准备工作
463 464 465 466
await videoPlayer.prepare().then(() => {
    console.info('prepare success');
}, failureCallback).catch(catchCallback);

W
wusongqing 已提交
467
// 设置循环播放属性
468 469
videoPlayer.loop = true;

W
wusongqing 已提交
470
// 调用play接口正式开始播放
471 472 473
await videoPlayer.play().then(() => {
    console.info('play success');
}, failureCallback).catch(catchCallback);
474 475 476 477
```

### Xcomponent创建方法

Z
zengyawen 已提交
478
播放视频中获取surfaceID依赖了Xcomponent,需要创建一个和xxx.js同名的xxx.hml文件,xxx.hml里面需要添加如下代码:
479 480 481

```js
<xcomponent id = 'Xcomponent'
Z
zengyawen 已提交
482
      if = "{{isFlush}}" // 刷新surfaceID,isFlush赋值false再赋值true为一次刷新,会主动再次加载LoadXcomponent获取新的surfaceID
483
      type = 'surface'
Z
zengyawen 已提交
484 485
      onload = 'LoadXcomponent' // 默认加载接口
      style = "width:720px;height:480px;border-color:red;border-width:5px;"> // 设置窗口宽高等属性
486 487
</xcomponent>
```