9

My Cordova app currently has two ways to play audio, one works on the web app and one works on the cross-platform native app.

  • HTML5 for Web HTML: <audio id="audio" src="audio.mp3"></audio> JS: document.getElementById('audio').play();
  • Cordova Plugin for Mobile JS new Media('audio.mp3').play();

I'm not a fan of writing things twice. Is there an audio solution that works for both web and mobile? Or an elegant way to fallback?

benshope
  • 2,936
  • 4
  • 27
  • 39
  • 1
    Could you please share the code to play the audio file on Ionic App on Device? I am getting "Media is not defined." please help. – Samarth Agarwal Jul 08 '15 at 09:02

3 Answers3

4

I have used both the media plugin, and HTML5 audio in Cordova apps.

https://github.com/devgeeks/ExampleHTML5AudioStreaming

The Audio element is fairly well supported on mobile.

Devgeeks
  • 5,659
  • 4
  • 28
  • 35
  • Your answer works great "new Audio('audio.mp3').play();" Thanks! – benshope Jul 09 '14 at 12:51
  • 1
    No Working when app is deployed to mobile phone. Please help. – Samarth Agarwal Jul 08 '15 at 09:00
  • I can get HTML5 Audio to work fine on mobile. the biggest issue is background audio or playing audio while the screen is locked. HTML5 version doesnt support that – Daniel_Madain Oct 19 '15 at 21:49
  • @Daniel_Madain - iOS can play HTML5 audio in the background, it just needs a little change to the native stuffs... http://stackoverflow.com/questions/11616001/uiwebview-html5-audio-pauses-in-ios-6-when-app-enters-background/12414719#12414719 – Devgeeks Oct 19 '15 at 23:16
  • Does someone know if I can work with promises like Cordova Media plugin? I can just get the success callback from the `new Audio().play()` function. But just that promise. – Andrey Luiz Jul 11 '16 at 02:11
0

There is a good solution here that works with the cordova media plugin on devices and the code to emulate it in browsers:

http://forum.ionicframework.com/t/how-to-play-local-audio-files/7479/5

.k

Keith
  • 1,136
  • 2
  • 13
  • 30
0

I released a Ionic/Angular module that interfaces with Cordova Media to play audio files. More: https://stackoverflow.com/a/30018460/1964599

You can find the module here: https://github.com/arielfaur/ionic-audio

Community
  • 1
  • 1
arielf
  • 401
  • 2
  • 8