【问题标题】:Read video stored on android native storage in my Ionic app在我的 Ionic 应用程序中读取存储在 android 本机存储中的视频
【发布时间】:2019-08-14 20:09:38
【问题描述】:

我正在开发 ionic 3 应用程序,将视频从服务器下载到设备的本机存储。我用这个 URL 来保存文件:

let externalDir = this.file.externalDataDirectory;

文件下载很好,我可以从设备上查看它。 有关更多详细信息,存储中的文件链接是:

/storage/emulated/0/Android/data/xxxxxxxx/files/VideoTest1/0.mp4

但问题是我想使用<video> 标签将下载的视频播放到我的应用程序中,我使用相同的链接查看它,但它不起作用。有什么帮助吗? 对不起我的英语

【问题讨论】:

    标签: android ionic-framework storage ionic-native


    【解决方案1】:

    我一直在寻找解决方案。我按如下方式解决此问题。

    platform.ready()函数之后我创建了一个目录如下。

    this.file.createDir(this.file.externalDataDirectory , 'Videos', false).then(() => {
            // console.log('Directory created successfully');
          }).catch(() => {
            // console.log('Failed to create directory');
          });
    

    在创建的目录中下载视频

    import { FileTransfer, FileTransferObject } from '@ionic-native/file-transfer';
    import { File } from '@ionic-native/file';
    import { Diagnostic } from '@ionic-native/diagnostic';
    ...
    
    constructor(private diagnostic: Diagnostic,
        private transfer: FileTransfer, private file: File)
    
    
    download(item) {
        let downloadProgress: any;
        const fileTransfer: FileTransferObject = this.transfer.create();
        const url = encodeURI(item.url);
        const targetPath = this.file.externalDataDirectory + 'Videos/' + item.title + '.mp4';
        this.diagnostic.requestCameraAuthorization(true).then(() => {
            fileTransfer.download(url, targetPath, true).then((entry) => {
                // console.log('download complete: ' + entry.toURL());
              }, (error) => {
                  console.log(error);
              });
              fileTransfer.onProgress((progress) => {
              downloadProgress = Math.round((progress.loaded / progress.total) * 100) + '%';
                  console.log('Downloading progress ...', downloadProgress);
              });
        }, (error) => {
            console.log(error);
        });
    
      }
    

    这会在创建的Videos 文件夹中下载一个文件。实际上,您可以通过 Android/data/com.your.app/files/ 在设备上找到它。

    如果您使用的是 android 但不起作用,请在 FileTransfer.java 中添加 file.setReadable(true, false);

    播放视频以供离线使用 确保这些行存在于index.html

    <meta http-equiv="Content-Security-Policy" content="style-src 'self' 'unsafe-inline'; media-src *; connect-src *">
    

    在您的config.xml 文件中

    <access origin="*" />
    <allow-navigation href="*" />
    <allow-navigation href="http://*/*" />
    <allow-navigation href="file://*/*" />
    <access origin="cdvfile://*" />
    <allow-intent href="*" />
    <access origin="*" />
    

    如果不添加它们。

    最后在你的视频播放器页面

    <video id="video" controls="controls" preload="metadata" autoplay="autoplay" webkit-playsinline="webkit-playsinline" class="videoPlayer">
         <source [src]="content" type="video/mp4" />
    </video>
    

    在组件中

    ionViewDidEnter() {
        this.file.listDir(this.file.externalDataDirectory, 'Videos')
          .then((data) => {
            console.log(data[0]);
            this.count = data.length;
            const src = data[0].toInternalURL();
            this.file.resolveLocalFilesystemUrl(src).then(data => {
              this.content = data.toURL();
              document.getElementById('video').setAttribute('src', this.content);
              console.log('Content path cahnged ', this.content);
            }, error => {
              console.log('File path error');
            })
          })
          .catch(err => console.log('Directory doesnt exist'));
    
      }
    

    就是这样。我希望这有效。

    【讨论】:

      【解决方案2】:

      如果您想访问任何文件以用于移动设备的本机存储。然后你必须为你想要访问的文件夹创建一个本地服务器。

      https://ionicframework.com/docs/native/httpd

      constructor(public file:File, public ngZone : NgZone, public httpd :Httpd){
      }
      
      startLocalServe(){
      
          let options: HttpdOptions = {
            www_root: this.file.externalDataDirectory.replace('file://',''),
            port: 9000,
            localhost_only: true //if you want to create multiple localhost then false  
          };
      
          this.httpd.startServer(options).subscribe(url => {
            console.log('Server is live',url); //Server is live http://127.0.0.0.1:9000
            this.ngZone.run(()=>{
              this.localServeUrl = url;
            })
      
          },error => {
            console.log('Fail to start Server : ',JSON.stringify(error));
          });
        }
      

      现在您可以将 localserveUrl 设置为 src="http://127.0.0.0.1:9000/VideoTest1/0.mp4"

      将代码 startLocalServe 放在 app.component.ts 中

      this.platform.ready().then(()=>{
      startLocalServe();
      });
      

      【讨论】:

        猜你喜欢
        • 2018-04-23
        • 1970-01-01
        • 2015-09-26
        • 2018-04-05
        • 1970-01-01
        • 2018-12-11
        • 2020-09-20
        • 1970-01-01
        • 2018-03-06
        相关资源
        最近更新 更多