【问题标题】:Set MPNowPlayingInfoCenter with other background audio playing设置 MPNowPlayingInfoCenter 与其他背景音频播放
【发布时间】:2016-04-06 10:20:08
【问题描述】:

我正在尝试使用 MPMoviePlayerController 为 Swift 中的 iOS 应用播放视频。

我的目标是能够用苹果音乐之类的东西播放系统音乐,然后打开我的应用程序并混合音频,但我希望我的应用程序能够控制MPNowPlayingInfoCenter

如何在设置 MPNowPlayingInfoCenter 的同时使用 AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryPlayback, withOptions: .MixWithOthers)

Google 地图在设置 MPNowPlayingInfoCenter 时会混入音频。以下是我尝试设置 MPNowPlayingInfoCenter 的方式:

func setMeta(){
    UIApplication.sharedApplication().beginReceivingRemoteControlEvents()
    self.becomeFirstResponder()
    if let player = PlayWorkoutViewController.player{
        let coverArt = MPMediaItemArtwork(image: UIImage(named: "AlbumArt")!)
        let dict: [String: AnyObject] = [
            MPMediaItemPropertyArtwork: coverArt,
            MPMediaItemPropertyTitle:workout.title,
            MPMediaItemPropertyArtist:"Alex",
            MPMediaItemPropertyAlbumTitle:workout.program.title,
            MPNowPlayingInfoPropertyPlaybackRate: player.currentPlaybackRate,
            MPNowPlayingInfoPropertyElapsedPlaybackTime: player.currentPlaybackTime,
            MPMediaItemPropertyPlaybackDuration: player.playableDuration
        ]
        MPNowPlayingInfoCenter.defaultCenter().nowPlayingInfo = dict
    }
}

当我同时尝试使用选项 (.MixWithOthers) 播放外部音乐,但我尝试使用选项播放外部音乐 (@ 987654325@) 信息中心没有更新。

编辑 1:只是为了让事情变得超级清楚,我已经可以正常播放视频我正在尝试使用其他背景音频播放视频,同时能够设置 MPNowPlayingInfoCenter。

【问题讨论】:

    标签: ios swift avfoundation mpnowplayinginfocenter


    【解决方案1】:

    目前这在 iOS 中是不可能的。即使只是将您的类别选项更改为 .MixWithOthers 也会导致您的 nowPlayingInfo 被忽略。

    我的猜测是 iOS 仅考虑将非混合应用程序包含在 MPNowPlayingInfoCenter 中,因为如果同时播放多个混合应用程序,则不确定哪个应用程序会显示在(例如)控制中心。

    如果 iOS 使用尽力而为的方法来选择“正在播放的应用程序”,我会非常高兴,如下所示:

    1. 如果播放的是非混音应用程序,请选择它。否则..
    2. 如果只播放一个混音应用程序,请选择它。否则..
    3. 如果有多个混音应用程序在播放,只选择一个 :) 或者不选一个,我都可以。

    如果您也喜欢这种行为,我建议您向 Apple 提交错误。

    【讨论】:

      【解决方案2】:

      您是否尝试过实现自己的自定义函数来更新MPNowPlayingInfoCenter?最近我用AVAudioPlayer播放音乐,需要手动更新。

      这基本上是我在加载新歌曲时调用的函数。

      func updateNowPlayingCenter() {
          let center = MPNowPlayingInfoCenter.defaultCenter()
          if nowPlayingItem == nil {
              center.nowPlayingInfo = nil
          } else {
              var songInfo = [String: AnyObject]()
      
              // Add item to dictionary if it exists
              if let artist = nowPlayingItem?.artist {
                  songInfo[MPMediaItemPropertyArtist] = artist
              }
              if let title = nowPlayingItem?.title {
                  songInfo[MPMediaItemPropertyTitle] = title
              }
              if let albumTitle = nowPlayingItem?.albumTitle {
                  songInfo[MPMediaItemPropertyAlbumTitle] = albumTitle
              }
              if let playbackDuration = nowPlayingItem?.playbackDuration {
                  songInfo[MPMediaItemPropertyPlaybackDuration] = playbackDuration
              }
              if let artwork = nowPlayingItem?.artwork {
                  songInfo[MPMediaItemPropertyArtwork] = artwork
              }
              center.nowPlayingInfo = songInfo
          }
      }
      

      我不确定在加载电影时执行此操作是否会覆盖 MPMoviePlayerController,但似乎值得一试。

      此外,他们还贬低了MPMoviePlayerController 并将其替换为AVPlayerViewController,所以这也值得研究。

      编辑:另外我会检查以确保您正确接收远程控制事件,因为这会影响信息中心显示的数据。

      【讨论】:

      • 您好,感谢您的回复。我已经更新了这个问题。问题不在于正常设置信息中心,而是在使用 .MixWithOthers 选项播放音乐时设置信息中心
      【解决方案3】:

      要快速播放视频,请使用:-

      func playVideoEffect() {
          let path = NSBundle.mainBundle().pathForResource("egg_grabberAnmi", ofType:"mp4")
          let url = NSURL.fileURLWithPath(path!)
          self.moviePlayer = MPMoviePlayerController(contentURL: url)
          if let player = moviePlayer {
              let screenSize: CGRect = UIScreen.mainScreen().bounds
              player.view.frame = CGRect(x: frame.size.width*0.10,y: frame.size.width/2, width:screenSize.width * 0.80, height: screenSize.width * 0.80)
              player.view.sizeToFit()
              player.scalingMode = MPMovieScalingMode.Fill
              player.fullscreen = true
              player.controlStyle = MPMovieControlStyle.None
              player.movieSourceType = MPMovieSourceType.File
              player.play()
              self.view?.addSubview(player.view)
              var timer = NSTimer.scheduledTimerWithTimeInterval(6.0, target: self, selector: Selector("update"), userInfo: nil, repeats: false)     
          }
      }
      
      
      // And Use the function to play Video
      playVideoEffect()
      

      【讨论】:

      • 是的,我们可以:- var url:NSURL = NSURL(string: "Your URL")
      • @Raksha 我认为你完全错过了这个问题。我已经知道如何播放视频,请重新阅读问题。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-04-30
      • 1970-01-01
      • 1970-01-01
      • 2018-06-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多