Home  >  Article  >  Web Front-end  >  UniApp realizes the integration and usage skills of video playback and recording

UniApp realizes the integration and usage skills of video playback and recording

PHPz
PHPzOriginal
2023-07-04 11:07:393736browse

UniApp realizes the integration and usage skills of video playback and recording

UniApp is a cross-platform application development framework that can be used to develop WeChat applets, H5 sites, APPs and other platforms. Implementing video playback and recording in UniApp is one of the very practical functions. This article will introduce how to integrate and use video playback and recording techniques in UniApp, and provide corresponding code examples.

1. Video playback integration and use

To implement video playback in UniApp, you can use the uni-mp-video component. This component is encapsulated based on the video component of the WeChat applet and can be used in Used on multiple platforms. The following is a code example using the uni-mp-video component:

  1. Introduce the uni-mp-video component in the json file of the page
{
  "usingComponents": {
    "uni-mp-video": "@dcloudio/uni-mp-video/uni-mp-video"
  }
}
  1. In the page The uni-mp-video component is used in the wxml file
<uni-mp-video src="/path/to/video.mp4"></uni-mp-video>

where src is the address of the video. Playback of different videos can be achieved by setting the src attribute.

  1. Interactive control of video playback
<uni-mp-video src="/path/to/video.mp4"
              controls
              autoplay></uni-mp-video>

By setting the controls attribute, you can display interactive controls of the video, such as play buttons, progress bars, etc. Automatic playback of videos can be achieved by setting the autoplay attribute.

2. Video recording integration and use

Video recording in UniApp can be called using APIs such as uni.chooseVideo and uni.saveVideoToPhotosAlbum. The following is a code example using uni.chooseVideo and uni.saveVideoToPhotosAlbum:

  1. Interactive control of video recording
<button bindtap="chooseVideo">选择视频</button>
<button bindtap="startRecord">开始录制</button>
<button bindtap="stopRecord">停止录制</button>

Video recording can be achieved through button click event binding Interactive control.

  1. Implementation of the choose video function
chooseVideo: function() {
  uni.chooseVideo({
    sourceType: ['album'],
    success: function(res) {
      console.log(res.tempFilePath); // 视频的临时文件路径
    }
  });
}

Using the uni.chooseVideo API, you can select local video files and obtain the temporary file path of the video.

  1. Implementation of starting video recording function
var recorder = null;

startRecord: function() {
  recorder = uni.createVideoRecorder({
    duration: 10,
    success: function(res) {
      console.log(res.tempVideoPath); // 录制视频的临时文件路径
    }
  });
  
  recorder.start();
}

A video recorder can be created through the uni.createVideoRecorder API, and the recording duration can be set by setting the duration attribute. You can start recording video by calling the recorder.start() method. After the recording is completed, you can obtain the temporary file path of the recorded video through the success callback function.

  1. Implementation of stop recording video function
stopRecord: function() {
  recorder.stop();
}

You can stop recording video by calling the recorder.stop() method.

3. Summary

Through the above code examples, we can integrate and use the video playback and recording functions in UniApp. In actual development, we can customize the style and interaction of video playback and recording according to specific needs. At the same time, we can also expand more video playback and recording functions by using other plug-ins or package components.

I hope this article can help you integrate and use video playback and recording in UniApp. Wishing you better results in your development!

The above is the detailed content of UniApp realizes the integration and usage skills of video playback and recording. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn