video-playback.md 17.0 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

Z
zengyawen 已提交
38
详细API含义可参考:[媒体服务API文档VideoPlayer](../reference/apis/js-apis-media.md)
39

40 41 42 43
*注意:按如下操作步骤就能正常播放视频,其中设置Surface需要在设置url和Prepare之间

![zh-ch_image_video_state](figures/zh-ch_image_video_state.png)

W
wusongqing 已提交
44
### 全流程场景
45

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

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

50 51
Xcomponent创建方法可参考:[Xcomponent创建方法](#Xcomponent创建方法)

52
```js
53 54 55
import media from '@ohos.multimedia.media'
import fileIO from '@ohos.fileio'

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

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

W
wusongqing 已提交
65
// 函数调用发生错误时用于上报错误信息
66 67 68 69 70 71
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 已提交
72
// 当函数调用发生异常时用于上报错误信息
73 74 75 76 77 78
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 已提交
79
// 用于打印视频轨道信息
80 81 82 83 84 85 86 87
function printfDescription(obj) {
    for (let item in obj) {
        let property = obj[item];
        console.info('key is ' + item);
        console.info('value is ' + property);
    }
}

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

98 99 100 101 102 103 104 105 106 107 108
// 用户选择视频设置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 已提交
109

110
videoPlayer.url = fdPath;
W
wusongqing 已提交
111 112

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

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

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

W
wusongqing 已提交
127
// 暂停播放
128 129 130 131
await videoPlayer.pause().then(() => {
    console.info('pause success');
}, failureCallback).catch(catchCallback);

W
wusongqing 已提交
132
// 通过promise回调方式获取视频轨道信息
133 134 135 136 137 138 139 140 141 142 143 144 145
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 已提交
146
// 跳转播放时间到50s位置,具体入参意义请参考接口文档
147
let seekTime = 50000;
148
await videoPlayer.seek(seekTime, media.SeekMode._NEXT_SYNC).then((seekDoneTime) => {
149 150 151
    console.info('seek success');
}, failureCallback).catch(catchCallback);

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

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

W
wusongqing 已提交
164
// 结束播放
165 166 167 168
await videoPlayer.stop().then(() => {
    console.info('stop success');
}, failureCallback).catch(catchCallback);

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

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

W
wusongqing 已提交
179
// 相关对象置undefined
180 181 182 183
videoPlayer = undefined;
surfaceID = undefined;
```

W
wusongqing 已提交
184
### 正常播放场景
185 186

```js
187 188 189
import media from '@ohos.multimedia.media'
import fileIO from '@ohos.fileio'

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

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

W
wusongqing 已提交
199
// 函数调用发生错误时用于上报错误信息
200 201 202 203 204 205
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 已提交
206
// 当函数调用发生异常时用于上报错误信息
207 208 209 210 211 212
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 已提交
213
// 设置'playbackCompleted'事件回调,播放完成触发
214 215 216 217 218 219 220 221 222 223 224 225 226
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 已提交
227
// 调用createVideoPlayer接口返回videoPlayer实例对象
228 229 230 231 232 233 234 235 236
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 已提交
237
// 设置事件回调
238 239
SetCallBack(videoPlayer);

240 241 242 243 244 245 246 247 248 249 250
// 用户选择视频设置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 已提交
251

252
videoPlayer.url = fdPath;
W
wusongqing 已提交
253 254

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

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

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

W
wusongqing 已提交
270
### 切视频场景
271 272

```js
273 274 275
import media from '@ohos.multimedia.media'
import fileIO from '@ohos.fileio'

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

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

W
wusongqing 已提交
285
// 函数调用发生错误时用于上报错误信息
286 287 288 289 290 291
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 已提交
292
// 当函数调用发生异常时用于上报错误信息
293 294 295 296 297 298
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 已提交
299
// 设置'playbackCompleted'事件回调,播放完成触发
300 301 302 303 304 305 306 307 308 309 310 311 312
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 已提交
313
// 调用createVideoPlayer接口返回videoPlayer实例对象
314 315 316 317 318 319 320 321 322
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 已提交
323
// 设置事件回调
324 325
SetCallBack(videoPlayer);

326 327 328 329 330 331 332 333 334 335 336
// 用户选择视频设置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 已提交
337

338
videoPlayer.url = fdPath;
W
wusongqing 已提交
339 340

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

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

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

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

361 362 363 364 365 366 367 368 369 370 371 372 373
// 用户选择视频设置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 已提交
374 375

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

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

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

W
wusongqing 已提交
391
### 单个视频循环场景
392 393

```js
394 395 396
import media from '@ohos.multimedia.media'
import fileIO from '@ohos.fileio'

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

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

W
wusongqing 已提交
406
// 函数调用发生错误时用于上报错误信息
407 408 409 410 411 412
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 已提交
413
// 当函数调用发生异常时用于上报错误信息
414 415 416 417 418 419
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 已提交
420
// 设置'playbackCompleted'事件回调,播放完成触发
421 422 423 424 425 426 427 428 429 430 431 432 433
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 已提交
434
// 调用createVideoPlayer接口返回videoPlayer实例对象
435 436 437 438 439 440 441 442 443
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 已提交
444
// 设置事件回调
445 446
SetCallBack(videoPlayer);

447 448 449 450 451 452 453 454 455 456 457
// 用户选择视频设置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 已提交
458

459
videoPlayer.url = fdPath;
W
wusongqing 已提交
460 461

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

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

W
wusongqing 已提交
471
// 设置循环播放属性
472 473
videoPlayer.loop = true;

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

### Xcomponent创建方法

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

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