iPhone SDK:你如何在视图中播放视频?而不是全屏

Sam*_*Sam 59 objective-c media-player mpmovieplayer ios

我正在尝试在a里面播放视频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];
}
Run Code Online (Sandbox Code Playgroud)

但是当它在自己的类中使用这个方法时,这只是崩溃应用程序,但在其他地方很好.有谁知道如何在视图中播放视频?并避免全屏?

tob*_*byc 83

从3.2 SDK开始,您可以访问view属性MPMoviePlayerController,修改其框架并将其添加到视图层次结构中.

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

这里有一个例子:http://www.devx.com/wireless/Article/44642/1954


mdz*_*iec 33

最好的方法是使用视图的图层:

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];
Run Code Online (Sandbox Code Playgroud)

不要忘记添加框架:

#import <QuartzCore/QuartzCore.h>
#import "AVFoundation/AVFoundation.h"
Run Code Online (Sandbox Code Playgroud)

  • 我更改了变量名称. (2认同)

jrc*_*jrc 15

查看代码,您需要设置影片播放器控制器视图的框架,并将影片播放器控制器的视图添加到视图中.另外,不要忘记将MediaPlayer.framework添加到目标.

这是一些示例代码:

#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
Run Code Online (Sandbox Code Playgroud)

  • 我爱你!!非常感谢,我现在至少要搜索一个小时,你只有一个简单的代码! (2认同)

Sur*_*gch 12

迅速

这是一个自包含项目,因此您可以在上下文中查看所有内容.

布局

使用a UIView和a 创建如下所示的布局UIButton.这UIView将是我们将播放视频的容器.

在此输入图像描述

将视频添加到项目中

如果您需要使用示例视频进行练习,可以从sample-videos.com获取.我在这个例子中使用的是mp4格式的视频.将视频文件拖放到项目中.我还必须将它明确地添加到bundle资源中(转到Build Phases> Copy Bundle Resources,请参阅此答案以获取更多信息).

这是项目的完整代码.

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()
    }
}
Run Code Online (Sandbox Code Playgroud)

笔记

  • 如果要打开和关闭不同的视频,可以使用AVPlayerItem.
  • 如果您只使用AVFoundationAVPlayer,则必须构建所有自己的控件.如果您想要全屏视频播放,您可以使用AVPlayerViewController.你需要导入AVKit它.它配备了用于暂停全套的控制,快进,快退,停止等在这里这里有一些视频教程.
  • MPMoviePlayerController 您可能已在其他答案中看到的已弃用.

结果

该项目现在看起来应该是这样的.

在此输入图像描述


小智 5

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

[self presentMoviePlayerViewControllerAnimated:playerv];
Run Code Online (Sandbox Code Playgroud)