Skip to content
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

Add ability to play audio through notifications channel #19

Closed
trevburley opened this issue Jun 20, 2018 · 10 comments
Closed

Add ability to play audio through notifications channel #19

trevburley opened this issue Jun 20, 2018 · 10 comments

Comments

@trevburley
Copy link

On iOS when playing music in the background from any other app (Spotify for example) the music stops when you play some audio through this plugin and it doesn't resume when finished.

When using this plugin as a music player that is absolutely fine but when using it to play motivational audio clips (similar to Strava's pace time updates etc) it doesn't really work since Spotify (in this case) will pause.

I come from a native Android developer background so the code is a little foreign to me but if there is such a thing as being able to treat the audio as a 'notification' instead of 'music' for iOS that would be a nice addition.

I understand this may not be the purpose this plugin was designed, just hoping you are open to the idea!

@luanpotter
Copy link
Member

Hi, @thirddimensionstudios , this is a very welcome addition, for sure. Makes a lot of sense to have that option. However, I'm not sure how to implement it; I was not even aware it was an option. I might study this soon to see how to, but the lib is nothing but a thin wrapper around the native APIs; so the relevant code would be put here and here.

If you are acquainted with the native code, a PR is more than welcome. I can help with the dart wrapper if necessary (just add another parameter on the native end and we can add an optional parameter on Dart to match it).

@trevburley
Copy link
Author

trevburley commented Jun 20, 2018

Thanks @luanpotter.

For iOS I found this SO which may help (needs modernising to iOS11): https://stackoverflow.com/questions/28985942/best-method-to-play-an-short-audio-sound-on-ios-8-2

I'll have a play in the next few days but I've never coded in Obj-C so it will need checking over by someone capable! Java isn't a problem though.

@trevburley
Copy link
Author

Adding this to the top of the togglePlay method in the iOS code seems to fix the issue BUT it retains the first file you ask it to play:

AVAudioSession *session = [AVAudioSession sharedInstance]; if (session) { NSError *setCategoryError = nil; BOOL success = [session setCategory:AVAudioSessionCategoryAmbient error:&setCategoryError]; if (success) { NSError *activationError = nil; [session setActive:YES error:&activationError]; } else { NSLog(@"Failed to set music category to ambient. May interrupt other audio."); } }

Really out of my depth here with Obj C - any idea why that might happen?

@feroult
Copy link
Contributor

feroult commented Jul 21, 2018

@thirddimensionstudios trying to get this working...

Have you tried to use this functionality using AudioPlayers on Android?

@trevburley
Copy link
Author

@feroult I'm trying to get my hands on an Android device so that I can play around rather than just guess so not yet. I prefer using real devices for audio testing rather than simulators.

@ngocdaothanh
Copy link

Apple's Doc has a section "Checking Whether Other Audio Is Playing".

@Cretezy
Copy link

Cretezy commented Nov 9, 2018

I believe this would be fixed by using local notifications with a custom sound. In practice, Android and iOS only allow for 1 app to be playing sounds, however notifications with sound will play over them.

@luanpotter
Copy link
Member

Is this by any chance fixed by the new respectSilence flag?

@luanpotter
Copy link
Member

I believe this was fixed due to our recent PRs about respectSilence and notification handling. Please re-open if I'm mistaken.

@volgin
Copy link
Contributor

volgin commented Jan 19, 2020

As of 0.13.7 (on iOs):

respectSilence: true - sound is turned off as soon as the app goes into background.
respectSilence: false - sound continues as the app goes into background, but if another app is playing (I tried with Safari), both apps will play over each other. Starting/stopping music in Safari has no effect on this plugin - it keeps playing.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

6 participants