2016-02-05 4 views
17

目標は、コントロールなしでUIView内のビデオファイル(* .mp4)を再生することです。背景/壁紙のようなコントロールなしでUIViewの内部でビデオを再生するには?

ViewControllerやその他のコントロールの背景/壁紙として使用されます。つまり、テーブルビュー、テキストフィールドなどです。画像は、再生された動画とともに表示されます。

これを行うにはどうすればよいですか? あなたが

+0

URLを使用したローカルビデオかユースチューブビデオですか? – Vidhyanand

+0

プロジェクトにインポートされたローカルファイルになります。実際には別々のViewController用に複数のファイル(ビデオ)があります –

+0

参照http://stackoverflow.com/questions/17922017/display-video-inside-the-uiwebview-not-in-device-full-screen – Vidhyanand

答えて

38

は私がネイティブAVPlayer

1.Used AVFoundationでゴールに達したありがとう:プレイヤーのための

#import <AVFoundation/AVFoundation.h> 

2.Usedプロパティ:

@property (nonatomic) AVPlayer *avPlayer; 

3.Addedビデオファイルを「ビデオ」フォルダに追加し、プロジェクトに「ビデオ」を追加しました

は、イベントのためにプレーヤー

NSString *filepath = [[NSBundle mainBundle] pathForResource:@"shutterstock_v885172.mp4" ofType:nil inDirectory:@"Video"]; 
NSURL *fileURL = [NSURL fileURLWithPath:filepath]; 
self.avPlayer = [AVPlayer playerWithURL:fileURL]; 
self.avPlayer.actionAtItemEnd = AVPlayerActionAtItemEndNone; 

AVPlayerLayer *videoLayer = [AVPlayerLayer playerLayerWithPlayer:self.avPlayer]; 
videoLayer.frame = self.view.bounds; 
videoLayer.videoGravity = AVLayerVideoGravityResizeAspectFill; 
[self.view.layer addSublayer:videoLayer]; 

[self.avPlayer play]; 

5.Subscribedを4.Initialized - ビデオは最後まで非常に

- (void)itemDidFinishPlaying:(NSNotification *)notification { 
    AVPlayerItem *player = [notification object]; 
    [player seekToTime:kCMTimeZero]; 
} 
1
関連した方法で起動から再生

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(itemDidFinishPlaying:) name:AVPlayerItemDidPlayToEndTimeNotification object:[self.avPlayer currentItem]]; 

6.Resumedビデオを再生しました

スイフト

スイフトでも同様です。リソースバンドルにビデオを追加します。私の完全な答えはhereです。

import UIKit 
import AVFoundation 

class ViewController: UIViewController { 

    var player: AVPlayer? 

    @IBOutlet weak var videoViewContainer: UIView! 

    override func viewDidLoad() { 
     super.viewDidLoad() 
     initializeVideoPlayerWithVideo() 
    } 

    func initializeVideoPlayerWithVideo() { 

     // get the path string for the video from assets 
     let videoString:String? = Bundle.main.path(forResource: "SampleVideo_360x240_1mb", ofType: "mp4") 
     guard let unwrappedVideoPath = videoString else {return} 

     // convert the path string to a url 
     let videoUrl = URL(fileURLWithPath: unwrappedVideoPath) 

     // initialize the video player with the url 
     self.player = AVPlayer(url: videoUrl) 

     // create a video layer for the player 
     let layer: AVPlayerLayer = AVPlayerLayer(player: player) 

     // make the layer the same size as the container view 
     layer.frame = videoViewContainer.bounds 

     // make the video fill the layer as much as possible while keeping its aspect size 
     layer.videoGravity = AVLayerVideoGravity.resizeAspectFill 

     // add the layer to the container view 
     videoViewContainer.layer.addSublayer(layer) 
    } 

    @IBAction func playVideoButtonTapped(_ sender: UIButton) { 
     // play the video if the player is initialized 
     player?.play() 
    } 
} 
関連する問題