【发布时间】:2017-07-16 19:57:50
【问题描述】:
我在将 native-audio cordova 插件与 ionic 一起使用时遇到问题。我使用 npm 安装了本机
sudo npm install --save @ionic-native/native-audio
并添加了一个名为 smartAudio 的新提供程序(代码附在下面)。 它在 ionic 的 web 视图和 iOS 模拟器/真实设备上都像魅力一样工作。但由于某种原因,android 模拟器/真实设备上根本没有声音。
我有一个使用 *ngFor 生成图像幻灯片的 ion-slides 元素,就像这样 -
<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);
});
}
}
}
【问题讨论】:
-
能否提供版本信息原生媒体插件?
-
我们看到了完全相同的问题。即使不使用
native-audio,而是使用纯 HTML5 音频,它也可以在浏览器中使用,但不能在设备中使用。 -
@Gandhi -
@ionic-native/native-audio": "^3.14.0" -
@GonrasKarols Android webview 已知在使用 html5 音频/视频标签时存在问题。此问题可能是由于路径问题。在调用 play 之前,您在代码中的路径是什么?
-
@RickyLevi 不幸的是没有......仍然坚持这个。
标签: android angular cordova ionic2