1# Video Playback Development 2 3## When to Use 4 5You can use video playback APIs to convert video data into visible signals, play the signals using output devices, and manage playback tasks. This document describes development for the following video playback scenarios: full-process, normal playback, video switching, and loop playback. 6 7**Figure 1** Video playback state transition 8 9 10 11 12 13**Figure 2** Layer 0 diagram of video playback 14 15 16 17Note: Video playback requires hardware capabilities such as display, audio, and codec. 18 191. A third-party application obtains a surface ID from the XComponent. 202. The third-party application transfers the surface ID to the VideoPlayer JS. 213. The media service flushes the frame data to the surface buffer. 22 23## Compatibility 24 25Use the mainstream playback formats and resolutions, rather than custom ones to avoid playback failures, frame freezing, and artifacts. The system is not affected by incompatibility issues. If such an issue occurs, you can exit stream playback mode. 26 27The table below lists the mainstream playback formats and resolutions. 28 29| Video Container Format| Description | Resolution | 30| :----------: | :-----------------------------------------------: | :--------------------------------: | 31| mp4 | Video format: H.264/MPEG-2/MPEG-4/H.263; audio format: AAC/MP3| Mainstream resolutions, such as 1080p, 720p, 480p, and 270p| 32| mkv | Video format: H.264/MPEG-2/MPEG-4/H.263; audio format: AAC/MP3| Mainstream resolutions, such as 1080p, 720p, 480p, and 270p| 33| ts | Video format: H.264/MPEG-2/MPEG-4; audio format: AAC/MP3 | Mainstream resolutions, such as 1080p, 720p, 480p, and 270p| 34| webm | Video format: VP8; audio format: VORBIS | Mainstream resolutions, such as 1080p, 720p, 480p, and 270p| 35 36## How to Develop 37 38For details about the APIs, see [VideoPlayer in the Media API](../reference/apis/js-apis-media.md). 39 40### Full-Process Scenario 41 42The full video playback process includes creating an instance, setting the URL, setting the surface ID, preparing for video playback, playing video, pausing playback, obtaining track information, seeking to a playback position, setting the volume, setting the playback speed, stopping playback, resetting the playback configuration, and releasing resources. 43 44For details about the **url** types supported by **VideoPlayer**, see the [url attribute](../reference/apis/js-apis-media.md#videoplayer_attributes). 45 46For details about how to create an XComponent, see [XComponent]( ../reference/arkui-ts/ts-basic-components-xcomponent.md). 47 48*Note: **setSurface** must be called after the URL is set but before **prepare** is called. 49 50```js 51import media from '@ohos.multimedia.media' 52import fileIO from '@ohos.fileio' 53export class VideoPlayerDemo { 54 // Report an error in the case of a function invocation failure. 55 failureCallback(error) { 56 console.info(`error happened,error Name is ${error.name}`); 57 console.info(`error happened,error Code is ${error.code}`); 58 console.info(`error happened,error Message is ${error.message}`); 59 } 60 61 // Report an error in the case of a function invocation exception. 62 catchCallback(error) { 63 console.info(`catch error happened,error Name is ${error.name}`); 64 console.info(`catch error happened,error Code is ${error.code}`); 65 console.info(`catch error happened,error Message is ${error.message}`); 66 } 67 68 // Used to print the video track information. 69 printfDescription(obj) { 70 for (let item in obj) { 71 let property = obj[item]; 72 console.info('key is ' + item); 73 console.info('value is ' + property); 74 } 75 } 76 77 async videoPlayerDemo() { 78 let videoPlayer = undefined; 79 let surfaceID = 'test' // The surfaceID parameter is used for screen display. Its value is obtained through the XComponent API. For details about the document link, see the method of creating the XComponent. 80 let fdPath = 'fd://' 81 // The stream in the path can be pushed to the device by running the "hdc file send D:\xxx\H264_AAC.mp4 /data/app/el1/bundle/public/ohos.acts.multimedia.video.videoplayer/ohos.acts.multimedia.video.videoplayer/assets/entry/resources/rawfile" command. 82 let path = '/data/app/el1/bundle/public/ohos.acts.multimedia.video.videoplayer/ohos.acts.multimedia.video.videoplayer/assets/entry/resources/rawfile/H264_AAC.mp4'; 83 await fileIO.open(path).then((fdNumber) => { 84 fdPath = fdPath + '' + fdNumber; 85 console.info('open fd success fd is' + fdPath); 86 }, (err) => { 87 console.info('open fd failed err is' + err); 88 }).catch((err) => { 89 console.info('open fd failed err is' + err); 90 }); 91 // Call createVideoPlayer to create a VideoPlayer instance. 92 await media.createVideoPlayer().then((video) => { 93 if (typeof (video) != 'undefined') { 94 console.info('createVideoPlayer success!'); 95 videoPlayer = video; 96 } else { 97 console.info('createVideoPlayer fail!'); 98 } 99 }, this.failureCallback).catch(this.catchCallback); 100 // Set the playback source for the player. 101 videoPlayer.url = fdPath; 102 103 // Set the surface ID to display the video image. 104 await videoPlayer.setDisplaySurface(surfaceID).then(() => { 105 console.info('setDisplaySurface success'); 106 }, this.failureCallback).catch(this.catchCallback); 107 108 // Call the prepare API to prepare for playback. 109 await videoPlayer.prepare().then(() => { 110 console.info('prepare success'); 111 }, this.failureCallback).catch(this.catchCallback); 112 113 // Call the play API to start playback. 114 await videoPlayer.play().then(() => { 115 console.info('play success'); 116 }, this.failureCallback).catch(this.catchCallback); 117 118 // Pause playback. 119 await videoPlayer.pause().then(() => { 120 console.info('pause success'); 121 }, this.failureCallback).catch(this.catchCallback); 122 123 // Use a promise to obtain the video track information. 124 let arrayDescription; 125 await videoPlayer.getTrackDescription().then((arrlist) => { 126 if (typeof (arrlist) != 'undefined') { 127 arrayDescription = arrlist; 128 } else { 129 console.log('video getTrackDescription fail'); 130 } 131 }, this.failureCallback).catch(this.catchCallback); 132 133 for (let i = 0; i < arrayDescription.length; i++) { 134 this.printfDescription(arrayDescription[i]); 135 } 136 137 // Seek to the 50s position. For details about the input parameters, see the API document. 138 let seekTime = 50000; 139 await videoPlayer.seek(seekTime, media.SeekMode.SEEK_NEXT_SYNC).then((seekDoneTime) => { 140 console.info('seek success'); 141 }, this.failureCallback).catch(this.catchCallback); 142 143 // Set the volume. For details about the input parameters, see the API document. 144 let volume = 0.5; 145 await videoPlayer.setVolume(volume).then(() => { 146 console.info('setVolume success'); 147 }, this.failureCallback).catch(this.catchCallback); 148 149 // Set the playback speed. For details about the input parameters, see the API document. 150 let speed = media.PlaybackSpeed.SPEED_FORWARD_2_00_X; 151 await videoPlayer.setSpeed(speed).then(() => { 152 console.info('setSpeed success'); 153 }, this.failureCallback).catch(this.catchCallback); 154 155 // Stop playback. 156 await videoPlayer.stop().then(() => { 157 console.info('stop success'); 158 }, this.failureCallback).catch(this.catchCallback); 159 160 // Reset the playback configuration. 161 await videoPlayer.reset().then(() => { 162 console.info('reset success'); 163 }, this.failureCallback).catch(this.catchCallback); 164 165 // Release playback resources. 166 await videoPlayer.release().then(() => { 167 console.info('release success'); 168 }, this.failureCallback).catch(this.catchCallback); 169 170 // Set the related instances to undefined. 171 videoPlayer = undefined; 172 surfaceID = undefined; 173 } 174} 175``` 176 177### Normal Playback Scenario 178 179```js 180import media from '@ohos.multimedia.media' 181import fileIO from '@ohos.fileio' 182export class VideoPlayerDemo { 183 // Report an error in the case of a function invocation failure. 184 failureCallback(error) { 185 console.info(`error happened,error Name is ${error.name}`); 186 console.info(`error happened,error Code is ${error.code}`); 187 console.info(`error happened,error Message is ${error.message}`); 188 } 189 190 // Report an error in the case of a function invocation exception. 191 catchCallback(error) { 192 console.info(`catch error happened,error Name is ${error.name}`); 193 console.info(`catch error happened,error Code is ${error.code}`); 194 console.info(`catch error happened,error Message is ${error.message}`); 195 } 196 197 // Used to print the video track information. 198 printfDescription(obj) { 199 for (let item in obj) { 200 let property = obj[item]; 201 console.info('key is ' + item); 202 console.info('value is ' + property); 203 } 204 } 205 206 async videoPlayerDemo() { 207 let videoPlayer = undefined; 208 let surfaceID = 'test' // The surfaceID parameter is used for screen display. Its value is obtained through the XComponent API. For details about the document link, see the method of creating the XComponent. 209 let fdPath = 'fd://' 210 // The stream in the path can be pushed to the device by running the "hdc file send D:\xxx\H264_AAC.mp4 /data/app/el1/bundle/public/ohos.acts.multimedia.video.videoplayer/ohos.acts.multimedia.video.videoplayer/assets/entry/resources/rawfile" command. 211 let path = '/data/app/el1/bundle/public/ohos.acts.multimedia.video.videoplayer/ohos.acts.multimedia.video.videoplayer/assets/entry/resources/rawfile/H264_AAC.mp4'; 212 await fileIO.open(path).then((fdNumber) => { 213 fdPath = fdPath + '' + fdNumber; 214 console.info('open fd success fd is' + fdPath); 215 }, (err) => { 216 console.info('open fd failed err is' + err); 217 }).catch((err) => { 218 console.info('open fd failed err is' + err); 219 }); 220 // Call createVideoPlayer to create a VideoPlayer instance. 221 await media.createVideoPlayer().then((video) => { 222 if (typeof (video) != 'undefined') { 223 console.info('createVideoPlayer success!'); 224 videoPlayer = video; 225 } else { 226 console.info('createVideoPlayer fail!'); 227 } 228 }, this.failureCallback).catch(this.catchCallback); 229 // Set the playback source for the player. 230 videoPlayer.url = fdPath; 231 232 // Set the surface ID to display the video image. 233 await videoPlayer.setDisplaySurface(surfaceID).then(() => { 234 console.info('setDisplaySurface success'); 235 }, this.failureCallback).catch(this.catchCallback); 236 237 // Call the prepare API to prepare for playback. 238 await videoPlayer.prepare().then(() => { 239 console.info('prepare success'); 240 }, this.failureCallback).catch(this.catchCallback); 241 242 // Call the play API to start playback. 243 await videoPlayer.play().then(() => { 244 console.info('play success'); 245 }, this.failureCallback).catch(this.catchCallback); 246 247 // Stop playback. 248 await videoPlayer.stop().then(() => { 249 console.info('stop success'); 250 }, this.failureCallback).catch(this.catchCallback); 251 252 // Release playback resources. 253 await videoPlayer.release().then(() => { 254 console.info('release success'); 255 }, this.failureCallback).catch(this.catchCallback); 256 257 // Set the related instances to undefined. 258 videoPlayer = undefined; 259 surfaceID = undefined; 260 } 261} 262``` 263 264### Switching to the Next Video Clip 265 266```js 267import media from '@ohos.multimedia.media' 268import fileIO from '@ohos.fileio' 269export class VideoPlayerDemo { 270 // Report an error in the case of a function invocation failure. 271 failureCallback(error) { 272 console.info(`error happened,error Name is ${error.name}`); 273 console.info(`error happened,error Code is ${error.code}`); 274 console.info(`error happened,error Message is ${error.message}`); 275 } 276 277 // Report an error in the case of a function invocation exception. 278 catchCallback(error) { 279 console.info(`catch error happened,error Name is ${error.name}`); 280 console.info(`catch error happened,error Code is ${error.code}`); 281 console.info(`catch error happened,error Message is ${error.message}`); 282 } 283 284 // Used to print the video track information. 285 printfDescription(obj) { 286 for (let item in obj) { 287 let property = obj[item]; 288 console.info('key is ' + item); 289 console.info('value is ' + property); 290 } 291 } 292 293 async videoPlayerDemo() { 294 let videoPlayer = undefined; 295 let surfaceID = 'test' // The surfaceID parameter is used for screen display. Its value is obtained through the XComponent API. For details about the document link, see the method of creating the XComponent. 296 let fdPath = 'fd://' 297 // The stream in the path can be pushed to the device by running the "hdc file send D:\xxx\H264_AAC.mp4 /data/app/el1/bundle/public/ohos.acts.multimedia.video.videoplayer/ohos.acts.multimedia.video.videoplayer/assets/entry/resources/rawfile" command. 298 let path = '/data/app/el1/bundle/public/ohos.acts.multimedia.video.videoplayer/ohos.acts.multimedia.video.videoplayer/assets/entry/resources/rawfile/H264_AAC.mp4'; 299 let nextPath = '/data/app/el1/bundle/public/ohos.acts.multimedia.video.videoplayer/ohos.acts.multimedia.video.videoplayer/assets/entry/resources/rawfile/MP4_AAC.mp4'; 300 await fileIO.open(path).then((fdNumber) => { 301 fdPath = fdPath + '' + fdNumber; 302 console.info('open fd success fd is' + fdPath); 303 }, (err) => { 304 console.info('open fd failed err is' + err); 305 }).catch((err) => { 306 console.info('open fd failed err is' + err); 307 }); 308 // Call createVideoPlayer to create a VideoPlayer instance. 309 await media.createVideoPlayer().then((video) => { 310 if (typeof (video) != 'undefined') { 311 console.info('createVideoPlayer success!'); 312 videoPlayer = video; 313 } else { 314 console.info('createVideoPlayer fail!'); 315 } 316 }, this.failureCallback).catch(this.catchCallback); 317 // Set the playback source for the player. 318 videoPlayer.url = fdPath; 319 320 // Set the surface ID to display the video image. 321 await videoPlayer.setDisplaySurface(surfaceID).then(() => { 322 console.info('setDisplaySurface success'); 323 }, this.failureCallback).catch(this.catchCallback); 324 325 // Call the prepare API to prepare for playback. 326 await videoPlayer.prepare().then(() => { 327 console.info('prepare success'); 328 }, this.failureCallback).catch(this.catchCallback); 329 330 // Call the play API to start playback. 331 await videoPlayer.play().then(() => { 332 console.info('play success'); 333 }, this.failureCallback).catch(this.catchCallback); 334 335 // Reset the playback configuration. 336 await videoPlayer.reset().then(() => { 337 console.info('reset success'); 338 }, this.failureCallback).catch(this.catchCallback); 339 340 // Obtain the next video FD address. 341 fdPath = 'fd://' 342 await fileIO.open(nextPath).then((fdNumber) => { 343 fdPath = fdPath + '' + fdNumber; 344 console.info('open fd success fd is' + fdPath); 345 }, (err) => { 346 console.info('open fd failed err is' + err); 347 }).catch((err) => { 348 console.info('open fd failed err is' + err); 349 }); 350 // Set the second video playback source. 351 videoPlayer.url = fdPath; 352 353 // Call the prepare API to prepare for playback. 354 await videoPlayer.prepare().then(() => { 355 console.info('prepare success'); 356 }, this.failureCallback).catch(this.catchCallback); 357 358 // Call the play API to start playback. 359 await videoPlayer.play().then(() => { 360 console.info('play success'); 361 }, this.failureCallback).catch(this.catchCallback); 362 363 // Release playback resources. 364 await videoPlayer.release().then(() => { 365 console.info('release success'); 366 }, this.failureCallback).catch(this.catchCallback); 367 368 // Set the related instances to undefined. 369 videoPlayer = undefined; 370 surfaceID = undefined; 371 } 372} 373``` 374 375### Looping a Video Clip 376 377```js 378import media from '@ohos.multimedia.media' 379import fileIO from '@ohos.fileio' 380export class VideoPlayerDemo { 381 // Report an error in the case of a function invocation failure. 382 failureCallback(error) { 383 console.info(`error happened,error Name is ${error.name}`); 384 console.info(`error happened,error Code is ${error.code}`); 385 console.info(`error happened,error Message is ${error.message}`); 386 } 387 388 // Report an error in the case of a function invocation exception. 389 catchCallback(error) { 390 console.info(`catch error happened,error Name is ${error.name}`); 391 console.info(`catch error happened,error Code is ${error.code}`); 392 console.info(`catch error happened,error Message is ${error.message}`); 393 } 394 395 // Used to print the video track information. 396 printfDescription(obj) { 397 for (let item in obj) { 398 let property = obj[item]; 399 console.info('key is ' + item); 400 console.info('value is ' + property); 401 } 402 } 403 404 async videoPlayerDemo() { 405 let videoPlayer = undefined; 406 let surfaceID = 'test' // The surfaceID parameter is used for screen display. Its value is obtained through the XComponent API. For details about the document link, see the method of creating the XComponent. 407 let fdPath = 'fd://' 408 // The stream in the path can be pushed to the device by running the "hdc file send D:\xxx\H264_AAC.mp4 /data/app/el1/bundle/public/ohos.acts.multimedia.video.videoplayer/ohos.acts.multimedia.video.videoplayer/assets/entry/resources/rawfile" command. 409 let path = '/data/app/el1/bundle/public/ohos.acts.multimedia.video.videoplayer/ohos.acts.multimedia.video.videoplayer/assets/entry/resources/rawfile/H264_AAC.mp4'; 410 await fileIO.open(path).then((fdNumber) => { 411 fdPath = fdPath + '' + fdNumber; 412 console.info('open fd success fd is' + fdPath); 413 }, (err) => { 414 console.info('open fd failed err is' + err); 415 }).catch((err) => { 416 console.info('open fd failed err is' + err); 417 }); 418 // Call createVideoPlayer to create a VideoPlayer instance. 419 await media.createVideoPlayer().then((video) => { 420 if (typeof (video) != 'undefined') { 421 console.info('createVideoPlayer success!'); 422 videoPlayer = video; 423 } else { 424 console.info('createVideoPlayer fail!'); 425 } 426 }, this.failureCallback).catch(this.catchCallback); 427 // Set the playback source for the player. 428 videoPlayer.url = fdPath; 429 430 // Set the surface ID to display the video image. 431 await videoPlayer.setDisplaySurface(surfaceID).then(() => { 432 console.info('setDisplaySurface success'); 433 }, this.failureCallback).catch(this.catchCallback); 434 435 // Call the prepare API to prepare for playback. 436 await videoPlayer.prepare().then(() => { 437 console.info('prepare success'); 438 }, this.failureCallback).catch(this.catchCallback); 439 // Set the loop playback attribute. 440 videoPlayer.loop = true; 441 // Call the play API to start loop playback. 442 await videoPlayer.play().then(() => { 443 console.info('play success'); 444 }, this.failureCallback).catch(this.catchCallback); 445 } 446} 447``` 448