应用播放背景音频不起作用

Posted

技术标签:

【中文标题】应用播放背景音频不起作用【英文标题】:App plays background audio not working 【发布时间】:2013-04-26 16:07:29 【问题描述】:

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

使用下面的代码只能在按下主页按钮两次时处理远程控制事件。但是当应用程序关闭时无法在后台播放应用程序音频。那么,当按下主页按钮关闭应用程序时,我如何在后台模式下播放应用程序的 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];

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

感谢任何帮助。

谢谢

【问题讨论】:

【参考方案1】:

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

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

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

【讨论】:

非常感谢您的帮助。它就像一个魅力。我真的很感激。【参考方案2】:

为了更清楚,将以下方法添加到 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;
        

【讨论】:

【参考方案3】:

将此代码添加到 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);
    

在下面添加配置相同的图像:

【讨论】:

以上是关于应用播放背景音频不起作用的主要内容,如果未能解决你的问题,请参考以下文章

在后台应用程序不起作用时播放音频

Xml 音频列表播放器在移动设备上不起作用,并且自动播放不起作用

允许使用 Swift 的背景音频不起作用

音频自动播放不起作用html5

播放背景音频故障

为啥我的停止按钮在这个 javascript 音频播放器上不起作用?