我正在为朋友的商店制作iPhone 6应用程序。我想在初始视图控制器启动时自动播放视频介绍。我没有声音或音频。
我的故事板上有一个UIViewController
,其中包含一个我重新归类为AVPlayerClass的视图
我还在"支持文件"下添加了cupcake1.mov(QuickTime格式)。
// AppDelegate.h
// MovieUIView
//
// Created by Daniel Habshush on 28.03.15.
// Copyright (c) 2015 H Company. All rights reserved.
//
#import <UIKit/UIKit.h>
@interface AppDelegate : UIResponder <UIApplicationDelegate>
@property (strong, nonatomic) UIWindow *window;
@end
//
// AppDelegate.m
// MovieUIView
//
// Created by Daniel Habshush on 28.03.15.
// Copyright (c) 2015 H Company. All rights reserved.
//
#import "AppDelegate.h"
@interface AppDelegate ()
@end
@implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
return YES;
}
- (void)applicationWillResignActive:(UIApplication *)application {
// Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
// Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
}
- (void)applicationDidEnterBackground:(UIApplication *)application {
// Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
// If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}
- (void)applicationWillEnterForeground:(UIApplication *)application {
// Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
}
- (void)applicationDidBecomeActive:(UIApplication *)application {
// Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}
- (void)applicationWillTerminate:(UIApplication *)application {
// Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}
@end
//
// ViewController.h
// MovieUIView
//
// Created by Daniel Habshush on 28.03.15.
// Copyright (c) 2015 H Company. All rights reserved.
//
#import <UIKit/UIKit.h>
#import <AVFoundation/AVFoundation.h>
#import "AVPlayerClass.h"
@class AVPlayer;
@class AVPlayerClass;
@interface ViewController : UIViewController
@property (nonatomic, strong) AVPlayer *player;
@property (nonatomic, strong) AVPlayerClass *playerView;
@end
//
// ViewController.m
// MovieUIView
//
// Created by Daniel Habshush on 28.03.15.
// Copyright (c) 2015 H Company. All rights reserved.
//
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
@synthesize player;
@synthesize playerView;
- (void)viewDidLoad {
[super viewDidLoad];
[self setupMovie];
}
- (void)setupMovie
{
NSURL *url = [[NSBundle mainBundle]URLForResource:@"cupcake1" withExtension:@"mov"];
self.player = [AVPlayer playerWithURL: url];
[self.playerView setMovieToPlayer:player];
[self.player play];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
//
// AVPlayerClass+.h
// MovieUIView
//
// Created by Daniel Habshush on 28.03.15.
// Copyright (c) 2015 H Company. All rights reserved.
//
#import <UIKit/UIKit.h>
#import <AVFoundation/AVFoundation.h>
@class AVPlayer;
@interface AVPlayerClass : UIView
@property (nonatomic, retain) AVPlayer* player;
- (void)setMovieToPlayer:(AVPlayer*)player;
@end
//
// AVPalyerClass.m
// MovieUIView
//
// Created by Daniel Habshush on 28.03.15.
// Copyright (c) 2015 H Company. All rights reserved.
//
#import "AVPlayerClass.h"
@implementation AVPlayerClass
+ (Class)layerClass
{
return [AVPlayerLayer class];
}
-(AVPlayer*)player
{
return [(AVPlayerLayer*) [self layer] player];
}
- (void)setMovieToPlayer:(AVPlayer *)player
{
[(AVPlayerLayer*)[self layer] setPlayer: player];
}
@end
//
// main.m
// MovieUIView
//
// Created by Daniel Habshush on 28.03.15.
// Copyright (c) 2015 H Company. All rights reserved.
//
#import <UIKit/UIKit.h>
#import "AppDelegate.h"
int main(int argc, char * argv[]) {
@autoreleasepool {
return UIApplicationMain(argc, argv, nil, NSStringFromClass([AppDelegate class]));
}
}
答案 0 :(得分:2)
我刚刚创建了一个与您类似的示例项目并让我的电影播放并使用了MPMoviePlayerController类。
我启动了Xcode并基于Single View Application模板创建了一个新的iOS应用程序项目。
我从vimeo中随机抽取了一个样本电影文件,将其下载并复制到我的项目中,并将其复制到电影&#34; movie.mp4&#34;。
在ViewController.m中,我遵循以下三个步骤:
<MediaPlayer/MediaPlayer.h>>
添加了一个电影播放器属性:
@property(nonatomic)MPMoviePlayerController * moviePlayer;
我添加了以下代码:
- (void)viewDidLoad {
[super viewDidLoad];
NSString *videoPath = [[NSBundle mainBundle] pathForResource:@"movie" ofType:@"mp4"];
NSURL *videoURL = [NSURL fileURLWithPath:videoPath];
self.moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:videoURL];
self.moviePlayer.controlStyle = MPMovieControlStyleDefault;
self.moviePlayer.shouldAutoplay = YES;
[self.moviePlayer setFullscreen:YES animated:YES];
UIView *videoView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 100)];
[self.moviePlayer.view setFrame:videoView.bounds];
[videoView addSubview:self.moviePlayer.view];
[self.view addSubview:videoView];
[self.moviePlayer prepareToPlay];
[self.moviePlayer play];
}
我建立并运行,我能够观看电影并全屏播放。 我在设备上测试了电影。 如果你做到这一点,那么你可以适应你的特定用户界面。 希望这会有所帮助。