web-dev-qa-db-ja.com

フルスクリーンではなくビュー内でビデオを再生するにはどうすればよいですか?

全画面ではなく(facebookのように)ビュー内でビデオを再生する必要があります!誰か助けてもらえますか?私はSwift 2とxcode 7を使用しています。私はそれについてググったが、私が見つけたすべてはAVPlayerViewControllerとフルスクリーンで実行されている。

PS:悪い英語でごめんね。

13
aline

これは機能します:

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.
}
22
aline

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];
}

AVPlayer および AVPlayerLayer の詳細

3
MichK
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()
}

このコードは私にとってはうまくいきます。ぜひお試しください。

Swift 3のポッドプレーヤーの適用

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

0
Giang
 @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()
    }
0
Eli Uzan