iOS AVPlayer issue - ios

I am new iOS developer .
I am trying to make the play/pause buttons in the now playing bar (home screen bottom bar) simulate the play/pause buttons in my app .
any help please .
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback
error:nil];
[[AVAudioSession sharedInstance] setActive:YES
error:nil];
[[UIApplication sharedApplication] beginReceivingRemoteControlEvents];
[self.player play];
UIBackgroundTaskIdentifier newTaskId = UIBackgroundTaskInvalid;
if([player play]){
newTaskId = [[UIApplication sharedApplication] beginBackgroundTaskWithExpirationHandler:NULL];
bgTask = [[UIApplication sharedApplication] beginBackgroundTaskWithExpirationHandler:^(void) {
[[UIApplication sharedApplication] endBackgroundTask: bgTask];
bgTask = UIBackgroundTaskInvalid;
}];
//Your bg code here
[[UIApplication sharedApplication] endBackgroundTask: bgTask];
bgTask = UIBackgroundTaskInvalid;

After calling:
[[UIApplication sharedApplication] beginReceivingRemoteControlEvents];
in your app delegate you will receive events when the buttons are pressed... handle them like this:
-(void)remoteControlReceivedWithEvent:(UIEvent *)event
{
switch(event.subtype)
{
case UIEventSubtypeRemoteControlPause:
[player pause];
break;
case UIEventSubtypeRemoteControlPlay:
[player play];
break;
}
}
And add other cases for next/prev/etc....

Related

background task is not working

I am trying to execute my code whenever my app enters into background. But my code is not working. Here is my following code:
- (void)applicationDidEnterBackground:(UIApplication *)application {
__block UIBackgroundTaskIdentifier bgTask;
NSLog(#"enter in background task");
UIApplication *app = [UIApplication sharedApplication];
NSLog(#"app is %#",app);
bgTask = [app beginBackgroundTaskWithExpirationHandler:^{
NSLog(#"called function");
[app endBackgroundTask:bgTask];
bgTask = UIBackgroundTaskInvalid;
}]; }
But my code is not working. As it does not print called function.
Thanks in advance.

how to call a method by using nstimer in background state

I want to fetch service for every 15 mins,so i am using NSTimer.It is working fine.But how to call same service while app is in background state by using nstimer.Nstimer is not working in background state.Please suggest me.
//when the application is about to move from active to inactive state.
- (void)applicationWillResignActive:(UIApplication *)application
{
[self sendBackgroundLocationToServer];
}
- (void) sendBackgroundLocationToServer
{
UIBackgroundTaskIdentifier bgTask = UIBackgroundTaskInvalid;
bgTask = [[UIApplication sharedApplication]
beginBackgroundTaskWithExpirationHandler:^{
[[UIApplication sharedApplication] endBackgroundTask:bgTask];
}];
//Start Timer
[self startTimer];
//Close the task
if (bgTask != UIBackgroundTaskInvalid)
{
[[UIApplication sharedApplication] endBackgroundTask:bgTask];
}
}

How to reinitialize app after backgrounding

-(void) closeUpShopBeforeBackgrounding {
// this listen to the UIApplicationDidEnterBackgroundNotification
[self beginBackgroundUpdateTask];
// do some closing stuff
[self endBackgroundUpdateTask]; }
- (void) beginBackgroundUpdateTask {
self.backgroundTask = [[UIApplication sharedApplication] beginBackgroundTaskWithExpirationHandler:^{
[self endBackgroundUpdateTask];
}]; }
- (void) endBackgroundUpdateTask {
[[UIApplication sharedApplication] endBackgroundTask: self.backgroundTask];
self.backgroundTask = UIBackgroundTaskInvalid; }
This code is working fine for me, but when I foreground the app again, its dead in the water. Its as if the viewDidLoads are not firing, and nothing is happening.
Is there anything I need to do when the app foregrounds to kick it back into life?

call stopVideoCapture method in Background thread - iOS

i'm making video capture function with overlay view. And now i want to call stopVideoCapture method in UIApplicationDidEnterBackgroundNotification. but the "stopVideoCapture" method is always failed with error message “/var/mobile/Media/PhotoData/takingvideo activity indicator already cleared”.
please tell me what's the problem.
following is the code invoked in UIApplicationDidEnterBackgroundNotification
UIApplication *app = [UIApplication sharedApplication];
self.bgTask = [app beginBackgroundTaskWithExpirationHandler:^{
[app endBackgroundTask:self.bgTask];
self.bgTask = UIBackgroundTaskInvalid;
}];
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
if (isRecording) {
dispatch_async(dispatch_get_main_queue(), ^{
[self.imagePickerController stopVideoCapture];
});
}
[NSThread sleepForTimeInterval:5];
[app endBackgroundTask:self.bgTask];
self.bgTask = UIBackgroundTaskInvalid;
});

If the phone rings while playing in background mode

I'm making an iphone app(Live radio app).
My app is supported background mode.
But, if the phone rings when radio app is playing in background mode, my app stop with errors .
MP AVAudioSessionDelegateMediaPlayerOnly end interruption. Interruptor <Phone> category <completed> resumable <0>, _state = 6
MP endInterruptionFromInterruptor :: resuming playback
So, I modified my code, but useless.
I'll add my code. Please tell me my faults. Thanks.
AppDelegate.h
#interface AppDelegate : UIResponder <UIApplicationDelegate, AVAudioSessionDelegate>
#property (assign, nonatomic) UIBackgroundTaskIdentifier bgTask;
... ...
#end
AppDelegate.m
- (void)applicationDidEnterBackground:(UIApplication *)application
{
bgTask = [application beginBackgroundTaskWithExpirationHandler:^{
// Clean up any unfinished task business by marking where you.
// stopped or ending the task outright.
[application endBackgroundTask:bgTask];
bgTask = UIBackgroundTaskInvalid;
}];
// Start the long-running task and return immediately.
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
// Do the work associated with the task, preferably in chunks.
[application endBackgroundTask:bgTask];
bgTask = UIBackgroundTaskInvalid;
});
}
- (void)applicationDidBecomeActive:(UIApplication *)application
{
bgTask = [application beginBackgroundTaskWithExpirationHandler:^{
[application endBackgroundTask:bgTask];
bgTask = UIBackgroundTaskInvalid;
}];
}
viewController.m
[[AVAudioSession sharedInstance] setCategory: AVAudioSessionCategoryPlayback error: nil];
moviePlayer = [[MPMoviePlayerController alloc] init];
[moviePlayer setContentURL:... m3u8];
[moviePlayer play];
you need to add this:
[[UIApplication sharedApplication] beginReceivingRemoteControlEvents];

Resources