【问题标题】:Audio isn't playing in background mode音频未在后台模式下播放
【发布时间】:2015-03-10 05:08:13
【问题描述】:

当我的应用程序进入后台模式时,我想在5 seconds 之后播放“音频”。 NSTimer 触发正确。 5 秒后我收到了NSLog(@"repeat");。但是,有些音频无法播放。我在我的目标中启用了背景模式。我尝试了许多其他解决方案,在stackoverflow 中找到,但没有运气。任何人都可以为我提供正确的解决方案。

在我的appdelegate.h 文件中:

#import <UIKit/UIKit.h>
#import <AVFoundation/AVFoundation.h>

@interface AppDelegate : UIResponder <UIApplicationDelegate>
{
    NSTimer* timer;
}

@property (strong, nonatomic) UIWindow *window;

@property (nonatomic, strong) NSString *musicName;
@property (nonatomic, strong) AVAudioPlayer *audioPlayer;

在我的appdelegate.m 文件中:

#import "AppDelegate.h"

@interface AppDelegate ()

@end

@implementation AppDelegate

@synthesize
musicName,
audioPlayer;

-(void) playAlarmSound
{
    musicName = @"note3BreakOf.mp3";
    // Construct URL to sound file
    NSString *path = [NSString stringWithFormat:@"%@/%@", [[NSBundle mainBundle] resourcePath], musicName];
    NSURL *soundUrl = [NSURL fileURLWithPath:path];

    // Create audio player object and initialize with URL to sound
    audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:soundUrl error:nil];
}

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    [self playAlarmSound];
    return YES;
}

-(void)methodRunAfterBackground
{
    [audioPlayer play];
    [timer invalidate];
    NSLog(@"repeat");
}

- (void)applicationDidEnterBackground:(UIApplication *)application
{
    UIApplication *app = [UIApplication sharedApplication];

    //create new uiBackgroundTask
    __block UIBackgroundTaskIdentifier bgTask = [app beginBackgroundTaskWithExpirationHandler:^{
        [app endBackgroundTask:bgTask];
        bgTask = UIBackgroundTaskInvalid;
    }];

    //and create new timer with async call:
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        //run function methodRunAfterBackground
        timer = [NSTimer scheduledTimerWithTimeInterval:5 target:self selector:@selector(methodRunAfterBackground) userInfo:nil repeats:NO];
        [[NSRunLoop currentRunLoop] addTimer:timer forMode:NSDefaultRunLoopMode];
        [[NSRunLoop currentRunLoop] run];
    });
}

【问题讨论】:

  • @David Karlsson,感谢您的评论。但恐怕问题和答案都在 SWIFT 中。
  • 您是否将“应用播放音频”添加到您的 plist 中?
  • 是的,我做到了。当我在我的目标中启用Background Modes 时,它会给我一个选项,我在列表中勾选Audio &amp; AirPlay。然后App plays audio自动添加到plist中的Required background modes中。
  • 与论坛网站不同,我们不使用“谢谢”、“感谢任何帮助”或Stack Overflow 上的签名。请参阅“Should 'Hi', 'thanks,' taglines, and salutations be removed from posts?。顺便说一句,这是“提前致谢”,而不是“致谢”。

标签: ios objective-c nstimer avaudioplayer


【解决方案1】:

您可以通过使应用处于活动背景来做到这一点。我在我的应用程序中这样做。我正在使用位置管理器 api 使应用程序在后台处于活动状态。然后在计时器的帮助下开始在后台播放音频。您可以使用此链接https://github.com/voyage11/Location 使应用程序在后台处于活动状态。希望这会有所帮助。

【讨论】:

    【解决方案2】:

    当我的应用进入后台模式时,我想在 5 秒后播放“音频”

    嗯,你不能。如果您的应用在进入后台时主动播放音频,那么当它进入后台时,它就会暂停 - 无论您的后台设置如何。您的计时器停止,应用进入睡眠状态。

    (许多人通过在应用程序进入后台时播放“无声”曲目来解决此问题,以便应用程序此时正在播放某些内容,并且可以在后台运行。)

    【讨论】:

    • 你不必积极地播放音频,你只需要有一个活跃的AVAudioSession(带有类别播放或playAndRecord)。
    • @JohnScala 不一定。您的会话在进入后台时可能处于活动状态,但您仍可能无法在后台开始播放。
    • 这不是我的经验,Apple 明确声明“您的应用程序在进入后台之前必须有一个活动的音频会话。” developer.apple.com/library/archive/qa/qa1668/_index.html
    • @JohnScala 是的,有一个活跃的音频会话是必要条件。但这并不总是充分条件。
    【解决方案3】:

    您需要将“playsAudio”添加到您的 plist 并设置

    • AVAudioSession sharedInstance 类别为:AVAudioSessionCategoryPlayback
    • AVAudioSession sharedInstance setActive:是的
    • UIApplication sharedApplication beginReceivingRemoteControlEvents

    似乎其中一些可能已被弃用,请查看here

    在 Objective-C 中:

    [[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error:nil];
    [[AVAudioSession sharedInstance] setActive: YES error: nil];
    [[UIApplication sharedApplication] beginReceivingRemoteControlEvents];
    

    【讨论】:

    • 感谢您的解决方案。但它不起作用。我把它放在applicationDidEnterBackgrounddidFinishLaunchingWithOptionsmethodRunAfterBackground 方法的开头。但是回到后台模式后它不会播放音乐。 :(
    • 我检查了applicationDidEnterBackground,并在didFinishLaunchingWithOptions 中播放音乐。然后启动应用程序后音乐播放良好。所以,音频代码是完美的。但是有些背景模式无法播放音乐。我NSLogapplicationDidEnterBackground 中有一些东西。虽然,它是打印日志。
    猜你喜欢
    • 2015-10-14
    • 1970-01-01
    • 1970-01-01
    • 2012-02-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多