Quantcast
Channel: Ionic Forum - Latest topics
Viewing all articles
Browse latest Browse all 70433

What is the best way to play audio at the moment?

$
0
0

@Ripper346 wrote:

Hi, I am writing a Ionic 5 + Angular app and using capacitor to build for android (and next to ios). I have to play some audio files (all stored in /src/assets folder) in certain moments. I tough it would be easy but after 3 days trying I am only more confused than before :sweat_smile:. If I am correct, there are 3 main ways to play an audio:

  1. HTML 5
let audio = new Audio('../../assets/audio/countdown/0.mp3');
audio.load();
audio.play();
  1. Native audio plugin with the latest update on 17 Apr 2017

  2. Media plugin that it’s active

I started with native audio but I had no luck (if I remember correctly I wasn’t able to provide a valid path) and since I saw that it wasn’t developed anymore I tried to go on media plugin. With media plugin, I made a simple button to test the play action, so I wrote in the component:

import { Media } from '@ionic-native/media/ngx';

@Component({
  selector: 'app-home',
  templateUrl: 'home.page.html',
  styleUrls: ['home.page.scss'],
})
export class HomePage implements OnInit {
  constructor(private media: Media) { }

  start() {
    let zero = this.media.create(`cdvfile://localhost/assets/audio/countdown/0.mp3`);
    zero.setVolume(1);
    zero.play();
  }
}

It doesn’t work. From Android Studio running logs I have

V/Capacitor/Plugin: To native (Cordova plugin): callbackId: INVALID, service: Media, action: create, actionArgs: ["98bc104d-bb37-3285-8a58-b87444470383","cdvfile:\/\/localhost\/assets\/audio\/countdown\/0.mp3"]
V/Capacitor/Plugin: To native (Cordova plugin): callbackId: INVALID, service: Media, action: setVolume, actionArgs: ["98bc104d-bb37-3285-8a58-b87444470383",1]
V/Capacitor/Plugin: To native (Cordova plugin): callbackId: INVALID, service: Media, action: startPlayingAudio, actionArgs: ["98bc104d-bb37-3285-8a58-b87444470383","cdvfile:\/\/localhost\/assets\/audio\/countdown\/0.mp3",null]
W/AudioManager: Use of stream types is deprecated for operations other than volume control
    See the documentation of requestAudioFocus() for what to use instead with android.media.AudioAttributes to qualify your playback use case

Thinking it was the path that was wrong I tried several times without success (cdvfile://, file:// or directly the path). Can anyone help me? Maybe I am missing something to import or to set, or maybe there’s a better way to do it, I don’t know at this point, I am fairly new to Ionic.

I had no problem with HTML 5 way but I read in this forum that it doesn’t work with background play so I wanted to implement with the cordova plugin.

Thank you.

Posts: 1

Participants: 1

Read full topic


Viewing all articles
Browse latest Browse all 70433

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>