2013-04-10 146 views
2

有一个名为IHeartRadio的应用程序,它可让您设置一个睡眠定时器,该定时器将在指定的时间间隔后关闭音频。在后台停止音频

您首先选择要收听的电台,然后选择一段时间后再停止播放。该应用程序不需要在前台发生这种情况。

应用程序在背景中时如何停止音频?

一个理论是用无声音频设置UILocalNotification。然后,UILocalNotification将接管设备的音频,实际上可以消除正在播放的任何音频。这没有用。

定时器在背景中不起作用,这在后台基于时间的行为方面没有多大意义。

回答

0

您可以在您的AppDelegate中使用applicationDidEnterBackground事件来处理应用程序进入后台并停止使用此方法的音频。

- (void)applicationDidEnterBackground:(UIApplication *)application 
{ 
    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later. 
    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits. 

    // add your audio stopping code .. 
} 
+0

也许我不清楚:我想停止音频后,应用程序已经在后台,而不是在它的方式背景。 – 2013-04-11 01:56:33

1

When the UIBackgroundModes key contains the audio value, the system’s media frameworks automatically prevent the corresponding app from being suspended when it moves to the background. As long as it is playing audio or video content, the app continues to run in the background. However, if the app stops playing the audio or video, the system suspends it.

iOS App Programming Guide

+0

嗯。根据我的经验,即使我在应用程序的plist中的“所需的背景模式”下声明了“应用程序播放音频”,NSTimer也不工作。如果不是UILocalNotification或NSTimer,我还能采取其他基于时间的行动吗? – 2013-04-11 02:00:49

+0

我想这可能是因为我的应用没有播放任何音频。我将尝试将沉默与播放类别混合,看看是否有效。 – 2013-04-11 21:32:55