【问题标题】:playing background audio on iphone在 iPhone 上播放背景音频
【发布时间】:2009-05-25 20:28:16
【问题描述】:

如何在我的应用程序运行时播放背景音频?

谢谢。

【问题讨论】:

标签: iphone audio


【解决方案1】:

好的。这是 iOS4 和 iOS5 上的背景声音解决方案(绝对适用于 iOS 5.0.1),我仅使用 AVPlayer 对其进行了测试。它应该也适用于 MPMusicPlayerController。

所需的框架:

  • AVFoundation.framework
  • AudioToolbox.framework

在您的Info.plist 中,为密钥UIBackgroundModes 添加audio

MyAppDelegate.h:

  • 参考<AVFoundation/AVFoundation.h> & <AudioToolbox/AudioToolbox.h>
  • 实现协议AVAudioSessionDelegate:

    @interface MyAppDelegate : NSObject <UIApplicationDelegate, AVAudioSessionDelegate>
    
  • 定义一个方法ensureAudio:

    // Ensures the audio routes are setup correctly
    - (BOOL) ensureAudio;
    

MyAppDelegate.m:

  • 实现ensureAudio 方法:

    - (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;
    }
    
  • application:didFinishLaunchingWithOptions: 方法中,在分配根视图控制器之前,运行[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;
    }
    
  • 像这样实现AVAudioSessionDelegate 方法:

    #pragma mark - AVAudioSessionDelegate
    
    - (void) beginInterruption
    {
    
    }
    
    - (void) endInterruption
    {
        // Sometimes the audio session will be reset/stopped by an interruption
        [self ensureAudio];
    }
    
    - (void) inputIsAvailableChanged:(BOOL)isInputAvailable
    {
    
    }
    
  • 确保您的应用程序继续在后台运行。如果您愿意,可以使用 ol'[[UIApplication sharedApplication] beginBackgroundTaskWithExpirationHandler],但我认为还有更好的方法。

  • 播放实际音频(注意我使用的是 ARC,这就是没有 release 调用的原因):

    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];
     }];
    
  • 它太棒了!

【讨论】:

  • 如果您使用 MPMusicPlayerController 来实际播放音乐,请务必使用 [MPMusicPlayerController iPodMusicPlayer],而不是使用 [MPMusicPlayerController applicationMusicPlayer]。还请看这里:stackoverflow.com/questions/1507541/… 如果您不希望您的应用程序在用户启动您的应用程序时停止播放任何音乐,它将告诉您该怎么做
  • 在物理设备上测试此行为。它在我的模拟器中不起作用。它确实适用于物理设备。
【解决方案2】:

如果您还使用您的应用进行录音 - 请不要忘记将 setCategory 更改为 AVAudioSessionCategoryPlayAndRecord。在其他情况下,您将无法录制

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-04-30
    • 2021-11-03
    • 1970-01-01
    • 2020-04-07
    • 2016-08-30
    相关资源
    最近更新 更多