在iPhone上播放背景音频

3
我应该如何在应用程序运行时播放后台音频?
谢谢。

2
如何在我的 iPhone 游戏中播放背景音乐? - Matthew Flaschen
2个回答

9

好的。这是一个针对iOS4和iOS5背景音效的解决方案(绝对适用于iOS 5.0.1及以下版本),我只测试了AVPlayer,但它也应该适用于MPMusicPlayerController。

所需框架:

  • AVFoundation.framework
  • AudioToolbox.framework

在你的Info.plist中,为键UIBackgroundModes添加audio

MyAppDelegate.h中:

  • reference <AVFoundation/AVFoundation.h> & <AudioToolbox/AudioToolbox.h>
  • implement the protocol AVAudioSessionDelegate:

    @interface MyAppDelegate : NSObject <UIApplicationDelegate, AVAudioSessionDelegate>
    
  • define a method ensureAudio:

    // Ensures the audio routes are setup correctly
    - (BOOL) ensureAudio;
    
MyAppDelegate.m文件中:
  • implement the ensureAudio method:

    - (BOOL) ensureAudio
    {
        // Registers this class as the delegate of the audio session (to get background sound)
        [[AVAudioSession sharedInstance] setDelegate: self];  
    
        // Set category
        NSError *categoryError = nil;
        if (![[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error:&categoryError]) {
            NSLog(@"Audio session category could not be set"); 
            return NO;
        }
    
        // Activate session
        NSError *activationError = nil;
        if (![[AVAudioSession sharedInstance] setActive: YES error: &activationError]) {
            NSLog(@"Audio session could not be activated");
            return NO;
        }
    
        // Allow the audio to mix with other apps (necessary for background sound)
        UInt32 doChangeDefaultRoute = 1;
        AudioSessionSetProperty(kAudioSessionProperty_OverrideCategoryMixWithOthers, sizeof(doChangeDefaultRoute), &doChangeDefaultRoute);
    
        return YES;
    }
    
  • in the application:didFinishLaunchingWithOptions: method, before you assign the root view controller, run [self ensureAudio]:

    - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
    {
        // Configure audio session
        [self ensureAudio];
    
        // Add the navigation controller's view to the window and display.
        self.window.rootViewController = self.navigationController;
        [self.window makeKeyAndVisible];
    
        return YES;
    }
    
  • implement the AVAudioSessionDelegate methods like this:

    #pragma mark - AVAudioSessionDelegate
    
    - (void) beginInterruption
    {
    
    }
    
    - (void) endInterruption
    {
        // Sometimes the audio session will be reset/stopped by an interruption
        [self ensureAudio];
    }
    
    - (void) inputIsAvailableChanged:(BOOL)isInputAvailable
    {
    
    }
    
  • ensure that your app continues to run in the background. You can use the ol' [[UIApplication sharedApplication] beginBackgroundTaskWithExpirationHandler] if you want, but I think there are better ways.

  • play the actual audio (note I'm using ARC, that's why there are no release calls):

    NSURL * file = [[NSBundle mainBundle] URLForResource:@"beep" withExtension:@"aif"];    
    AVURLAsset * asset = [[AVURLAsset alloc] initWithURL:file options:nil];
    AVPlayerItem * item = [[AVPlayerItem alloc] initWithAsset:asset];
    __block AVPlayer * player = [[AVPlayer alloc]initWithPlayerItem:item];
    __block id finishObserver = [[NSNotificationCenter defaultCenter] addObserverForName:AVPlayerItemDidPlayToEndTimeNotification 
                                                                          object:player.currentItem 
                                                                           queue:[NSOperationQueue mainQueue] 
                                                                      usingBlock:^(NSNotification *note) {
        [[NSNotificationCenter defaultCenter] removeObserver:finishObserver];
    
        // Reference the 'player' variable so ARC doesn't release it until it's
        // finished playing.
        player = nil;
    }];
    
    // Trigger asynchronous load
    [asset loadValuesAsynchronouslyForKeys:[NSArray arrayWithObject:@"tracks"] completionHandler:^{
        // Start playing the beep (watch out - we're not on the main thread here)!
        [player play];
     }];
    
  • And it shooooooooooooould work!


如果您正在使用MPMusicPlayerController播放音乐,请确保使用[MPMusicPlayerController iPodMusicPlayer],而不是使用[MPMusicPlayerController applicationMusicPlayer]。此外,请参阅此处:https://dev59.com/SUnSa4cB1Zd3GeqPPZzP,它将向您展示如果您不希望用户在启动您的应用程序时停止播放任何音乐,则该怎么做。 - Andy Weinstein
在实际设备上测试这种行为,模拟器无法正常工作。但实际设备上可以正常运作。 - Webdevotion

0

如果您的应用程序还用于录音,请不要忘记将setCategory更改为AVAudioSessionCategoryPlayAndRecord。否则,您将无法录制

[[AVAudioSession sharedInstance] setCategory: AVAudioSessionCategoryPlayAndRecord error:&setCategoryErr];

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接