Ionic Native-Audio 在安卓设备上无法运行。

15

我在使用ionic时遇到了使用本地音频 cordova 插件的问题。我已经通过npm安装了native插件。

sudo npm install --save @ionic-native/native-audio

我添加了一个名为smartAudio的新提供商(代码如下)。 它在ionic的Web视图和iOS模拟器/真实设备上都能很好地工作...但由于某种原因,在android模拟器/真实设备上没有声音。

我有一个ion-slides元素,使用*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
@Gandhi 我不知道如何在调用play之前检查路径,但我使用相对路径 - 例如:{ name: 'cat', img_url: './assets/img/animals/cat.jpg', sound_url: './assets/sounds/animals/cat.wav' },当用户点击正确的动物时,它的声音应该播放(来自assets/sounds/animals文件夹)。 - Gonras Karols
你能试一下这个代码吗?setTimeout(()=>{ this.smartAudio.play(currentIndex); },3000); - Swapnil Patwa
"@ionic-native/native-audio": "3.12.1" 请尝试在package.json中更改版本号。 - Swapnil Patwa
1
很遗憾,RickyLevi,还是卡在这个问题上了。 - Gonras Karols
显示剩余14条评论
3个回答

1
我遇到了同样的问题。但是我找到了这篇文章: 使用HTML5和Ionic中的本地音频实现声音效果 我按照这篇文章的步骤做了一遍,我的安卓手机上就有声音了。你也可以试试,应该能够正常工作。以防万一,我会展示文件 package.json:
  "dependencies": {
    "@angular/common": "4.4.3",
    "@angular/compiler": "4.4.3",
    "@angular/compiler-cli": "4.4.3",
    "@angular/core": "4.4.3",
    "@angular/forms": "4.4.3",
    "@angular/http": "4.4.3",
    "@angular/platform-browser": "4.4.3",
    "@angular/platform-browser-dynamic": "4.4.3",
    "@ionic-native/core": "4.3.0",
    "@ionic-native/native-audio": "^4.3.0",
    "@ionic-native/social-sharing": "^4.3.0",
    "@ionic-native/splash-screen": "4.3.0",
    "@ionic-native/status-bar": "4.3.0",
    "@ionic/storage": "2.0.1",
    "ionic-angular": "3.7.1",
    "ionicons": "3.0.0",
    "rxjs": "5.4.3",
    "sw-toolbox": "3.6.0",
    "zone.js": "0.8.18"
  },
  "devDependencies": {
    "@ionic/app-scripts": "3.0.0",
    "typescript": "2.3.4"
  },

使用的是最新版本的Ionic。这个例子在文章中可以运行。试试看吧。

2
欢迎来到Stack Overflow。您能用英语重新发布一下吗?https://meta.stackexchange.com/questions/13676/do-posts-have-to-be-in-english-on-stack-exchange - chicks
1
@GonrasKarols: 这个命令没有起作用的主要原因是,需要将它放在这里:`platform.ready().then(() => { smartAudio.preload('tabSwitch', 'assets/audio/clickSound.mp3'); });`而不是早些或晚些的地方。 - Павел Полько

1
我发现你的smartAudio提供程序存在问题。请将"asset: key"更改为"asset: asset",然后重试。

enter image description here


它并没有解决问题,反而使其在iOS上无法工作。 - Gonras Karols

0

请确保您的Android API级别为14-21。

在旧版本中可能无法正常工作。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接