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

Allow background audio play on iOs #1

Open
pabloacastillo opened this issue Sep 6, 2019 · 7 comments
Open

Allow background audio play on iOs #1

pabloacastillo opened this issue Sep 6, 2019 · 7 comments

Comments

@pabloacastillo
Copy link

pabloacastillo commented Sep 6, 2019

Hi there, this port is great it just needs background audio when a iOs device is blocked or the screen turns off.

I saw the code at nstudio has been updated to run in this mode and seems to be an easy fix.
The six line change occurs in in ios/player.ts:

playFromUrl function change this:

let audioSession = AVAudioSession.sharedInstance();
let output = audioSession.currentRoute.outputs.lastObject.portType;

to this:

let audioSession = AVAudioSession.sharedInstance();
audioSession.setCategoryWithOptionsError(
  AVAudioSessionCategoryAmbient,
  AVAudioSessionCategoryOptions.DuckOthers
);
let output = audioSession.currentRoute.outputs.lastObject.portType;

and in playFromFile function change this:

let audioSession = AVAudioSession.sharedInstance();
let output = audioSession.currentRoute.outputs.lastObject.portType;
TNS_Player_Log('output', output);

to this:

let audioSession = AVAudioSession.sharedInstance();
audioSession.setCategoryWithOptionsError(
  AVAudioSessionCategoryAmbient,
  AVAudioSessionCategoryOptions.DuckOthers
);
let output = audioSession.currentRoute.outputs.lastObject.portType;
TNS_Player_Log('output', output);

I hope this helps someone else who has to dig around this issue.

check more information at nstudio/nativescript-audio#19

@mehyaa
Copy link
Owner

mehyaa commented Sep 6, 2019

Can you send a PR?

@pabloacastillo
Copy link
Author

pabloacastillo commented Nov 28, 2019

Will do after finishing my tests (university is keeping me busy).
New version use AVPlayer instead of AVAudioPlayer so now audio streaming is working on iOs.

@pabloacastillo
Copy link
Author

Pull request done.

@FlawaCLV
Copy link

FlawaCLV commented Mar 3, 2020

Audio doesn't work when I switch my iPhone to silent or lock it. Any idea how to solve this issue ?

@FlawaCLV
Copy link

FlawaCLV commented Mar 9, 2020

@mehyaa @pabloacastillo my iOS users are getting pissed by this... I'm quite stuck 🙈 Any idea ?

@pabloacastillo
Copy link
Author

Can you check this if it works?

In app/App_Resources/iOS/Info.plist

add

<key>UIBackgroundModes</key>
<array>
<string>audio</string>
</array>

You can google "UIBackgroundModes" for more details, but it might work.

Best of lucks!

@FlawaCLV
Copy link

@pabloacastillo thanks for your answer, I'd already tried that but no success.

But the good news is I finally managed to make it work! The solution was to add this code to my iOS Delegate. And I do not use your plugin actually, but nativescript-audio.

MyDelegate.prototype.applicationDidEnterBackground = function (application) {
    AVAudioSession.sharedInstance().setCategoryWithOptionsError(
        AVAudioSessionCategoryPlayAndRecord,
        AVAudioSessionCategoryOptions.DefaultToSpeaker
    );
};

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

No branches or pull requests

3 participants