我无法弄清楚为什么SmartAudioProvider会在我的Mac上提供音频,但无法在iPhone上运行

时间:2017-12-20 07:13:36

标签: angular audio ionic-framework ionic-native

我有一个应用程序在打开页面时播放音频,但只能在我的Mac上运行。当我使用离子DevApp时,它不能用我的手机播放。这只是一个简单的声音效果,我加载到资产,但它不能在手机上工作。这是页面的代码。同样,声音在我的浏览器中运行完美,但不会播放我的手机。请帮忙!!!

export class FinalPage {
price: any;
location: any;
cuisine: any;
restaurants: Array<string>;
selectedItem: any;
restaurant: any;


constructor(public alertCtrl: AlertController, public navCtrl: 
NavController, public navParams: NavParams, private vibration: 
Vibration, 
public smartAudio: SmartAudioProvider) {
this.selectedItem = navParams.get('item');
this.restaurants = ["Pepolino Restaurant", "Locanda Verde", "Scalini 
Fedeli", "Gran Morsi", "Ecco", "Tutto il Giorno", "The Odeon", "Mamo 
Restaurant", "Petrarca Cucina E Vino", "Osteria Morini","Maialino", 
"Marta", "Giorgios", "Zio Ristorante", " Manzo Ristorante", "Obica 
Mozzarella Bar Pizza e Cucina",'Rafele Restaurante', 'Artusi', 'Palma', 
'Barbuto', 'Dell Anima', 'Morandi', 'Via Carota', 'I Sodi', 'Lupa', 
'Babbo'];

this.restaurantChoice();
this.vibrate();
this.showAlert();
this.buttonPush();


}

restaurantChoice() {
let restaurant = Math.floor(Math.random() * 30);
this.restaurant = restaurant;
}

ionViewDidLoad() {
console.log('ionViewDidLoad FinalPage');
}

vibrate() {
this.vibration.vibrate(10000);
}

 showAlert() {
 let alert = this.alertCtrl.create({
  title: this.restaurants[this.restaurant],
  buttons: ['Enjoy your meal!!']
 });
 alert.present();
 }

itemTapped(event, item) {
this.navCtrl.push(HomePage, {
  item: item
});


}
buttonPush(){
this.smartAudio.play('buttonPush')
}
}

以下是smartAudioProvider的代码:

import { HttpClient } from '@angular/common/http';
import { Injectable } from '@angular/core';
import {Platform} from "ionic-angular";
import {NativeAudio} from "@ionic-native/native-audio";


@Injectable()
export class SmartAudioProvider {

 audioType: string = 'html5';
 sounds: any = [];

 constructor(public nativeAudio: NativeAudio, platform: Platform) {

 if(platform.is('cordova')){
  this.audioType = 'native';
 }

 }

 preload(key, asset) {

 if(this.audioType === 'html5'){

  let audio = {
    key: key,
    asset: asset,
    type: 'html5'
  };

  this.sounds.push(audio);

  } else {

  this.nativeAudio.preloadSimple(key, asset);

  let audio = {
    key: key,
    asset: key,
    type: 'native'
  };

  this.sounds.push(audio);
  }

  }

play(key){

let audio = this.sounds.find((sound) => {
  return sound.key === key;
});

if(audio.type === 'html5'){

  let audioAsset = new Audio(audio.asset);
  audioAsset.play();

} else {

  this.nativeAudio.play(audio.asset).then((res) => {
    console.log(res);
  }, (err) => {
    console.log(err);
  });

}

 }

}

0 个答案:

没有答案