我遇到了使用原生音频cordova插件和离子的问题。我安装的本机使用NPM离子原生音频无法在Android上运行
sudo npm install --save @ionic-native/native-audio
而且增加了一个名为的SmartAudio(附后代码)新的供应商。 它在离子的网络视图和iOS模拟器/真实设备上都像一个魅力一样。但由于某种原因,Android模拟器/真实设备上没有任何声音。
我有一个生成图像幻灯片使用* ngFor,像这样的离子幻灯片元素 -
<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>
凡playAnimalSound()函数看起来像这样 -
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);
}
我的SmartAudio提供商是为像这样定义 -
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);
});
}
}
}
您能否提供版本信息原生媒体插件? – Gandhi
我们看到完全相同的问题。即使不使用“原生音频”,但使用纯HTML5音频,它可以在浏览器中运行,但不能在设备上运行。 –
@Gandhi - '@ ionic-native/native-audio“:”^ 3.14.0“' –