且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何开始在iPhone的锁定屏幕中播放音乐

更新时间:2023-12-04 08:23:58

@manju对不起,我迟到了,但它适合你。

  selectedDate = [timePicker date]; 

选择日期对象,此时您的通知将会到来。

[NSDate date] 由此您可以找到当前时间。并通过它找到不同的时间。

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

现在,当您安排通知时,请写入。编写此代码

  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];
}];

- (无效)backgroundTask
{
NSLog(@这里你可以通过播放器播放歌曲);
}

不要忘记添加app.infolist



UIApplication BackgroundMode音频
它将解决您的问题,在设备锁定时播放歌曲。


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

According to Apple doc: if iphone is locked and device receives local notification then it will play 30 second sound and when user slides the slider then it will take app in foreground and two scenarios take place

  1. If App not running then calls method

    (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions

  2. If App already running then calls method -

    (void)application:(UIApplication *)app didReceiveLocalNotification:(UILocalNotification *)notify

and i can perform my task in these methods.

Problem : I need my application to be able to play my sound (it may be iPod music or stream url but not local notification sound for 30 sec) when the iPhone is locked and local notification received and app is running in foreground.

Reference App : "Alarm Clock HD" and other apps also available in app store are working fine in this way but these are not able to play alarm when app is running in background (i could not understand why???)

I also tried how-to-prevent-iphone-from-deep-sleeping :

which prevent the iPhone from deep sleep by playing silent music periodically but how to notify app that notification has received so start to play "actual music" ? And how to start to play new music?

@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.