2014-10-07 286 views
0

我試圖在視圖加載時播放一個簡單的mp4文件全屏。這裏是代碼:iOS無法播放視頻

我沒有得到一個視頻,但我知道viewDidLoad被調用,並且存在一個視圖。

// 
// FirstViewController.m 
// WSTR Finale 
// 
// Created by Chris Muench on 10/6/14. 
// Copyright (c) 2014 World Series of Team Roping. All rights reserved. 
// 

#import "WatchLiveViewController.h" 
#import <MediaPlayer/MediaPlayer.h> 

@interface WatchLiveViewController() 

@end 

@implementation WatchLiveViewController 

- (void)viewDidLoad { 
    [super viewDidLoad]; 
    // Do any additional setup after loading the view, typically from a nib. 
    NSURL *movieURL = [NSURL URLWithString:@"http://download.wavetlan.com/SVV/Media/HTTP/H264/Talkinghead_Media/H264_test1_Talkinghead_mp4_480x360.mp4"]; 
    MPMoviePlayerController *moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:movieURL]; 
    moviePlayer.controlStyle = MPMovieControlStyleFullscreen; 
    moviePlayer.view.transform = CGAffineTransformConcat(moviePlayer.view.transform, CGAffineTransformMakeRotation(M_PI_2)); 
    UIWindow *backgroundWindow = [[UIApplication sharedApplication] keyWindow]; 
    [moviePlayer.view setFrame:backgroundWindow.frame]; 
    [backgroundWindow addSubview:moviePlayer.view]; 
    [moviePlayer play]; 
} 

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

@end 
+0

是否沒有變換工作?您應該考慮使用backgroundWindow.bounds而不是.frame來避免重複偏移。 – 2014-10-07 15:56:11

+0

我確定在將實際視圖控制器放入導航堆棧之前,您已經知道「框架」和「邊界」屬性是未定義的(甚至可能是CGRectNull) - 並且調用了「-viewDidLoad」方法絕對_before_。 – holex 2014-10-07 16:02:57

+0

我試圖改變它viewDidAppear現在我只是得到一個大的黑色屏幕沒有任何視頻控制器或視頻 – 2014-10-07 16:05:11

回答

1

試試這個代碼:

MPMoviePlayerViewController *mpvController = [[MPMoviePlayerViewController alloc] initWithContentURL:movieURL]; 
mpvController.moviePlayer.controlStyle = MPMovieControlStyleFullscreen; 
mpvController.moviePlayer.scalingMode = MPMovieScalingModeFill; 
mpvController.view.transform = CGAffineTransformConcat(mpvController.view.transform, CGAffineTransformMakeRotation(M_PI_2)); 
[self presentViewController:mpvController animated:NO completion:^{ 
    [mpvController.moviePlayer play]; 
}]; 
+0

作品非常感謝! – 2014-10-07 17:15:26

+0

你可以在肖像中做這個戲嗎? – 2014-10-07 17:18:14

+0

是的,註釋掉mpvController.view.transform行 – gabbler 2014-10-07 17:33:42

0

我想你是失蹤prepareToPlay。嘗試是這樣的:

... 
[moviePlayer prepareToPlay]; 
[moviePlayer play]; 
+0

我試過,但它仍然沒有播放......只是一個空視圖 – 2014-10-07 16:03:45

+0

我也試圖改變它以查看出現,現在我只是得到一個大的黑色屏幕沒有任何視頻控制器或視頻 – 2014-10-07 16:05:29