首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >设置MPNowPlayingInfoCenter与其他背景音频播放

设置MPNowPlayingInfoCenter与其他背景音频播放
EN

Stack Overflow用户
提问于 2016-01-02 06:54:23
回答 3查看 3.4K关注 0票数 17

我正试图在Swift中使用MPMoviePlayerController播放iOS应用程序的视频。

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

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

Google在设置MPNowPlayingInfoCenter时混合音频。下面是我试图设置MPNowPlayingInfoCenter的方式:

代码语言:javascript
复制
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。

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2016-01-12 15:12:10

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

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

如果iOS在选择“现在播放的应用程序”时使用了一种尽力而为的方法,我会非常喜欢这样的:

  1. 如果有一个非混合应用程序,选择这个。否则..。
  2. 如果只有一个混合应用程序在玩,那就选吧。否则..。
  3. 如果有多个混合应用在玩,只需选择一个:)或选择一个,我对两者都没有意见。

如果你也喜欢这种行为,我会鼓励你向苹果公司提交一个bug。

票数 10
EN

Stack Overflow用户

发布于 2016-01-06 01:11:41

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

这基本上是我调用一首新歌加载的函数。

代码语言:javascript
复制
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取代了它,所以这也是值得研究的。

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

票数 0
EN

Stack Overflow用户

发布于 2016-01-11 10:04:06

若要迅速播放该录影带,请注意:-

代码语言:javascript
复制
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()
票数 -1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/34563451

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档