nativescript-videoplayer
nativescript-videoplayer copied to clipboard
Let the video keep playing in background
I'd like the sound of the video to keep playing in the background, even if I close the app (just as a music app would do). Is there a way to achieve this?
Thanks in advance!
Hey there, you need this:
import * as application from 'tns-core-modules/application';
declare var UIResponder: any, UIApplicationDelegate: any, UIApplication: any, AVAudioSession: any, AVAudioSessionCategoryPlayback: any;
class MyDelegate extends UIResponder {
public static ObjCProtocols = [UIApplicationDelegate];
public applicationDidFinishLaunchingWithOptions(application: UIApplication, launchOptions: any): boolean {
let audioSession = AVAudioSession.sharedInstance();
try {
audioSession.setCategoryError(AVAudioSessionCategoryPlayback);
audioSession.setActiveError(true);
console.log("audioSession category set and active");
} catch(err) {
console.log("setting audioSession category failed");
}
return true;
}
}
application.ios.delegate = MyDelegate;
I have that in a videoaudio.ts utility file, then import it into app.module, if youre using angular.
Thanks for the quick reply. However, I tested your code in Playground and it throws an error:
https://play.nativescript.org/?template=play-ng&id=z8f9A6
[iPhone msch]: 2019-03-11 16:52:40.951 nsplaydev[4892:844918] ***** Fatal JavaScript exception - application has been terminated. *****
2019-03-11 16:52:40.952 nsplaydev[4892:844918] Native stack trace:
1 0x101207370 NativeScript::reportFatalErrorBeforeShutdown(JSC::ExecState*, JSC::Exception*, bool)
2 0x101243c20 -[TNSRuntime executeModule:referredBy:]
3 0x10107b3c8 -[UIApplication(PlayLiveSync) swizzled_executeModule:]
4 0x100ae73f0
5 0x1da8d6bb4 <redacted>
2019-03-11 16:52:40.954 nsplaydev[4892:844918] JavaScript stack trace:
2019-03-11 16:52:40.954 nsplaydev[4892:844918] 1 NSStringFromClass@file:///app/tns_modules/tns-core-modules/application/application.js:275:87
2 start@file:///app/tns_modules/tns-core-modules/application/application.js:275:87
3 run@file:///app/tns_modules/tns-core-modules/application/application.js:303:10
4 bootstrapNativeScriptApp@file:///app/tns_modules/nativescript-angular/platform-common.js:188:26
5 bootstrapApp@file:///app/tns_modules/nativescript-angular/platform-common.js:106:38
6 bootstrapModule@file:///app/tns_modules/nativescript-angular/platform-common.js:90:26
7 anonymous@file:///app/main.js:11:57
8 evaluate@[native code]
9 moduleEvaluation@[native code]
10 promiseReactionJob@[native code]
2019-03-11 16:52:40.954 nsplaydev[4892:844918] JavaScript error:
file:///app/tns_modules/tns-core-modules/application/application.js:275:87: JS ERROR Error: Value is not a class.
2019-03-11 16:52:40.956 nsplaydev[4892:844918] PlayLiveSync: Uncaught Exception
2019-03-11 16:52:40.957 nsplaydev[4892:844918] *** JavaScript call stack:
(
)
2019-03-11 16:52:40.957 nsplaydev[4892:844918] *** Terminating app due to uncaught exception 'NativeScript encountered a fatal error: Error: Value is not a class.
at
1 NSStringFromClass@file:///app/tns_modules/tns-core-modules/application/application.js:275:87
2 start@file:///app/tns_modules/tns-core-modules/application/application.js:275:87
3 run@file:///app/tns_modules/tns-core-modules/application/application.js:303:10
4 bootstrapNativeScriptApp@file:///app/tns_modules/nativescript-angular/platform-common.js:188:26
5 bootstrapApp@file:///app/tns_modules/nativescript-angular/platform-common.js:106:38
6 bootstrapModule@file:///app/tns_modules/nativescript-angular/platform-common.js:90:26
7 anonymous@file:///app/main.js:11:57
8 evaluate@[native code]
9 moduleEvaluation@[native code]
10 promiseReactionJob@[native code]
', reason: '(null)'
*** First throw call stack:
(0x1dae8dea4 0x1da05da50 0x1012077b4 0x101243c20 0x10107b3c8 0x100ae73f0 0x1da8d6bb4)
libc++abi.dylib: terminating with uncaught exception of type NSException
2019-03-11 16:52:40.957 nsplaydev[4892:844918] PlayLiveSync: Uncaught Exception
@davecoffin Any suggestions on how to resolve the above-mentioned error? I've linked the NS Playground if you want to have a look at the code or try it out for yourself.
Try it in a real app. Not the playground
Actually, the provided solution doesn't work. @ManuelSch did you manage to make it work?