uniapp rtsp实时直播视频流

1、封装组件

<template>
  <!-- autoplay="false" -->
  <view name="videoPlayer" class="flex flex-direction" style="width: 100%">
    <video
      id="myVideo"
      class="response"
      :src="videoUrl"
      :title="videoTitle"
      controls="true"
      page-gesture="true"
      object-fit="contain"
      show-mute-btn="true"
      enable-play-gesture="true"
      vslide-gesture="true"
      :autoplay="true"
      @error="videoErrorCallback"
      @waiting="videoWaiting"
      @loadedmetadata="videoLoadOk"
      style="width: 100%"
    ></video>
  </view>
</template>

<script>
export default {
  name: 'videoPlayer',
  props: {
    title: {
      type: String,
      default: '',
    },
    firstPic: {
      type: String,
      default: '',
    },
    videoType: {
      type: String,
      default: '',
    },
    videoSrc: {
      type: String,
      default: '',
    },
  },
  data() {
    return {
      videoUrl: this.videoSrc,
      firstImg: this.firstPic,
      videoTitle: this.title,
    };
  },
  watch: {
    videoSrc() {
      this.setVideoUrl();
    },
  },
  mounted() {
    this.setVideoUrl();
  },

  onReady: function (res) {
    // #ifndef MP-ALIPAY
    this.videoContext = uni.createVideoContext('myVideo');
    // #endif
  },
  methods: {
    setVideoUrl() {
      console.log(this.videoSrc);
      // uni.setNavigationBarTitle({
      //     title: this.title
      // });
      this.videoUrl = this.videoSrc;
      this.firstImg = this.firstPic;
      this.videoTitle = this.title;

      console.log(this.firstImg + '___' + this.firstPic);

      console.log(this.title + '___' + this.videoTitle);
    },
    videoErrorCallback(e) {
      uni.showModal({
        content: e.target.errMsg,
        showCancel: false,
      });
    },
    videoWaiting() {
      // uni.showLoading({
      //     title: '加载中'
      // });
    },
    videoLoadOk() {
      // uni.hideLoading();
    },
  },
};
</script>

<style></style>

2、组价引用

<videoPlayer title="奥力给" videoSrc="rtsp://XXXXXXXXXXXXXXX"></videoPlayer>
  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
UniApp(统一应用平台)是一款基于Web技术栈开发原生移动应用的框架,它可以让你用一套代码跨多个平台构建响应式应用程序。要在UniApp中播放RTSP(Real-Time Streaming Protocol,实时流协议)视频流,你需要利用其支持的HTML5 Video API,并结合第三方库或服务来处理RTSP流。 以下是基本步骤: 1. **引入Video组件**:首先,在你的项目中引入 UniApp 中的 Video 组件,这是用来显示视频的基础。 ```html <video id="myVideo" style="width: 100%; height: auto;"></video> ``` 2. **创建RTSP源**:在JavaScript中,你需要构建 RTSP 视频源,通常是通过 URL,比如 `rtsp://your_server_address/video_stream`。 3. **初始化并设置视频源**:创建一个 MediaSource 对象,然后添加 SourceBuffer,最后将 RTSP URL 设置为 video 的 sourceURL 属性。 ```javascript const video = document.getElementById('myVideo'); const mediaSource = new MediaSource(); const videoSrcObject = new MediaStreamTrack(); mediaSource.addEventListener('sourceopen', () => { const sourceBuffer = mediaSource.addSourceBuffer('video/mp4; codecs="avc1.64001f, mp4a.40.2"'); fetch(`rtsp://your_server_address/video_stream`) .then(res => res.arrayBuffer()) .then(data => sourceBuffer.appendBuffer(data)) .catch(error => console.error('Error loading RTSP stream:', error)); video.srcObject = videoSrcObject; }); ``` 4. **启用自动播放**:为了保证视频能立即开始播放,可以在创建 `MediaStreamTrack` 时开启 autoplay 和 muted。 ```javascript video.srcObject = new MediaStream([new MediaStreamTrack()], { kind: 'video' }); video.autoplay = true; video.muted = true; ``` 5. **兼容性和错误处理**:注意RTSP在不同设备和浏览器上的兼容性可能不一致,你可能需要进行一些适配或者在用户端安装相应的插件(如ijkplayer等),以确保视频在所有目标平台上正常播放。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

程序员阿明

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值