将MPNowPlayingInfoCenter设置为播放其他背景音频

时间:2016-01-02 06:54:23

标签: ios swift avfoundation mpnowplayinginfocenter

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

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

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

谷歌地图混合音频,同时设置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)播放外部音乐时,上述功能有效,但是当我尝试使用该选项播放外部音乐时(.MixWithOthers)信息中心不会更新。

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

3 个答案:

答案 0 :(得分:8)

这在iOS中目前无法实现。即使只是将类别选项更改为.MixWithOthers,也会导致nowPlayingInfo被忽略。

我的猜测是iOS只考虑包含在MPNowPlayingInfoCenter中的非混音应用,因为如果有多个混音应用在同一时间播放,那么在(例如)控制中心中会出现哪些应用存在不确定性

如果iOS使用尽力而为的方法来选择现在正在玩的应用程序"我非常喜欢它,如下所示:

  1. 如果有非混音应用,请选择。其他..
  2. 如果只有一个混音应用播放,请选择。其他..
  3. 如果有多个混音应用播放,只需选择一个:)或选择无,我也可以。
  4. 如果您也喜欢此行为,我建议您向Apple提交错误。

答案 1 :(得分:0)

您是否尝试过实施自己的自定义功能来更新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,因此也值得研究。

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

答案 2 :(得分:0)

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

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()
相关问题