我需要在不是全屏的视图中播放视频(比如facebook)!有人可以帮忙吗?我使用的是swift 2和xcode 7.我已经用Google搜索了它,但我找到的只是使用AVPlayerViewController和全屏运行。
Ps:抱歉英文不好。
答案 0 :(得分:22)
这有效:
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.
}
答案 1 :(得分:2)
您可以尝试使用所需大小和位置的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];
}
的更多信息
答案 2 :(得分:2)
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()
}
本规范对我有用,请试试。
答案 3 :(得分:0)
Swift 3应用pod Player
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()
}
答案 4 :(得分:0)
@IBOutlet weak var backView: UIView!
override func viewDidLoad() {
super.viewDidLoad()
playVideo()
}
private func playVideo() {
guard let path = Bundle.main.path(forResource: "video", ofType:"mp4") else {
debugPrint("video.mp4 not found")
return
}
let player = AVPlayer(url: URL(fileURLWithPath: path))
let playerController = AVPlayerViewController()
playerController.player = player
--- start from here :
let playerLayerAV = AVPlayerLayer(player: player)
//now we set the size of frame to be like the view ("backview")
playerLayerAV.frame = backView.bounds
// the backview is the view i'm using it to insert a video
backView.layer.addSublayer(playerLayerAV)
player.play()
}