如何在Ionic iOS中显示来自多媒体资料的视频

如何在Ionic iOS中显示来自多媒体资料的视频,ios,angular,cordova,ionic-framework,html5-video,Ios,Angular,Cordova,Ionic Framework,Html5 Video,我正在使用html5的视频标签来显示我从图库中选择的视频。我遇到了一个问题,视频无法加载,即使我提供了源 这是一个以电容器为桥梁的离子/角度项目,但仍使用Cordova插件。以下是我的代码原型: 我的页面.page.ts import { Camera, CameraOptions } from '@ionic-native/camera/ngx'; import { Capacitor } from '@capacitor/core'; @Component({...}) export cl

我正在使用html5的视频标签来显示我从图库中选择的视频。我遇到了一个问题,视频无法加载,即使我提供了源

这是一个以电容器为桥梁的离子/角度项目,但仍使用Cordova插件。以下是我的代码原型:

我的页面.page.ts

import { Camera, CameraOptions } from '@ionic-native/camera/ngx';
import { Capacitor } from '@capacitor/core';

@Component({...})
export class MyPage {

  // ... some code which gets the a reference to the video element from the template

  // this function is called when the user clicks a button to choose video from gallery
  onPickVideo() {
    const cameraOptions: CameraOptions = {
      destinationType: this.camera.DestinationType.NATIVE_URI,
      sourceType: this.camera.PictureSourceType.SAVEDPHOTOALBUM,
      mediaType: this.camera.MediaType.VIDEO,
    };
    this.camera.getPicture(cameraOptions).then(videoUri => {
      console.log(videoUri);
      this.videoSrc = Capacitor.convertFileSrc(videoUri);
      console.log(this.videoSrc);
      this.videoEl.load();
      this.videoEl.currentTime = 0.1;
    });
  }
}
我的页面.page.html

<video playsinline #video>
  <source [src]=".videoSrc" type="video/mp4" />
  Your browser does not support the video tag.
</video>
这在桌面和安卓系统上都有效。它不适用于带iOS 12的iPhone 6。未在其他iOS版本上测试

我尝试过的一些事情:

  • 添加了NSCamerauseDescription、NSPhotoLibraryUsageDescription、NSPhotoLibraryAddUsageDescription、NSMicrophoneUsageDescription
  • 在视频标记中使用了[src]=并删除了源标记。或省略“视频/mp4”类型
  • 在实时重新加载模式下运行,而不是仅仅构建
  • 在将文件传递给
    convertFileSrc()
    之前,切掉
    'file://'
    的开头。或者执行前者并直接将其设置为videoSrc,而根本不使用
    convertFileSrc()
通过“清理”URL解决。我还没明白这到底意味着什么。这是代码,以防有人需要

import { Camera, CameraOptions } from '@ionic-native/camera/ngx';
import { Capacitor } from '@capacitor/core';
import { DomSanitizer, SafeUrl } from '@angular/platform-browser';


@Component({...})
export class MyPage {

  // ... some code which gets the a reference to the video element from the template

  safeUrl: SafeUrl;

  constructor(private sanitizer: DomSanitizer) {}

  // this function is called when the user clicks a button to choose video from gallery
  onPickVideo() {
    const cameraOptions: CameraOptions = {
      destinationType: this.camera.DestinationType.NATIVE_URI,
      sourceType: this.camera.PictureSourceType.SAVEDPHOTOALBUM,
      mediaType: this.camera.MediaType.VIDEO,
    };
    this.camera.getPicture(cameraOptions).then(videoUri => {
      this.safeUrl = this.sanitizer.bypassSecurityTrustUrl(
          Capacitor.convertFileSrc(videoUri)
        );
      this.videoEl.load();
      this.videoEl.currentTime = 0.1;
    });
  }
}
然后确保在模板中使用safeUrl
[src]=“safeUrl”

通过“清理”URL解决。我还没明白这到底意味着什么。这是代码,以防有人需要

import { Camera, CameraOptions } from '@ionic-native/camera/ngx';
import { Capacitor } from '@capacitor/core';
import { DomSanitizer, SafeUrl } from '@angular/platform-browser';


@Component({...})
export class MyPage {

  // ... some code which gets the a reference to the video element from the template

  safeUrl: SafeUrl;

  constructor(private sanitizer: DomSanitizer) {}

  // this function is called when the user clicks a button to choose video from gallery
  onPickVideo() {
    const cameraOptions: CameraOptions = {
      destinationType: this.camera.DestinationType.NATIVE_URI,
      sourceType: this.camera.PictureSourceType.SAVEDPHOTOALBUM,
      mediaType: this.camera.MediaType.VIDEO,
    };
    this.camera.getPicture(cameraOptions).then(videoUri => {
      this.safeUrl = this.sanitizer.bypassSecurityTrustUrl(
          Capacitor.convertFileSrc(videoUri)
        );
      this.videoEl.load();
      this.videoEl.currentTime = 0.1;
    });
  }
}
然后确保在模板
[src]=“safeUrl”
中使用safeUrl