【问题标题】:Ionic native-audio doesn't work on android离子原生音频不适用于android
【发布时间】: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


【解决方案1】:

我在您的 smartAudio 提供程序中发现了一个问题。 请将 "asset: key" 更改为 "asset:asset" 并重试。

【讨论】:

  • 它不能解决问题.. 并且使它不能在 iOS 上工作。
【解决方案2】:

我遇到了同样的问题。但是我找到了这篇文章: Sound Effects using HTML5 and Native Audio in Ionic

我按照本文所述进行了所有操作 - 我的 Android 手机发出了声音。试试看 - 它应该工作。以防万一,我将显示文件 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。此示例在文章中有效。试试看。

【讨论】:

  • 欢迎来到 Stack Overflow。你能用英文重新发布这个吗? meta.stackexchange.com/questions/13676/…
  • @GonrasKarols: 对我不起作用的主要原因命令:this.smartAudio.preload() 必须放在这里:platform.ready (). then (() =&gt; { smartAudio.preload ('tabSwitch', 'assets / audio / clickSound.mp3'); }); 不早不晚
【解决方案3】:

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

在旧版本中可能无法使用。

【讨论】:

    猜你喜欢
    • 2019-02-08
    • 1970-01-01
    • 1970-01-01
    • 2014-11-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多