如何开始在iPhone的锁定屏幕中播放音乐 [英] how to start to play music in locked screen in iPhone

查看:226
本文介绍了如何开始在iPhone的锁定屏幕中播放音乐的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 UILocalNotification 提醒用户应用程序是否未运行以及 MPMusicPlayerController 播放iPod音乐和 MPMoviePlayerController 播放无线电流网址。



根据Apple doc:
如果iphone被锁定且设备收到本地通知然后它将播放30秒的声音,当用户滑动滑块然后它将采用前景应用程序并发生两种情况


  1. 如果App未运行,则调用方法



    (BOOL)应用程序:(UIApplication *)应用程序didFinishLaunchingWithOptions:(NSDictionary *)launchOptions


  2. 如果App已在运行,则调用方法 -



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


我可以用这些方法完成我的任务。



问题:
我需要我的应用程序能够解决当iPhone被锁定并收到本地通知且应用程序在前台运行时,我的声音(可能是iPod音乐或流媒体网址,但不是30秒的本地通知声音)。



<参考应用程序:
闹钟高清以及应用程序商店中也可用的其他应用程序以这种方式工作正常但是当应用程序在后台运行时它们无法播放警报(我无法理解为什么? ?)



我也试过如何防止iphone从深度睡眠



阻止iPhone进入通过定期播放无声音乐深度睡眠,但如何通知应用通知已收到,所以开始播放实际音乐?以及如何开始播放新音乐?

解决方案

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

这篇关于如何开始在iPhone的锁定屏幕中播放音乐的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆