Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Phonegap / Cordova - Code not playing audio on android (Not a path issue)

I am working on a phonegap/cordova app which plays audio files stored locally.

I am having issues with some code and android where example 1 does not play on android and example 2 does play.

NOTE: The issues is not the path, I know that the path to the file is correct.

Example 1 - Does not play on android but plays fine on IOS where the path is modified for IOS.

var audiofile = cordova.file.dataDirectory+'android_asset/www/audio/1.aac';

 ...

 $( ".player" ).html('<div class="audioDemo-wrapper"><audio class="audioDemo" controls><source src="'+audiofile+'" type="audio/mpeg"></audio><div class="closeAudioBtn" onclick="stopAndCloseAudio();">X</div></div>');
 $(".audioDemo").trigger('play');

Example 2 - This works on Android but it's not the way I'd like to go.

var audiofile = cordova.file.dataDirectory+'android_asset/www/audio/1.aac';

 window.open(audiofile, '_blank', 'location=no,closebuttoncaption=Close');

The difference is mainly that Example 2 opens on a new window ?

Why does Example 1 not play on Android when it plays fine on IOS (Note: Paths are not the problem)

Any ideas?

like image 279
Satch3000 Avatar asked Nov 09 '22 19:11

Satch3000


1 Answers

With the Cordova webview on Android you can use:

1) Audio HTML5 API (also wrapped with jQuery) for playing audio files from the web.

2) Cordova plugins which provide APIs to play local audio files bundled with the Cordova project; for example two Cordova plugins to play audio files are:

  • cordova-plugin-media
  • cordova-plugin-nativeaudio

An alternative is to use Crosswalk in a Cordova project, so replacing the native Cordova webview with the Crosswalk webview. This way you have a Chromium based webview, enabling support for all modern web APIs, including audio.

Some useful mini-tutorials for Ionic/Cordova project (applying to only Cordova projects):

  • http://gonehybrid.com/how-to-add-sound-effects-to-your-ionic-app-with-native-audio/
  • https://blog.nraboy.com/2014/11/playing-audio-android-ios-ionicframework-app/

P.S.: unfortunately I wasn't able to find on Cordova docs details about web APIs supported by Android Cordova webview

like image 121
beaver Avatar answered Nov 14 '22 22:11

beaver