【问题标题】:Ionic 3 and firebase storageIonic 3 和 Firebase 存储
【发布时间】:2018-06-27 08:23:04
【问题描述】:

有一种情况,我在 firebase 存储中有一些音频,需要下载到手机本地内存,所以当我启动应用程序时,它应该检查本地内存中是否有任何音频,如果存在,它会播放,否则从 firebase 存储下载。

任何人都可以帮助我这个 ionic 3 和 firebase 吗?

我有这个连接

<script src="https://www.gstatic.com/firebasejs/5.0.4/firebase.js"></script>
<script >
var config = {
apiKey: "AIzaSyAg6dBk2BJ-DlFHm0f0G-9XpIcjb-Zl7DY",
authDomain: "helloworld-4e6e6.firebaseapp.com",
databaseURL: "https://helloworld-4e6e6.firebaseio.com",
projectId: "helloworld-4e6e6",
storageBucket: "helloworld-4e6e6.appspot.com",
messagingSenderId: "386944915967"
};
firebase.initializeApp(config);
</script>

这是我的.ts 文件

import { Component } from '@angular/core';
import { NavController } from 'ionic-angular';
import { AboutPage } from '../about';
import { FileTransfer, FileUploadOptions, FileTransferObject } from '@ionic- 
native/file-transfer';
import { NativeAudio } from '@ionic-native/native-audio';

@Component({
   selector: 'page-home',
   templateUrl: 'home.html'
 })
 export class HomePage {
 bleep = new Audio();
   constructor() {
}
 download() {
     const url = 'https://firebasestorage.googleapis.com/v0/b/helloworld- 
 4e6e6.appspot.com/o/audio%2Fp10_1.mp3?alt=media&token=12bf3400-17a7-4cf9- 
 b862-973707a3164e';
      fileTransfer.download(url, this.file.dataDirectory + 
 'test.mp3').then((entry) => {
       console.log('download complete: ' + entry.toURL());
     }, (error) => {
          console.log('Error during download: '+error);
      });
   }


 wajid()
 {

 const fileTransfer: FileTransferObject = this.transfer.create();
 this.download();
 this.nativeAudio.preloadSimple('uniqueId1', this.file.dataDirectory + 
 'test.mp3').then((res) => {
         console.log('Mp3 ready');
     }, (error) => {
         console.log("Some error during load the audio "+error);
      });

      this.nativeAudio.play('uniqueId1').then((onSuccess)=>{
          console.log('Mp3 reproduced ok');
      }, (onError)=>{
          console.log("Some error during reproduce of the audio "+onError);
      });

      // can optionally pass a callback to be called when the file is done 
   playing
      //this.nativeAudio.play('uniqueId1', () => console.log('uniqueId1 is 
  done playing'));


 }

这是我的html按钮

<div class= "sections" style="width: 18%;" (tap)="wajid()" (press) = "wajidlong5()">
      <div class = "sections" id = "sec1" >
       ﻞَ  
      </div><!--
      --><div class = "sections" id = "sec2" >
         ﻌَ  
      </div><!--
      --><div class = "sections" id = "sec3" >
           ﺟَ  
      </div>

    </div> 

【问题讨论】:

  • 不是真的......你必须了解更多关于离子......它不是那么简单。
  • @Cristian 从那里我学会了实现这一点,我真的需要尽快做到这一点!帮帮我伙计
  • 这里:ionicframework.com/docs/native,寻找文件传输,在那里你也可以检查文件是否已经存在
  • @Cristian 我需要从 Firebase 存储下载音频,然后检查它是否已经从资产播放,否则它将下载该音频并将其放置在资产文件夹中,然后从资产播放。请帮助:)
  • 不能直接放入assets。检查 FILETRANSFER 离子插件

标签: cordova firebase ionic3


【解决方案1】:

要下载音频,请使用:File Transfer

const fileTransfer: FileTransferObject = this.transfer.create();

import { File } from '@ionic-native/file';

constructor(private file: File) { }

download() {
  const url = 'https://firebasestorage.googleapis.com/v0/b/helloworld-4e6e6.appspot.com/o/audio%2Fp10_1.mp3?alt=media&token=12bf3400-17a7-4cf9-b862-973707a3164e';
  fileTransfer.download(url, this.file.dataDirectory + 'test.mp3').then((entry) => {
    console.log('download complete: ' + entry.toURL());
  }, (error) => {
    // handle error
  });
}

要检查文件是否在您的文件夹中,请使用本机插件File

查看两个插件规范。

有问题在这里问,我会编辑答案

要播放文件,您必须使用 NATIVE AUDIO 。 所以:

this.nativeAudio.preloadSimple('uniqueId1', this.file.dataDirectory + 'test.mp3').then(onSuccess, onError);

现在:

this.nativeAudio.play('uniqueId1').then(onSuccess, onError);

// can optionally pass a callback to be called when the file is done playing
this.nativeAudio.play('uniqueId1', () => console.log('uniqueId1 is done playing'));

完整代码:

import { FileTransfer, FileUploadOptions, FileTransferObject } from '@ionic-native/file-transfer';
import { File } from '@ionic-native/file';
import { NativeAudio } from '@ionic-native/native-audio';

constructor(private transfer: FileTransfer,private file: File,private nativeAudio: NativeAudio) {
    const fileTransfer: FileTransferObject = this.transfer.create();
    this.download();
    this.nativeAudio.preloadSimple('uniqueId1', this.file.dataDirectory + 'test.mp3').then((res) => {
        console.log('Mp3 ready');
    }, (error) => {
        console.log("Some error during load the audio "+error);
    });

    this.nativeAudio.play('uniqueId1').then((onSuccess)=>{
        console.log('Mp3 reproduced ok');
    }, (onError)=>{
        console.log("Some error during reproduce of the audio "+onError);
    });

    // can optionally pass a callback to be called when the file is done playing
    //this.nativeAudio.play('uniqueId1', () => console.log('uniqueId1 is done playing'));

 }

download() {
    const url = 'https://firebasestorage.googleapis.com/v0/b/helloworld-4e6e6.appspot.com/o/audio%2Fp10_1.mp3?alt=media&token=12bf3400-17a7-4cf9-b862-973707a3164e';
    fileTransfer.download(url, this.file.dataDirectory + 'test.mp3').then((entry) => {
      console.log('download complete: ' + entry.toURL());
    }, (error) => {
        console.log('Error during download: '+error);
    });
  }

【讨论】:

  • 在您的 page.ts 中。 download() 是下载文件的函数。 this.file.dataDirectory 是下载文件的目录
  • 我是否必须指定替换 this.file.dataDirectory 的路径,或者让它成为它的存在方式,顺便说一下我的文件应该下载到目录 (Desktop\hello\www\assets\sounds)
  • 我的 firebase 存储音频 url 是 --> firebasestorage.googleapis.com/v0/b/… 我需要在哪里下载它,即 --> Desktop\hello\www\assets\sounds 你能用这些 url 编辑吗
  • 我放了网址,保存路径,在文件插件上搜索
  • 我在点击按钮时发布了您上面的代码,所以现在应该下载以及如何播放下载的文件?
猜你喜欢
  • 1970-01-01
  • 2018-04-02
  • 2018-03-10
  • 1970-01-01
  • 2018-06-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-04-23
相关资源
最近更新 更多