0

I have an app that plays audio upon opening of a page, but will only work on my mac. WHen I use ionic DevApp it won't play off of my phone. It is just a simple sound effect that I loaded into assets but it won't work on the phone. This is the code for the page. Again, the sound is running perfectly in my browser but wont play off my phone. PLEASE HELP!!!

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')
}
}

Here is the code for the 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);
  });

}

 }

}
zgue
  • 3,793
  • 9
  • 34
  • 39

0 Answers0