Iphone 在后台播放音频

Iphone 在后台播放音频,iphone,objective-c,ios,Iphone,Objective C,Ios,我在表视图中显示媒体层次结构。当我在表视图中点击歌曲时,它会使用MPMoviePlayerViewController播放歌曲。但当我点击“完成”按钮时,声音停止播放。我创建了以下代码: NSURL *songUrl=[operationControl getSong:stringId]; mediaPlayerController = [[MPMoviePlayerViewController alloc] initWithContentURL:songUrl];

我在表视图中显示媒体层次结构。当我在表视图中点击歌曲时,它会使用MPMoviePlayerViewController播放歌曲。但当我点击“完成”按钮时,声音停止播放。我创建了以下代码:

        NSURL *songUrl=[operationControl getSong:stringId];
    mediaPlayerController = [[MPMoviePlayerViewController alloc] initWithContentURL:songUrl];
    [self presentModalViewController:mediaPlayerController animated:YES];
    [[mediaPlayerController moviePlayer] play];

我想浏览媒体层次结构以及在后台播放歌曲。我如何才能做到这一点?

听起来您的音频会话设置不正确。从

例如,当使用默认音频会话时,当自动锁定周期超时且屏幕锁定时,应用程序中的音频停止。如果要确保在锁定屏幕的情况下继续播放,请在应用程序的初始化代码中包含以下行:

NSError *setCategoryErr = nil;
NSError *activationErr  = nil;
[[AVAudioSession sharedInstance] setCategory: AVAudioSessionCategoryPlayback error:&setCategoryErr];
[[AVAudioSession sharedInstance] setActive:YES error:&activationErr];

AVAudioSessionCategoryPlayback类别确保在屏幕锁定时继续播放。激活音频会话将使指定的类别生效。

听起来您没有正确设置音频会话。从

例如,当使用默认音频会话时,当自动锁定周期超时且屏幕锁定时,应用程序中的音频停止。如果要确保在锁定屏幕的情况下继续播放,请在应用程序的初始化代码中包含以下行:

NSError *setCategoryErr = nil;
NSError *activationErr  = nil;
[[AVAudioSession sharedInstance] setCategory: AVAudioSessionCategoryPlayback error:&setCategoryErr];
[[AVAudioSession sharedInstance] setActive:YES error:&activationErr];

AVAudioSessionCategoryPlayback类别确保在屏幕锁定时继续播放。激活音频会话将使指定的类别生效。

您应该启动音频会话,并在主列表中声明您的应用程序在后台播放音乐


在didFinishLaunchingWithOptions中:

  // Setup audio session
  AVAudioSession *sharedSession = [AVAudioSession sharedInstance];
  [sharedSession setCategory:AVAudioSessionCategoryPlayback error:nil];
在ApplicationIDBecomeActive中:

  [sharedSession setActive:YES error:nil]; // FIXME: Error handling

在main plist add:Required background modes-应用程序播放音频

中,您应该启动音频会话,并在main plist中声明您的应用程序在后台播放音乐


在didFinishLaunchingWithOptions中:

  // Setup audio session
  AVAudioSession *sharedSession = [AVAudioSession sharedInstance];
  [sharedSession setCategory:AVAudioSessionCategoryPlayback error:nil];
在ApplicationIDBecomeActive中:

  [sharedSession setActive:YES error:nil]; // FIXME: Error handling

在main plist add:Required background modes-App plays audio

是的,我没有设置音频会话,但不知道该设置在哪里?是的,我没有设置音频会话,但不知道该设置在哪里?DidFinishLaunchingwith options:?@VXtreme它在你的AppDelegate.m文件中。此外,如果您正在播放音频,为什么要使用电影播放器?我的意思是应用程序:didFinishLaunchingWithOptions:@nsPostWhenId:我应该同时播放音频和视频didFinishLaunchingWithOptions:?@VXtreme它在您的AppDelegate.m文件中。此外,如果您正在播放音频,为什么要使用电影播放器?我的意思是应用程序:didFinishLaunchingWithOptions:@nsPostWhenId:我应该同时播放音频和视频