简体   繁体   中英

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?

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:

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):

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM