how to start to play music in locked screen in iPhone

前端 未结 2 1938
暗喜
暗喜 2021-02-10 15:03

I am using UILocalNotification to alert the user if app not running and MPMusicPlayerController to play iPod music and MPMoviePlayerController

2条回答
  •  长发绾君心
    2021-02-10 16:00

    @manju sorry i got late but it work for you.

     selectedDate = [timePicker date];
    

    it is selectedDate date object,at this time your notification will came.
    [NSDate date] by this you can find current time. and find different both time via.

     NSTimeInterval timeDifference =  [ selectedDate timeIntervalSinceDate:[NSDate date]];   
    

    Now when your are schedule your notification then .write this code

     UIApplication *app = [UIApplication sharedApplication];
     UIBackgroundTaskIdentifier bgTask = 0;
    
     NSTimer  *backgroundTimer = [NSTimer scheduledTimerWithTimeInterval:timeDifference    target:self  selector:@selector(backgroundTask) userInfo:nil repeats:NO];
    
    
        bgTask = [app beginBackgroundTaskWithExpirationHandler:^{
        [app endBackgroundTask:bgTask];
        }];
    
        -(void)backgroundTask
        {
            NSLog(@"Here you can play song via player ");
           }
    

    Don't forgot to add in app.infolist

    UIApplication BackgroundMode Audio it will solve your problem , play song when device is lock.

提交回复
热议问题