iphone - 在后台播放音频

标签 iphone objective-c ios

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

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

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

最佳答案

您应该启动 AVAudioSession 并在主 plist 中声明您的应用程序在后台播放音乐。

在 didFinishLaunchingWithOptions 中:

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

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

在主 plist 中添加:必需的背景模式 - 应用播放音频

关于iphone - 在后台播放音频,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12108228/

相关文章:

ios - 向上滚动时如何撤消 View Y 更改?

ios - 访问在界面生成器中创建的工具栏

ios - iPhone应用程序在退出事件状态后崩溃(核心数据删除后)

objective-c - 将 C 前/后递增/递减与 Objective-C 点运算符混合工作?

ios - 将mapkit注释对象转换为mapbox注释对象

ios - 应用程序进入后台后每 n 分钟获取一次用户位置

iphone - 如何判断何时删除 subview UIView

ios - 子类化 UIViewController,何时以及何时不

iphone - 适用于 iPad 的限制应用程序

c# - 在 mac/win 上以编程方式访问 chrome 和 firefox 密码?