2016-01-14 109 views
9

我需要在視圖內播放視頻而不是全屏(如Facebook)!誰能幫忙?我使用的是swift 2和xcode 7.我用google搜索了一下,但是我發現所有的代碼都是用AVPlayerViewController和全屏運行的。如何在視圖中播放視頻而不是全屏?

詩:對不起英文不好。

回答

1

您可以嘗試使用具有所需大小和位置的AVPlayerLayer並在其上播放視頻。

示例代碼設置播放機和層:

- (void)setupPlayer 
{ 
    AVPlayerItem *playerItem = [AVPlayerItem playerItemWithURL:<Your video item URL>]; 
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(playFinished:) name:AVPlayerItemDidPlayToEndTimeNotification object:playerItem]; 

    self.player = [AVPlayer playerWithPlayerItem:playerItem]; 

    self.playerLayer = [AVPlayerLayer playerLayerWithPlayer:self.player]; 

    self.playerLayer.videoGravity = AVLayerVideoGravityResizeAspectFill; 

    [self.view.layer addSublayer:self.playerLayer]; 
} 

更多AVPlayerAVPlayerLayer

+0

好的,我會盡快發佈反饋意見 – aline

15

這工作:

class BeginController: UIViewController { 
@IBOutlet weak var videoPreviewLayer: UIView! 
var player: AVPlayer! 
var avpController = AVPlayerViewController() 

override func viewDidLoad() { 
    super.viewDidLoad() 
    let moviePath = NSBundle.mainBundle().pathForResource("Hello Moto", ofType: "mp4") 
    if let path = moviePath { 
     let url = NSURL.fileURLWithPath(path) 
     self.player = AVPlayer(url: url) 
     self.avpController = AVPlayerViewController() 
     self.avpController.player = self.player 
     avpController.view.frame = videoPreviewLayer.frame 
     self.addChildViewController(avpController) 
     self.view.addSubview(avpController.view)    
    } 
    // Do any additional setup after loading the view. 
} 
0

斯威夫特3採用POD播放器

Podfile:

platform :ios, '10.0' 
use_frameworks! 

def pods 
    pod "Player", "~> 0.5.0" 
end 

target 'YourProject' do 
    pods 
end 

YourViewController:

import Player 

    // Player 
    var player:Player 

    // MARK: object lifecycle Player 
    convenience init() { 
     self.init(nibName: nil, bundle:nil) 
    } 

    required init?(coder aDecoder: NSCoder) { 
     self.player = Player() 
     super.init(coder: aDecoder) 
    } 

    override init(nibName nibNameOrNil: String?, bundle nibBundleOrNil: Bundle?) { 
     self.player = Player() 
     super.init(nibName: nibNameOrNil, bundle: nibBundleOrNil) 
    } 

    deinit { 
     self.player.willMove(toParentViewController: self) 
     self.player.view.removeFromSuperview() 
     self.player.removeFromParentViewController() 
    } 


    private func playeVideo(url: URL) { 

     player.playerDelegate = self 
     player.playbackDelegate = self 
     player.view.frame = self.view.bounds // your frame here 
     self.addChildViewController(self.player) 
     self.view.insertSubview(player.view, at: 0) 

     player.url = url 
     self.player.playFromBeginning() 

    } 

請詳細點擊這裏詳細https://github.com/piemonte/Player

1
import AVFoundation 
import AVKit 

var player = AVPlayer() 
var playerController = AVPlayerViewController() 

func playVideo() { 
    let videoURL = NSURL(string: videoUrl) 
    player = AVPlayer(url: videoURL! as URL) 
    let playerController = AVPlayerViewController() 
    playerController.player = player 
    self.addChildViewController(playerController) 

    // Add your view Frame 
    playerController.view.frame = self.view.frame 

    // Add sub view in your view 
    self.view.addSubview(playerController.view) 

    player.play() 
} 

供玩家停止或暫停

func stopVideo() { 
    player.pause() 
} 

本準則是對我,請嘗試工作。