iPhone SDK:いかにしていきビデオ内のとおりです。ではなくフルスクリーン

StackOverflow https://stackoverflow.com/questions/1266750

質問

今は遊びビデオ内 UIView, なので私の最初の段階として追加のクラスが見ら再生を開始映画の中でこのコード:

- (IBAction)movie:(id)sender{
    NSBundle *bundle = [NSBundle mainBundle];
        NSString *moviePath = [bundle pathForResource:@"Movie" ofType:@"m4v"];
    NSURL *movieURL = [[NSURL fileURLWithPath:moviePath] retain];
    MPMoviePlayerController *theMovie = [[MPMoviePlayerController alloc] initWithContentURL:movieURL];
    theMovie.scalingMode = MPMovieScalingModeAspectFill;
    [theMovie play];
}

これだけでクラッシュのアプリこのメソッドを使用する場合の中での自分のクラスがある。なんだろうけど、日本人の遊び方画中ですか?なでフル表示順の設定ができます。

役に立ちましたか?

解決

3.2 SDKのとして、あなたは、MPMoviePlayerControllerのビュープロパティにアクセスそのフレームを変更して、ビュー階層に追加することができます。

MPMoviePlayerController *player = [[MPMoviePlayerController alloc] initWithContentURL:[NSURL fileURLWithPath:url]];
player.view.frame = CGRectMake(184, 200, 400, 300);
[self.view addSubview:player.view];
[player play];

の例はここにあります: http://www.devx.com/wireless/Article / 44642/1954

他のヒント

最善の方法は、ビューのinsted層を使用することです

AVPlayer *player = [AVPlayer playerWithURL:[NSURL url...]]; // 

AVPlayerLayer *layer = [AVPlayerLayer layer];

[layer setPlayer:player];
[layer setFrame:CGRectMake(10, 10, 300, 200)];
[layer setBackgroundColor:[UIColor redColor].CGColor];
[layer setVideoGravity:AVLayerVideoGravityResizeAspectFill];

[self.view.layer addSublayer:layer];

[player play];

フレームワークを追加することを忘れないで下さいます:

#import <QuartzCore/QuartzCore.h>
#import "AVFoundation/AVFoundation.h"

を見てからコードを指定する必要がありますフレームの動画再生プレイヤーコントローラのビューにも追加の映画のプレイヤーコントローラのビューを眺め。また、忘れずに追加 MediaPlayer.枠組み をしている。

こちらのサンプルコード:

#import <MediaPlayer/MediaPlayer.h>

@interface ViewController () {
    MPMoviePlayerController *moviePlayerController;
}

@property (weak, nonatomic) IBOutlet UIView *movieView; // this should point to a view where the movie will play

@end

@implementation ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.

    // Instantiate a movie player controller and add it to your view
    NSString *moviePath = [[NSBundle mainBundle] pathForResource:@"foo" ofType:@"mov"];
    NSURL *movieURL = [NSURL fileURLWithPath:moviePath];    
    moviePlayerController = [[MPMoviePlayerController alloc] initWithContentURL:movieURL];
    [moviePlayerController.view setFrame:self.movieView.bounds];  // player's frame must match parent's
    [self.movieView addSubview:moviePlayerController.view];

    // Configure the movie player controller
    moviePlayerController.controlStyle = MPMovieControlStyleNone;        
    [moviePlayerController prepareToPlay];
}

- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];

    // Start the movie
    [moviePlayerController play];
}

@end

迅速かつ

このはつくれるプロジェクトできるよう見ます。

レイアウト

レイアウトは次のように、 UIViewUIButton.の UIView するコンテナのまちです。

enter image description here

追加のビデオのプロジェクト

が必要な場合は、サンプル動画を練習し、だから sample-videos.com.を使用しているmp4形式の映像にこの例です。ドラッグアンドドロップビデオファイルをプロジェクトに関するまでも追加することを明示的にバンドル資源を の構築相>コピーバンドルの資源, 参照 この答え たします。

コード

こちらは完全にコードさせるまでには至らなかった。

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

注記

  • の場合はまさに切り替えに異なる映像を使用でき AVPlayerItem.
  • のみを使用する場合には AVFoundationAVPlayer, そのすべての自分の制御できます。したい場合はフル画面動画再生中に利用でき AVPlayerViewController.インポートする必要 AVKit います。でのフルセット制御のための一時停止、早送り、巻き戻しを停止等 こちらのこちらの 一部ビデオアントになります。
  • MPMoviePlayerController が現実のものとなる可能性があるその他の回答が推奨されていません。

結果

のプロジェクトはこのようになっているでしょう。

enter image description here

NSString * pathv = [[NSBundle mainBundle] pathForResource:@"vfile" ofType:@"mov"];
playerv = [[MPMoviePlayerViewController alloc] initWithContentURL:[NSURL fileURLWithPath:pathv]];

[self presentMoviePlayerViewControllerAnimated:playerv];
NSURL *url = [NSURL URLWithString:[exreciesDescription objectForKey:@"exercise_url"]];
moviePlayer =[[MPMoviePlayerController alloc] initWithContentURL: url];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(doneButtonClicked) name:MPMoviePlayerWillExitFullscreenNotification object:nil];
[[moviePlayer view] setFrame: [self.view bounds]];  // frame must match parent view
[self.view addSubview: [moviePlayer view]];
[moviePlayer play];

-(void)playMediaFinished:(NSNotification*)theNotification 
{
    moviePlayer=[theNotification object];
    [[NSNotificationCenter defaultCenter] removeObserver:self
                                                    name:MPMoviePlayerPlaybackDidFinishNotification
                                                  object:moviePlayer];


    [moviePlayer.view removeFromSuperview];
}

-(void)doneButtonClicked
  {
         [moviePlayer stop];
        [moviePlayer.view removeFromSuperview];
         [self.navigationController popViewControllerAnimated:YES];//no need this if you are      opening the player in same screen;
  }

スウィフトバージョン:

import AVFoundation

func playVideo(url: URL) {

    let player = AVPlayer(url: url)

    let layer: AVPlayerLayer = AVPlayerLayer(player: player)
    layer.backgroundColor = UIColor.white.cgColor
    layer.frame = CGRect(x: 0, y: 0, width: 300, height: 300)
    layer.videoGravity = .resizeAspectFill
    self.view.layer.addSublayer(layer)

    player.play()
}

次のメソッドを使用します。

self.imageView_VedioContainerがあなたのAVPlayerのコンテナ図である。

- (void)playMedia:(UITapGestureRecognizer *)tapGesture
{
    playerViewController = [[AVPlayerViewController alloc] init];

    playerViewController.player = [AVPlayer playerWithURL:[[NSBundle mainBundle]
                                                 URLForResource:@"VID"
                                                         withExtension:@"3gp"]];
    [playerViewController.player play];
    playerViewController.showsPlaybackControls =YES;
    playerViewController.view.frame=self.imageView_VedioContainer.bounds;
    [playerViewController.view setAutoresizingMask:UIViewAutoresizingNone];// you can comment this line 
    [self.imageView_VedioContainer addSubview: playerViewController.view];
}

あなたは、ビュー内でビデオを再生することはできません。これは、フルスクリーン再生することがあります。

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top