而程式碼裡面會看到有三個button
第一個PlayBtn所使用的方法是直接開啟AVPlayerViewController做播放
並且要自行在點擊播放(如需自動播放加入.play()即可)
第二個InViewBtn所使用的方法是在storyboard裡面建立一個View(自訂大小)然後將AVPlayer放入View裡面並且直接自動播放player.play()
第三個就是將在view內播放時可按暫停
其他深入的小功能就靠大家自行摸索摟~
//
// ViewController.swift
// AVPlayer
//
// Created by John on 2017/1/23.
// Copyright © 2017年 John. All rights reserved.
//
import UIKit
import AVKit
import AVFoundation
class ViewController: UIViewController {
@IBOutlet weak var UrlText: UITextField!
@IBOutlet weak var PlayerView: UIView!
var player:AVPlayer!
var Bool:Bool! = false
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
@IBAction func PlayBtn(_ sender: UIButton) {
let url = URL(string: UrlText.text!)
player = AVPlayer(url: url!)
let playerViewController = AVPlayerViewController()
playerViewController.player = player;
self.present(playerViewController, animated: true, completion: nil)
// player.play()
}
@IBAction func InViewBtn(_ sender: UIButton) {
let url = URL(string: UrlText.text!)
player = AVPlayer(url: url!);
let playerLayer = AVPlayerLayer(player: player)
playerLayer.frame = self.PlayerView.bounds
self.PlayerView.layer.addSublayer(playerLayer)
player.play()
Bool = true
}
@IBAction func Player(_ sender: UIButton) {
if Bool == true{
player.pause()
Bool = false
}else{
player.play()
Bool = true
}
}
}