使用Swift使用MPMoviePlayerController禁用音频(和中断) [英] Disable Audio (and interruption) with MPMoviePlayerController using Swift

查看:125
本文介绍了使用Swift使用MPMoviePlayerController禁用音频(和中断)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此刻,这就是我在UIViewController的子视图上播放视频的方式:

At the moment, this is how I'm playing a video on the subview of my UIViewController:

override func viewDidAppear(animated: Bool)  {
    let filePath = NSBundle.mainBundle().pathForResource("musicvideo", ofType: "mp4")
    self.moviePlayerController.contentURL = NSURL.fileURLWithPath(filePath)
    self.moviePlayerController.play()
    self.moviePlayerController.repeatMode = .One
    self.moviePlayerController.view.frame = self.view.bounds
    self.moviePlayerController.scalingMode = .AspectFill
    self.moviePlayerController.controlStyle = .None
    self.moviePlayerController.allowsAirPlay = false
    self.view.addSubview(self.moviePlayerController.view)
}

我已通过以下操作(完全无效)来阅读禁用音频的方法.请记住,我正在尝试禁用它,以至于不中断通过音乐"应用程序,Spotify等播放的当前音乐.

I've read on ways to disable the audio by doing the following below (none of which work, at all). Keep in mind I'm trying to disable it to the point of not interrupting the current music playing via the Music app, Spotify, etc.

// Playing media items with the applicationMusicPlayer will restore the user's Music state after the application quits.

// The current volume of playing music, in the range of 0.0 to 1.0.
// This property is deprecated -- use MPVolumeView for volume control instead.

1)MPMusicPlayerController.applicationMusicPlayer().volume = 0

2)MPVolumeView甚至没有用于设置设置实际音量的设置?这是一个控件.

2) MPVolumeView doesn't even have a setting for setting the actual volume? It's a control.

3)self.moviePlayerController.useApplicationAudioSession = false

推荐答案

所以我发现这是我最终使用的Swift代码.然后,我使用AVPlayerLayer作为子层添加到视图中,该子层运行良好.

This is my Swift code that I ended up going with. I then used an AVPlayerLayer to add to the view as a sublayer, which works perfectly.

感谢OP设法获得了Apple技术人员的支持,并提供了

Thanks to the OP who managed to get a hold of an Apple technician and provided the original Objective-C code.

我现在面临的唯一问题是:

The only problems I'm facing now is that it:

1)中断当前的音乐播放,无论它是来自Music,Spotify等.

1) Interrupts current music playback, whether it's from Music, Spotify, etc.

2),如果我关闭该应用并再次将其打开,则视频将停止播放.

2) Video stops playing if I close the app and open it up again.

override func viewDidAppear(animated: Bool)  {
    let filePath = NSBundle.mainBundle().pathForResource("musicvideo", ofType: "mp4")

    var asset: AVURLAsset?
    asset = AVURLAsset.URLAssetWithURL(NSURL.fileURLWithPath(filePath), options: nil)
    var audioTracks = NSArray()
    audioTracks = asset!.tracksWithMediaType(AVMediaTypeAudio)

    // Mute all the audio tracks
    let allAudioParams = NSMutableArray()
    for track: AnyObject in audioTracks {
        // AVAssetTrack
        let audioInputParams = AVMutableAudioMixInputParameters()
        audioInputParams.setVolume(0.0, atTime: kCMTimeZero)
        audioInputParams.trackID = track.trackID
        allAudioParams.addObject(audioInputParams)
    }

    let audioZeroMix = AVMutableAudioMix()
    audioZeroMix.inputParameters = allAudioParams

    // Create a player item
    let playerItem = AVPlayerItem(asset: asset)
    playerItem.audioMix = audioZeroMix

    // Create a new Player, and set the player to use the player item
    // with the muted audio mix
    let player = AVPlayer.playerWithPlayerItem(playerItem) as AVPlayer

    player.play()

    let layer = AVPlayerLayer(player: player)
    player.actionAtItemEnd = .None

    layer.frame = self.view.bounds
    layer.videoGravity = AVLayerVideoGravityResizeAspectFill
    self.view.layer.addSublayer(layer)
}

这篇关于使用Swift使用MPMoviePlayerController禁用音频(和中断)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆