-
Notifications
You must be signed in to change notification settings - Fork 767
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Streaming & Background threading on play #62
Conversation
Fix for CB-57 Updated to use avplayer when url starts with http:// or https:// for full streaming support. All other urls will use the standard AVAudioplayer Fix for CB-8222 Background thread on play to prevent locking during initial load of media.
I tested this on my app and the playback starts and pauses as expected [on a streaming m3u]. +1 |
Been testing this and the stop() method does not seem to work, I had to use pause() to get the audio to actually stop when streaming an mp3 file. |
avplayer which is used for streaming does not have an equivalent stop method. You can just pause() and then release() there is no need to stop(). |
Awesome thanks! |
The API includes stop, if pause+release is the same thing, then your code should do that! |
I'll look at putting something in that mimics the existing stop behavior applied to avaudioplayer. Any chance of this actually getting merged once that is done? |
Hi, NB : @ghenry22 the getDuration seems to not work |
+1 |
Alkashi - getDuration definitely works as expected on ios devices. I use it every day in my app and have tested it extensively on multiple devices and on the simulator. There is always the chance that some files won't work of course, but that is more a limitation of the underlying AVPlayer framework and not something we can do anything about. |
@ghenry22 OK Thanks for the feedback I'll try again asap. |
@ghenry22 I just tested and the getDuration() method which is supposed to return the total duration of the mp3 file return me always 0. On Android everything woks fine. |
This pull request doesn't make any changes to the behavior of get duration calls though, I see with my media some times 0 is returned for some files sometimes it's a full duration, seems to vary depending on what information is available in the file or stream. |
@Alkashi Im using this same code on iOS 9 and its working properly. Here is a code snippet on what I am doing. function onPlayerStatus(status) {
console.log("onPlayerStatus:" + status);
if (status == 2) {
$('#btnPlay').addClass("btn-play-active");
currentDuration = player.getDuration();
console.log("CURRENT DURATION: " + currentDuration);
updateMetas(mediaButton.data('title'),section);
} else if (status == 4) {
clearInterval(mediaTimer);
mediaTimer = null;
$('#btnPlay').removeClass("btn-play-active");
$('#circProgress').circleProgress('value', 0);
}
}; |
+1 for @ghenry22 's fix. All you have to do is run a |
Good stuff - only downside is it currently always reports '0' as the duration of the resource. CDVSound should have |
stop will now behave the same when avplayer is used to play from a stream as when avaudioplayer is used to play from a local file.
seekto 0 then stop playing.
@dts - thanks for the suggestion, I have implemented this and hopefully also fixed for playback from local files as well. @purplecabbage - updated with implementation for stop() for compliance as requested. incremented the version number so that I can see which version is actually installed. if you install from my branch it should show version 1.0.3-dev when you run cordova plugin list. |
OK so I was not completely crazy. Many thanks to @ghenry22 and others. |
@Alkashi - nope you were quite right. I realised I had a different branch of the plugin installed myself to test some things for Android and it was giving me correct durations, Hence I bumped the version number so I could see what I have running! |
Not sure about soundcloud as I haven't tried to stream from them before. Whether you can seek in a stream or not would probably depend on the source and the meta data it provides and if that is enough for the avplayer underlying framework of Apples to be happy. One case I have come across on Android with some streams is that if the length of the stream is unknown it assigns it a length of 0 and then any attempt to seek or a stall in streaming and then resume will reset back to start, I haven't hit the same with iOS yet on those same streams but there are probably other quirks / scenarios where the iOS framework would have a similar issue. |
seekTo definitely works on streams from other sources, my primary test is the subsonic media server but I have tried other random sources like free sample streams from spotify which also worked for me. |
I'm having trouble hearing sound from a local mp3 on iOS. I can play a sound from a http:// address but not from the local file tree www/assets/titleMusic.mp3 Yet I don't hear any sound in this case. I'm new to iOS development but I did my best to trace through the code and no errors appeared and everything looked like it should be playing. |
It's trying to play a local file as a stream due to the path being used by the look of it. Please try using the cordova.file.* paths to get a cdvfile:// path to your audio file and play with that path, should resolve your issue and make sure that your app works properly across platforms and installs as the path is consistent. Check the file plugin for details on the file paths. Cheers
|
How Allow IOS Support Streaming? |
@nuruddinbadawi install my branch of the media plugin with this command: cordova plugin add https://github.com/ghenry22/cordova-plugin-media.git#avplayer_streaming That will get you the version with streaming support |
Skipping 'cordova-plugin-media' for android On Mon, Jan 4, 2016 at 4:14 AM, Gaven Henry notifications@github.com
|
update your android platform and try again |
in ios work fine but in android no can install this brush just in ios and let android use default Media Plugin and thanks On Mon, Jan 4, 2016 at 9:28 AM, Gaven Henry notifications@github.com
|
This reverts commit e7f9f04.
This will still intermittently fail test 19 but this is more to do with the success criteria of that rest. rate is applied and works.
@purplecabbage I have added rate control when streaming from a URL using avplayer and tested that it works. However test case 19 still fails in the automated suite but I believe that is due to the way media is loaded and the success evaluator on the test. To explain: The problem with the test is it plays for 4 seconds and expects the playback position to be >7 when playing at rate 2. This is fine when using the old avaudioplayer implementation as it will first download the entire file locally and then start paying immediately at rate 2, resulting in a final position generally around 8. When using avplayer though to stream properly we don't download the entire file in advance, we only start streaming when play is pushed so we now have a variance that throws the test out. If the file is slow to load from the source server for some reason we have a larger variance. GIven only 4 seconds play time there is not enough time for the stream to start buffering and play at 2x rate in order to reach the magic test passing number. This is completely normal and expected. If the length of the test run was extended to say 10 seconds you would find a lot less variance as it allows some time to buffer. If the mp3 source file is hosted on a local http server on the same machine running the emulator and tests it will also download almost instantly and pass the test. (I have tested this scenario). To test rate you could potentially play for 4 seconds at rate 1 and then play for 4 seconds at rate 2 and if the output of the second play is close to 2x the first play then you have success. Given that the tests are run one after the other they are likely to both have the same buffering time which mostly rules that out as a variable. I am not sure how to rewrite the test to do this though. If someone could help to rewrite the test then all tests will pass and this PR can finally get merged which would be fantastic! |
@ghenry22 Hey, I am not able to use the .m3u stream on iOS. I am getting following error. 2016-01-28 23:44:41.939 MPESG Radio[3073:44952] Failed to initialize AVAudioPlayer: The operation couldn’t be completed. (OSStatus error 1954115647.) |
@imsingh That doesn't really have anything to do with this pull request. I'm not sure what "MPESG Radio" is that is generating the error messages, never seen that one before. From my experience with the media plugin you need to give it a single file or single source URL to play, I don't think it will handle an m3u file with multiple items. No idea what it would do with an m3u with multiple entries. You could always grab the m3u file, strip out the list of items and then manage them as a playlist in your app code feeding them to the media plugin as sources appropriately. Oh also it looks like you are either loading the m3u file locally or using the main branch of the media plugin, not my branch with the streaming code as if the resource is loaded from a HTTP:// url with this branch then it will use avplayer not avaudioplayer. |
@ghenry22 MPESG Radio is just name of Cordova App and m3u file has a single URL entry. I Tried using the URL Directly again it is not working. However the plugin at https://github.com/keosuofficial/cordova-audio-stream-plugin works fine, but it has not implemented callbacks so I can't use it either. |
@imsingh where are you loading the m3u file from? If you are opening the m3u file from the local file system it will try to play it like a local file. If you open it from a http url then it will try to stream it. This is just the logic in the media plugin, it looks at the source prefix to determine if it should stream or play locally. If you haven't already done so I have a fork of the plugin here that you can install using the command below, this fork has the updated ios streaming code from this pull request build in. If you install from here and you're m3u file is being loaded from a url that starts with http or https you should see a message in the native console saying "playing stream with avplayer" instead of the normal message you see which is tied to "avaudioplayer". avaudioplayer is the original implementation that does not support streaming, avplayer is the framework implemented in this PR to add proper streaming support. cordova plugin add https://github.com/ghenry22/cordova-plugin-media.git#avplayer_streaming that's the best that I can think to recommend, hope it helps a bit! |
@purplecabbage sorry to bug you but if you could have a look at my previous comment that would be great, I really want to get this merged as it has been hanging for a long time and is starting to be difficult to maintain my fork and keep everything up to date. |
Good to go. Thanks for sharing your work @ghenry22 ! |
Sweet!! |
Awesome! Glad I could contribute something back to the project! |
My streaming player got much faster. Thank you. I originally tried with But didn't work. |
We are further and further off topic, but |
@purplecabbage comments on this PR could not possibly go further off topic :) Seems to have become defacto media plugin support for a while there. I had some odd permission issue on my mac initially and used sudo to install as well. No matter what I did always had that problem, probably because I installed something early on with Sudo and then all the permissions became a mess. Was resolved when I did a refresh of the OS on my mac and a clean install of all the tools without using sudo for anything. Doesn't seem to hurt anything either way though and all build tools etc work. |
Hello @purplecabbage |
Thanks @Taewa ! I was just curious. I wonder how often this happens?! Never on windows ... ;) |
hi @ghenry22, must say your work here is stellar! I have 2 issues with this plugin so far (i am using the latest pull from the main repo as your work is now merged there)
Any help here would be immensely appreciated @ghenry22 . Many many thanks! |
Hey! I can't use the "cordova plugin add https://github.com/ghenry22/cordova-plugin-media.git#avplayer_streaming" command, it says: Am I missing something? |
That branch ( and this pr ) has been merged. @ghenry22 's branch no longer exists. |
Thanks for the info! But adding that works in browser and emulated android device. But when i try on a real device, it takes around 20s to start playing the stream! Any tips? |
Hi use cordova plugin add cordova-plugin-media@2.2 solved slowness start |
This fixes 2 issues in the Cordova Jira, both with iOS. It enables streaming support for media playback from URLs while retaining the existing playback for local media.
The second part is the simply background thread the play function so it doesn't block the ui or lock up the app on load. Gets rid of some of those pesky warnings in xcode too.
Tested with simulator and on iphone 5 and iphone 6 and ipad 2, with iOS 8.1, 8.3 & 8.4
Please merge these and bump the version of the media plugin so I can stop installing them from my own fork :)
Fix for CB-57
Updated to use avplayer when url starts with http:// or https:// for
full streaming support.
All other urls will use the standard AVAudioplayer
Fix for CB-8222
Background thread on play to prevent locking during initial load of
media.