I’m having a problem to use the native-audio cordova plugin with ionic. I installed native using npm
sudo npm install --save @ionic-native/native-audio
And added a new provider called smartAudio (code attached below).
It works like a charm both in ionic’s web view and on iOS emulator/real device as well… but for some reason there’s no sound on android emulators/real devices at all.
I have an ion-slides element that generate images slide using *ngFor, like this -
<ion-slides (ionSlideDidChange)="muteAnimalSound()" pager dir="rtl" [loop]="true" >
<ion-slide *ngFor="let slide of animals_slides; let i = index" style="background-color: white">
<img src="{{slide.img_url}}" (click)="playAnimalSound($event)">
</ion-slide>
</ion-slides>
Where playAnimalSound() function looks like this -
playAnimalSound(ev) {
let animalSound = this.getAnimalBySource(ev.target.src);
let currentIndex = this.slides.getActiveIndex();
this.smartAudio.preload(currentIndex, animalSound[0].sound_url);
this.smartAudio.play(currentIndex);
}
My smartAudio provider was is defined like this -
export class SmartAudio {
audioType: string = 'html5';
sounds: any = [];
constructor(public nativeAudio: NativeAudio, platform: Platform) {
if(platform.is('cordova')){
this.audioType = 'native';
}
//testing atlassian sourcetree
}
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);
});
}
}
stop(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.stop(audio.asset).then((res) => {
console.log(res);
}, (err) => {
console.log(err);
});
}
}
}
The paths are defined in a json structured like this:
{ name: 'cat', img_url: './assets/img/animals/cat.jpg', sound_url: './assets/sounds/animals/cat.wav' }