我正试图在Swift中使用MPMoviePlayerController播放iOS应用程序的视频。
我的目标是能够播放系统音乐,像苹果音乐,然后打开我的应用程序,让音频混合,但我希望我的应用程序能够控制MPNowPlayingInfoCenter。
如何在设置AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryPlayback, withOptions: .MixWithOthers)时使用MPNowPlayingInfoCenter?
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)播放外部音乐时,上面的函数可以工作,但是当我尝试使用选项(.MixWithOthers)播放外部音乐时,信息中心不会更新。
编辑1:为了让事情变得非常清楚,我已经有了正确的视频播放,我正在尝试与其他背景音频播放视频,同时能够设置MPNowPlayingInfoCenter。
发布于 2016-01-12 15:12:10
这在iOS中目前是不可能的。即使只是将类别选项更改为.MixWithOthers,也会导致忽略nowPlayingInfo。
我的猜测是iOS只考虑将非混合应用程序包含在MPNowPlayingInfoCenter中,因为不确定哪个应用程序会出现在其中(例如)。控制中心如果有多个混合应用同时播放。
如果iOS在选择“现在播放的应用程序”时使用了一种尽力而为的方法,我会非常喜欢这样的:
如果你也喜欢这种行为,我会鼓励你向苹果公司提交一个bug。
发布于 2016-01-06 01:11:41
您是否尝试过实现自己的自定义函数来更新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取代了它,所以这也是值得研究的。
编辑:我也会检查,以确保您正在正确地接收远程控制事件,因为这会影响到信息中心显示的数据。
发布于 2016-01-11 10:04:06
若要迅速播放该录影带,请注意:-
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()https://stackoverflow.com/questions/34563451
复制相似问题