How to play local audio files

Thank you for your fast response, I will try your suggestion. Merci bcp !

@loicknuchel
Hi, I use you code, now it’s worded.
But I want get the media status, like html5 pased( ).
And when the media end, the media should give me a event, tell me the media is end.
Can you tell me how to do it ?

Thanks.

Hi,

You have the onStatus and onStop callbacks for that.
If you want a method like : getStatus(), you have to save it every time it changed (via onStatus and onStop)

Hi,

I just created a gist with some code sample : https://gist.github.com/loicknuchel/f0222bb66fdaaf7e3391

I added a field status to media instance to get the current media status.

Tell me if it helps.

3 Likes

ok.
Thank you.
Let me try.

@loicknuchel
Hi,
I want to get mediaStatus info in controllers.js, because services.js can’t inject the $scope.
And when the music end, a callback method auto run in my controllers.js.
So, how can I do ?

see https://gist.github.com/loicknuchel/f0222bb66fdaaf7e3391#file-app-js-L18
Il you call myMedia.status in the method onStop you will have it… But it will always be STOPPED

Thank you for your help.
But I use myMedia.status.code get media status code.

@loicknuchel
Hi, I want to get the audio duration when the audio begin, how can I do ?

@loicknuchel
Hi, I fix the problem.I add onError() and onStatus(status).

function onError(){
alert(“Get audio error”);
}
function onStatus(status){
var audioDuration = $scope.media._duration;
}

good for you! save alot of my time!

Hi ,
I have built an app by using media plugin but i want to automatically pause and resume the media player while getting a call or message.
can you please tell how we can achieve this.

Thanks and Regards
Anil Kumar B

thank you for this, loicknuchel

Hi, from this thread, it sounds like the media plugin does not allow for audio playback while the app is “minimized” or “in the background”, meaning the user navigated away from the app. However, there is this thread on github that suggests it does. Has someone tested this on iOS and Android? Could you please clarify. Thanks!

1 Like

It does work if I switch the screen off, or switch apps - at least on Android. I heard it works on iOS too!

Thank you so much for the fallback script!!!

You stated “In Android, the HTML5 audio API is muted, it should be the same on iOS, that’s why the fallback didn’t work.”

How does one unmute that? I can simply pass a mpeg audio stream through an audio element in phonegap with no issues. Why is this an issue in ionic? My audio tags work great in ionic serve, once I build my app I hear nothing.

Hi,

I don’t know much but, of course it’ not an issue with Ionic, it’s an issue with android browser.
As you say, it works great on desktop (ionic serve) but not on device, that why you should use the media plugin (sadly, it adds a lot of permissions).
I can’t find an option to enable this feature but your user won’t too…

data-tap-disabled=“true” had to be added to the tag :laughing:

I have successfully used NativeAudio plugin to play files that are under assets www /audio subfolder.
It works quite well.
The PhoneGap Build page for the plugin
Github page for the plugin

Unfortunately it only works for the files that were bundled with the app.
If you want to download (e.g. with FileTransfer plugin) files and then play them, you can not use that plugin. The reason is that it is coded to use asset folder files (it prefixes path with “www”),
and FileTransfer can not save into assets folder (as per this StackOverflow thread)

So while this plugin does not cover all my needs, it might be of help to you if you only need bundled audio files played.