1

即使通过按主页按钮关闭应用程序,也希望此应用程序选项在后台模式下播放音频。

使用下面的代码只能在按下主页按钮两次时处理远程控制事件。但是当应用程序关闭时无法在后台播放应用程序音频。那么,当按下主页按钮关闭应用程序时,如何在后台模式下播放应用程序的 mp3 类型音频,这是我的应用程序中的可听内容。

在 Info.plist 文件中,我添加了选项

所需的背景模式 应用播放音频

- (void) setupAudioSession {

AVAudioSession *audioSession = [AVAudioSession sharedInstance];

 // Specify that this object is the delegate of the audio session, so that this object's endInterruption method will be invoked when needed.

[audioSession setDelegate: self];

// Assign the Playback category to the audio session.

NSError *audioSessionError = nil;

//[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error:nil];

[audioSession setCategory: AVAudioSessionCategoryPlayback error: &audioSessionError];

if (audioSessionError != nil) {

    NSLog (@"Error setting audio session category.");
    return;
}

// Activate the audio session
[audioSession setActive: YES  error: &audioSessionError];

if (audioSessionError != nil) {

    NSLog (@"Error activating audio session during initial setup.");
    return;
}
}

- (void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
[[UIApplication sharedApplication] beginReceivingRemoteControlEvents];
[self becomeFirstResponder];
 }

- (void)viewWillDisappear:(BOOL)animated {
[super viewWillDisappear:animated];
[[UIApplication sharedApplication] endReceivingRemoteControlEvents];
[self resignFirstResponder];
 }

- (BOOL)canBecomeFirstResponder {
return YES;
 }

- (void)remoteControlReceivedWithEvent:(UIEvent *)event {
//if it is a remote control event handle it correctly
if (event.type == UIEventTypeRemoteControl) {
    if (event.subtype == UIEventSubtypeRemoteControlTogglePlayPause) {
        //[player play];

        [self playAction];
   // } else if (event.subtype == UIEventSubtypeRemoteControlPause) {
    //    [player pause];
    }  else if (event.subtype == UIEventSubtypeRemoteControlPreviousTrack) {
        [self rewButtonPressed];

    } else if (event.subtype == UIEventSubtypeRemoteControlNextTrack)
        [self ffwButtonPressed:nil];
}}

当按下主页按钮时,我在代码中缺少的应用程序的音频没有在后台播放。

感谢任何帮助。

谢谢

4

3 回答 3

2

您必须在 AppDelegate 的以下方法中设置音频会话。

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

然后请将音频会话作为实例对象,以便可以将其称为 self.youraudiosession。

于 2013-04-27T16:29:25.503 回答
0

Just to be more clear add below method to appDelegate.m

- (void) setupAudioSession {

         AVAudioSession *audioSession = [AVAudioSession sharedInstance];

        // Specify that this object is the delegate of the audio session, so that this object's endInterruption method will be invoked when needed.

        [audioSession setDelegate: self];

        // Assign the Playback category to the audio session.

        NSError *audioSessionError = nil;

        [audioSession setCategory: AVAudioSessionCategoryPlayback error: &audioSessionError];

        if (audioSessionError != nil) {

             NSLog (@"Error setting audio session category.");
             return;
        }

        // Activate the audio session
        [audioSession setActive: YES  error: &audioSessionError];

        if (audioSessionError != nil) {

             NSLog (@"Error activating audio session during initial setup.");
             return;
         }
        }

Now call  [self setupAudioSession]; from within didFinishLaunchingWithOptions.

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

          [self setupAudioSession];

          //cont. with usual code…
          self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
          // Override point for customization after application launch.
          self.viewController = [[YOURVIEWCONTROLLER alloc] initWithNibName:@"YOURVIEWCONTROLLER" bundle:nil];

          self.window.rootViewController = self.viewController;
          [self.window makeKeyAndVisible];

          return YES;
        }
于 2013-12-18T17:39:58.580 回答
0

将此代码添加到 AppDelegate.m

#import <AVFoundation/AVFoundation.h>
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
       AVAudioSession *audioSession = [AVAudioSession sharedInstance];
    BOOL ok;
    NSError *setCategoryError = nil;
    ok = [audioSession setCategory:AVAudioSessionCategoryPlayback
                             error:&setCategoryError];
    if (!ok) {
        NSLog(@"%s setCategoryError=%@", __PRETTY_FUNCTION__, setCategoryError);
    }
}

在下面添加配置相同的图像: 在此处输入图像描述

于 2017-06-21T16:34:32.153 回答