【问题标题】:Disable Audio (and interruption) with MPMoviePlayerController using Swift使用 Swift 通过 MPMoviePlayerController 禁用音频(和中断)
【发布时间】:2014-06-22 13:54:46
【问题描述】:

目前,这就是我在 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 等播放的音乐。

// 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 甚至没有设置设置实际音量?这是一个控件。

3)self.moviePlayerController.useApplicationAudioSession = false

【问题讨论】:

    标签: swift mpmovieplayercontroller avplayer avplayerlayer


    【解决方案1】:

    于是我找到了this answer

    这是我最终使用的 Swift 代码。然后我使用AVPlayerLayer 作为子层添加到视图中,效果很好。

    感谢 OP 设法找到了一名 Apple 技术人员并提供了原始的Objective-C code

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

    1) 中断当前音乐播放,无论是来自 Music、Spotify 等。

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

    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)
    }
    

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-11-17
    • 2013-09-19
    • 1970-01-01
    • 2013-04-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-12-26
    相关资源
    最近更新 更多