2012-09-25 82 views
1

我之前沒有在iOS中播放視頻文件,所以我不確定我是否僅僅缺少一些東西。玩家可以打開,但它會卡住加載動畫,我無法點擊完成按鈕。難道我的視頻文件不支持?我把它編碼爲ipad。本地視頻不會在iOS中播放6 iPad應用程序

#import "ViewController.h" 

@interface ViewController() 

@end 

@implementation ViewController 

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

    UIButton *video_btn = [UIButton buttonWithType:UIButtonTypeRoundedRect]; 
    video_btn.frame = CGRectMake(0, 0, 100, 50); 
    [video_btn setTitle:@"Moonrise" forState:UIControlStateNormal]; 
    [video_btn addTarget:self action:@selector(playMovie:) forControlEvents:UIControlEventTouchUpInside]; 
    [self.view addSubview:video_btn]; 



} 

-(IBAction)playMovie:(id)sender 
{ 

    NSString *filepath = [[NSBundle mainBundle] pathForResource:@"moonrise" ofType:@"mp4"]; 
    NSURL *fileURL = [NSURL fileURLWithPath:filepath]; 
    MPMoviePlayerController *moviePlayerController = [[MPMoviePlayerController alloc] initWithContentURL:fileURL]; 
    [self.view addSubview:moviePlayerController.view]; 
    moviePlayerController.fullscreen = YES; 
    [moviePlayerController play]; 

} 

- (void)didReceiveMemoryWarning 
{ 
    [super didReceiveMemoryWarning]; 
    // Dispose of any resources that can be recreated. 
} 

@end 

我得到這個作爲輸出:

2012-09-25 12:47:56.206 App[4895:c07] [MPAVController] Autoplay: Disabling autoplay for pause 
2012-09-25 12:47:56.207 App[4895:c07] [MPAVController] Autoplay: Disabling autoplay 
2012-09-25 12:47:56.221 App[4895:c07] [MPAVController] Autoplay: Disabling autoplay for pause 
2012-09-25 12:47:56.222 App[4895:c07] [MPAVController] Autoplay: Disabling autoplay 
2012-09-25 12:47:56.226 App[4895:c07] [MPAVController] Autoplay: Skipping autoplay, disabled (for current item: 1, on player: 0) 
2012-09-25 12:47:56.255 App[4895:c07] [MPAVController] Autoplay: Enabling autoplay 

回答

0

嘗試設置你的MPMoviePlayerController作爲屬性。

@property (strong, nonatomic) MPMoviePlayerController *moviePlayerController; 

自動關閉你必須設置你的MPMoviePlayerController的通知。像這樣:

[[NSNotificationCenter defaultCenter] addObserver:self 
             selector:@selector(videoFinishedCallback:) 
              name:MPMoviePlayerPlaybackDidFinishNotification 
              object:moviePlayerController]; 

這將在視頻結束時調用函數 - videoFinishedCallback。 然後,您執行回調:

- (void) videoFinishedCallback:(NSNotification*)aNotification 
{ 
    //remove notification 
    [[NSNotificationCenter defaultCenter] removeObserver:self 
                name:MPMoviePlayerPlaybackDidFinishNotification 
                object:object:moviePlayerController]; 

    // here you set your code to remove the video 
    // for example, you can remove from superview and set value to nil 
} 
+0

謝謝。我如何讓視頻自動關閉? – Adam

+0

我編輯回覆您的評論。 – Yudmt