2017-07-16 44 views
12

我遇到了使用原生音频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); 
      }); 

     } 

    } 

} 
+0

您能否提供版本信息原生媒体插件? – Gandhi

+0

我们看到完全相同的问题。即使不使用“原生音频”,但使用纯HTML5音频,它可以在浏览器中运行,但不能在设备上运行。 –

+0

@Gandhi - '@ ionic-native/native-audio“:”^ 3.14.0“' –

回答

0

我在您的smartAudio提供程序中发现问题。 请将“asset:key”改为“asset:asset”并重试。

enter image description here

+0

它不能解决问题,并使其不能在iOS上工作。 –

-1

我有同样的问题。但是,我发现这篇文章: 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。这个例子在文章中有效。尝试一下。

+2

欢迎使用堆栈溢出。你能用英文转发吗? https://meta.stackexchange.com/questions/13676/do-posts-have-to-be-in-english-on-stack-exchange – chicks

+1

@GonrasKarols: 主要原因为什么没有为我工作 命令: this.smartAudio.preload() 有必要放在这里: 'platform.ready()。然后(()=> {smartWord.preload('tabSwitch','assets/audio/clickSound.mp3'); –

0

确认您的Android API级别是14 - 21

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

相关问题