应用程序在后台时的IOS AV播放器

时间:2015-09-08 03:44:09

标签: ios background location avplayer

问候

我想在调用位置管理器委托时播放音频。这在应用程序处于前台时有效。但它不会在应用程序处于后台时播放。代表被召唤。 我正在打印记录2(背景)

的应用程序状态

我正在使用iPhone 6 OS 8.2 Xcode 6.3.2

info.plist CustomIOS目标属性

所需的背景模式 第0项 - 应用程序使用AirPlay播放音频或流音频/视频

请让我知道可能是什么问题

test.h

@property(强,非原子)AVAudioPlayer * backgroundMusicPlayer;

test.m

- (void)locationManager:(CLLocationManager *)manager
      didDetermineState:(CLRegionState)state forRegion:(CLRegion *)region
{
    UIApplicationState state = [UIApplication sharedApplication].applicationState;
    NSLog(@"app state %ld",state);

    NSLog(@"Playing sound");

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

    // Create audio player with background music
    NSString *backgroundMusicPath = [[NSBundle mainBundle] pathForResource:@“mysong” ofType:@"caf"];
    NSURL *backgroundMusicURL = [NSURL fileURLWithPath:backgroundMusicPath];

    self.backgroundMusicPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:backgroundMusicURL error:nil];
    self.backgroundMusicPlayer.delegate = self;  // We need this so we can restart after interruptions
    //   self.backgroundMusicPlayer.numberOfLoops = -1; // Negative number means loop forever

    [self.backgroundMusicPlayer prepareToPlay];
    [self.backgroundMusicPlayer play];

}

0 个答案:

没有答案