【问题标题】:Run Sounds in background and with music playing在后台运行声音并播放音乐
【发布时间】:2013-11-24 03:38:25
【问题描述】:

我正在构建一个正在运行的应用程序,它会每隔几分钟通过音频通知用户他们需要开始跑步或开始步行。即使在使用 AVAudioPlayer 锁定屏幕的情况下,我也能够让声音在后台运行。

这是我所拥有的sn-ps:

ViewController.h

@property (nonatomic, strong) AVAudioPlayer *audioWalk;

ViewController.m

- (void)viewDidLoad{
    [super viewDidLoad];
    // Walk Audio File
    NSString *soundFile2 = [[NSBundle mainBundle] pathForResource:@"Walk" ofType:@"wav"];
    audioWalk = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:soundFile2] error:nil];

    // Load the audio into memory
    [audioWalk prepareToPlay];

    // Permit the timer to run in the background
    bgTask = 0;
    UIApplication  *app = [UIApplication sharedApplication];
    bgTask = [app beginBackgroundTaskWithExpirationHandler:^{
        [app endBackgroundTask:bgTask];
    }];

    // Prevent the application from going to sleep while it is running
    [UIApplication sharedApplication].idleTimerDisabled = YES;

    // Starts recieving remote control events and is the first responder
    [[UIApplication sharedApplication] beginReceivingRemoteControlEvents];
    [self becomeFirstResponder];

    // Plays audio
    [audioWarmup play];

}

我正在尝试弄清楚如何在不中断后台播放的音乐的情况下播放我的音频。此外,声音需要在后台和屏幕锁定时播放。任何帮助将不胜感激!

【问题讨论】:

    标签: ios objective-c audio


    【解决方案1】:

    我能够修复它。这是我在下面所做的。此解决方案允许音频文件在后台运行,即使在锁定屏幕的情况下,也不会中断或暂停来自其他应用程序(尤其是音乐)的音频。

    - (void)viewDidLoad{
        [super viewDidLoad];
    
        // Play audio even if lock screen is on, the with options allows audio from other applications to play without interruption
        [[AVAudioSession sharedInstance] setCategory: AVAudioSessionCategoryPlayback withOptions:AVAudioSessionCategoryOptionMixWithOthers error: nil];
        [[AVAudioSession sharedInstance] setActive: YES error:nil];
    
        // Walk Audio File
        NSString *soundFile2 = [[NSBundle mainBundle] pathForResource:@"Walk" ofType:@"wav"];
        audioWalk = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:soundFile2] error:nil];
    
        // Load the audio into memory
        [audioWalk prepareToPlay];
    
        // Permit the timer to run in the background
        bgTask = 0;
        UIApplication  *app = [UIApplication sharedApplication];
        bgTask = [app beginBackgroundTaskWithExpirationHandler:^{
            [app endBackgroundTask:bgTask];
        }];
    
        // Prevent the application from going to sleep while it is running
        [UIApplication sharedApplication].idleTimerDisabled = YES;
    
        // Starts receiving remote control events and is the first responder
        [[UIApplication sharedApplication] beginReceivingRemoteControlEvents];
        [self becomeFirstResponder];
    
        // Plays audio
        [audioWarmup play];
    
    }
    

    【讨论】:

    • setCategory 任务必须在所有其他内容之前使用选项 AVAudioSessionCategoryOptionMixWithOthers 定义,否则将无法正常工作。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-01-24
    • 1970-01-01
    • 2011-11-29
    • 2021-09-03
    相关资源
    最近更新 更多