2015-06-07 227 views
2

我目前使用MPMoviePlayerController?在我的應用程序內播放視頻。但我希望擁有Facebook風格的視頻,最初視頻會播放靜音,但用戶不會靜音。iOS Swift靜音 - 取消靜音音頻像facebook自動播放視頻

我是編程新手,但根據我的研究,我無法在MPMoviePlayerController中找到任何東西?我們可以做到這一點。

當前我正在使用以下代碼播放視頻。

class InformationViewController: UIViewController { 

@IBOutlet var thumbnailImage: UIImageView! 
@IBOutlet var videoPlayerView: UIView! 

@IBOutlet var controlBtn: UIButton! 

var moviePlayer:MPMoviePlayerController! 
var url:NSURL = NSURL(string: "http://jplayer.org/video/m4v/Big_Buck_Bunny_Trailer.m4v")! 
var audioPlayer = AVAudioPlayer() 

override func viewDidLoad() { 
    super.viewDidLoad() 


} 


@IBAction func controlBtn(sender: AnyObject) { 
    controlBtn.alpha = 0 
    moviePlayer = MPMoviePlayerController(contentURL: url) 
    let frameWidth = self.videoPlayerView.frame.size.width 
    let frameHeight = self.videoPlayerView.frame.size.height 

    //moviePlayer.view.frame = CGRect(x: 24, y: 18, width: frameWidth, height: frameHeight) 

    self.videoPlayerView.addSubview(moviePlayer.view) 


    //  Send button backword 
    self.videoPlayerView.sendSubviewToBack(moviePlayer.view) 
    moviePlayer.fullscreen = false 
    moviePlayer.controlStyle = MPMovieControlStyle.Embedded 

    // Pure Visual Format Language style (http://stackoverflow.com/questions/26180822/swift-adding-constraints-programmatically) 
    (moviePlayer.view).setTranslatesAutoresizingMaskIntoConstraints(false) 
    let views = ["view": videoPlayerView, "videoView": moviePlayer.view] 

    var constH = NSLayoutConstraint.constraintsWithVisualFormat("H:[view]-(<=0)-[videoView(\(frameWidth))]", options: .AlignAllCenterY, metrics: nil, views: views) 
    view.addConstraints(constH) 
    var constW = NSLayoutConstraint.constraintsWithVisualFormat("V:[view]-(<=0)-[videoView(\(frameHeight))]", options: .AlignAllCenterX, metrics: nil, views: views) 
    view.addConstraints(constW) 

} 

override func viewDidDisappear(animated: Bool) { 
    super.viewDidDisappear(animated) ///< Don't for get this BTW! 

    if let player = self.moviePlayer { 
     player.stop() 
    } 
}} 

回答

1

在與Apple技術人員交談後發現,使用MPMoviePlayerController無法控制或靜音音頻。

因此,您可以使用AVFoundations AVPlayer類在播放像FB時將視頻靜音。

源代碼:

AVAsset *asset = [AVAsset assetWithURL:[NSURL URLWithString:strURL]]; 
AVPlayerItem *playerItem = [[AVPlayerItem alloc] initWithAsset:asset]; 
AVPlayer *avPlayer = [AVPlayer playerWithPlayerItem:playerItem]; 

//Using this you can mute the audio and play only video as you wish 
    avPlayer.muted = YES; 

AVPlayerLayer *avPlayerLayer = [AVPlayerLayer playerLayerWithPlayer:avPlayer]; 
avPlayerLayer.frame = IS_IPAD ? CGRectMake(0, 0, 748, 423): CGRectMake(0, 0, 311, 200); 
avPlayerLayer.backgroundColor =[UIColor clearColor].CGColor; 
avPlayerLayer.videoGravity =AVLayerVideoGravityResize; 

一旦靜音音頻U可以只改變布爾取消靜音音頻像下面。 avPlayer.muted = NO;

相關問題