Pergunta

Eu estou tentando reproduzir vídeo dentro de um UIView, por isso o meu primeiro passo foi adicionar uma classe para essa visão e começar a jogar um filme nele usando este código:

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

Mas isso só trava o aplicativo ao usar este método dentro da própria classe, mas é bom em outro lugar. Alguém sabe como jogar vídeo dentro de um ponto de vista? e evitá-lo sendo tela cheia?

Foi útil?

Solução

Como do 3.2 SDK você pode acessar a propriedade vista MPMoviePlayerController, modificar sua estrutura e adicioná-lo à sua hierarquia de vista.

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

Há um exemplo aqui: http://www.devx.com/wireless/Article / 44642/1954

Outras dicas

A melhor maneira é camadas de uso insted de pontos de vista:

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

Não se esqueça de adicionar os quadros:

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

Olhando para o código, você precisa definir o quadro de vista do controlador de jogador de filme, e também adicionar a visão do controlador de jogador de filme para o seu ponto de vista. Além disso, não se esqueça de adicionar MediaPlayer.framework para o seu alvo.

Aqui está um código de exemplo:

#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

Swift

Esta é uma auto-contido projeto de modo que você pode ver tudo no contexto.

Layout

Criar um layout como o seguinte com um UIView e uma UIButton. O UIView será o recipiente em que vamos jogar o nosso vídeo.

 enter descri&ccedil;&atilde;o da imagem aqui

Adicionar um vídeo para o projeto

Se você precisa de um vídeo de exemplo para a prática com, você pode obter um de sample-videos.com . Eu estou usando um formato de vídeo mp4 neste exemplo. Arraste e solte o arquivo de vídeo em seu projeto. Eu também tive que adicioná-lo explicitamente para os recursos do pacote (vá para Fases Construir> Copiar Pacote de Recursos , consulte este responder para mais).

Código

Aqui está o código completo para o projeto.

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

Notas

  • Se você estiver indo para estar trocando dentro e fora vídeos diferentes, você pode usar AVPlayerItem .
  • Se você estiver usando apenas AVFoundation e AVPlayer , então você tem que construir tudo de seus próprios controles. Se você quiser a reprodução de vídeo em tela cheia, você pode usar AVPlayerViewController . Você precisará importar AVKit para isso. Ele vem com um conjunto completo de controles para pausar, avançar, retroceder, parada, etc. Aqui e aqui estão alguns tutoriais em vídeo.
  • MPMoviePlayerController que você pode ter visto em outras respostas está obsoleta.

Resultado

O projeto deverá ser parecido com isso agora.

 enter descri&ccedil;&atilde;o da imagem aqui

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

Versão Swift:

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

Use o método a seguir.

self.imageView_VedioContainer é a visão recipiente de sua 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];
}

Você não pode reproduzir um vídeo dentro de uma visão. Tem que ser jogado em tela cheia.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top