ios - 按下主页按钮时在后台播放音乐
<p><p>我想播放音乐背景,为此我在 info.plist 中进行了更改。我将应用程序不在后台运行设置为<em>NO</em>,<em>应用程序播放音频所需的后台模式</em>。</p>
<p>我在 AppDelegate 中添加了这个:</p>
<pre><code>- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
[ setDelegate:self];
[ setCategory:AVAudioSessionCategoryPlayback error:nil];
[ setActive:YES error:nil];
[ beginReceivingRemoteControlEvents];
}
- (void)applicationDidEnterBackground:(UIApplication *)application {
AVAudioSession *audioSession = ;
NSError *error = nil;
NSLog(@"Activating audio session");
if (!) {
NSLog(@"Unable to set audio session category: %@", error);
}
BOOL result = ;
if (!result) {
NSLog(@"Error activating audio session: %@", error);
}
[ beginReceivingRemoteControlEvents];
}
</code></pre>
<p>但是当我按下主页按钮时,音乐不再播放。请问我哪里错了?</p></p>
<br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
<p><p> <a href="/image/c3sEI.png" rel="noreferrer noopener nofollow"><img src="/image/c3sEI.png" alt="enter image description here"/></a> </p>
<p>并在 homeBtnEventMethod 上的 homeBtn 屏幕上播放音频</p>
<p>1.) 在 <strong>viewController.h</strong> <code>#import <AVFoundation/AVAudioPlayer.h></code></p>
<p>2.) <code>@property (nonatomic, strong) AVAudioPlayer *theAudio;</code></p>
<p>3.) 在 <strong>viewController.m</strong> 在 viewDidLoad</p>
<pre><code> NSError *error = nil;
self.theAudio = [ initWithContentsOfURL:soundURL error:&error];
</code></pre>
<p>4.) <code>- (IBAction)homeBtnTpd {
;
}</code></p></p>
<p style="font-size: 20px;">关于ios - 按下主页按钮时在后台播放音乐,我们在Stack Overflow上找到一个类似的问题:
<a href="https://stackoverflow.com/questions/32369391/" rel="noreferrer noopener nofollow" style="color: red;">
https://stackoverflow.com/questions/32369391/
</a>
</p>
页:
[1]