Call my.createVideoContext to obtain a video context object for controlling video playback.
my.createVideoContext
Accepts a video ID and returns a videoContext object. The video ID is the custom ID property assigned to the corresponding video tag.
Use the videoContext object to control a video component.
Version requirements: Supported on base libraries 1.14.1 and later.
Code sample
In the .axml file, assign a custom ID to the video tag. In the following example, the ID is myVideo.
<view>
<!-- onPlay is of the EventHandle type. It triggers the play event when playback starts or resumes. -->
<video id="myVideo" src="{{src}}" onPlay="{{onPlay}}" enableNative="{{true}}"></video>
<button type="default" size="defaultSize" onTap="play"> Play </button>
<button type="default" size="defaultSize" onTap="pause"> Pause </button>
<button type="default" size="defaultSize" onTap="stop"> stop </button>
<button type="default" size="defaultSize" onTap="seek"> seek </button>
<button type="default" size="defaultSize" onTap="requestFullScreen"> requestFullScreen </button>
<button type="default" size="defaultSize" onTap="exitFullScreen"> exitFullScreen </button>
<button type="default" size="defaultSize" onTap="mute"> mute </button>
</view>
In the .js file, add the following code:
Page({
data: {
// src is the resource address of the video to be played. src supports apFilePath, for example, https://resource/xxx.video.
src: "http://flv.bn.netease.com/tvmrepo/2012/7/C/7/E868IGRC7-mobile.mp4",
},
onLoad() {
this.videoContext = my.createVideoContext('myVideo');
},
play() {
this.videoContext.play();
},
pause() {
this.videoContext.pause();
},
stop() {
this.videoContext.stop();
},
seek() {
this.videoContext.seek(100);
},
requestFullScreen() {
this.videoContext.requestFullScreen({
direction: 0
});
},
exitFullScreen() {
this.videoContext.exitFullScreen();
},
mute() {
this.videoContext.mute(false);
},
});
videoContext methods
|
Method |
Parameter |
Type |
Description |
|
play |
None |
- |
Plays the video. |
|
pause |
None |
- |
Pauses the video. |
|
stop |
None |
- |
Stops the video. |
|
seek |
position |
Number |
Seeks to a specified position. The unit is seconds (s). |
|
requestFullScreen |
direction |
Number |
Enters full-screen mode.
|
|
exitFullScreen |
None |
- |
Exits full-screen mode. |
|
showStatusBar |
None |
- |
Shows the status bar. Valid only in full-screen mode on iOS. |
|
hideStatusBar |
None |
- |
Hides the status bar. Valid only in full-screen mode on iOS. |
|
mute |
ison |
Boolean |
Toggles the mute state. |
|
playbackRate |
rate |
Number |
Sets the playback speed (0.5 ≤ rate ≤ 2.0). mPaaS does not support this method. |
To use full-screen mode on iOS, select Landscape Left and Landscape Right in Xcode > General > Deployment Info, as shown in the following figure.
