ios - 如何在 Ionic iOS 中显示来自画廊的视频
问题描述
我正在使用 html5 的视频标签来显示我从图库中选择的视频。我遇到了一个问题,即使我提供了来源,视频也无法加载。
这是一个以电容器为桥梁的 Ionic/Angular 项目,但仍使用 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.page.html
<video playsinline #video>
<source [src]=".videoSrc" type="video/mp4" />
Your browser does not support the video tag.
</video>
my-page.page.ts 的输出是:
file:///private/var/mobile/Containers/Data/Application/7D85727C-CE9A-4816-BC42-C97F03AFDEB4/tmp/F6DCE819-ED4A-41E4-BAFB-814500F2FB27.MOV
capacitor://localhost/_capacitor_file_/private/var/mobile/Containers/Data/Application/7D85727C-CE9A-4816-BC42-C97F03AFDEB4/tmp/F6DCE819-ED4A-41E4-BAFB-814500F2FB27.MOV
这适用于桌面和 Android。它不适用于装有 iOS 12 的 iPhone 6。在其他 iOS 版本上未经测试。
我尝试过的一些事情:
- 添加了 NSCameraUsageDescription、NSPhotoLibraryUsageDescription、NSPhotoLibraryAddUsageDescription、NSMicrophoneUsageDescription
- 在 video 标签中使用了 [src]=,并删除了 source 标签。或省略“video/mp4”类型
- 以实时重新加载模式运行与仅构建。
- 在
'file:///'
将videoUri
其传递给convertFileSrc()
. 或者做前者并直接将其设置为 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"
。
推荐阅读
- flutter - 如何在 Flutter 中为 Material Icons 和 Font Awesome Icons 使用相同的大小?
- node.js - Socket.io 禁用 perMessageDeflate
- google-data-studio - 在 Google 数据洞察社区连接器中刷新凭据
- python - 如何检查没有顺序的列表中存在的列表列表
- java - 如何让 JAVA_HOME 显示给我看?如果我先做 source ~ / .bash_profile 它只会向我显示
- powershell - 具有返回值空间的 curl 命令无法分配给 shell 中的变量
- git - 在 Azure DevOps 中使用 Git 进行门控推送
- entity-framework-core - 然后在 Entity Framework Core 3.1.4 中包含缺失?
- symfony - 在 symfony encore 上全局引导 mixin
- python - 在 tensorflow 库上实现 tflite