2013-07-10 28 views
7

Tôi có đoạn mã này:iOS chơi video với MPMoviePlayerController

theMoviPlayer = [[MPMoviePlayerController alloc] initWithContentURL:[NSURL fileURLWithPath:@"/Resources/disc.mp4"]]; 
    theMoviPlayer.controlStyle = MPMovieControlStyleFullscreen; 
    theMoviPlayer.view.transform = CGAffineTransformConcat(theMoviPlayer.view.transform, CGAffineTransformMakeRotation(M_PI_2)); 
    UIWindow *backgroundWindow = [[UIApplication sharedApplication] keyWindow]; 
    [theMoviPlayer.view setFrame:backgroundWindow.frame]; 
    [backgroundWindow addSubview:theMoviPlayer.view]; 
    [theMoviPlayer play]; 

Nhưng tôi thật sự không biết làm thế nào để thêm video vào dự án của tôi. Trong thư mục nào tôi phải đặt tập tin video? Hay tôi phải làm gì đó để thêm nó vào dự án của tôi?

EDIT:

Có vẻ như thế này trong xcode, có chính xác không? Vì tôi gặp lỗi phát lại ngay bây giờ. Trước đây tôi sử dụng một địa chỉ để chơi video này và điều này đã làm việc khá tốt, nhưng với tập tin này tại địa phương không :(

enter image description here

Trả lời

13

Ok đường bó của bạn trông jacked, dưới đây nên làm việc.

NSBundle *bundle = [NSBundle mainBundle]; 
NSString *moviePath = [bundle pathForResource:@"disc" ofType:@"mp4"]; 
NSURL *movieURL = [[NSURL fileURLWithPath:moviePath] retain]; 

theMoviPlayer = [[MPMoviePlayerController alloc] initWithContentURL:movieURL]; 
theMoviPlayer.controlStyle = MPMovieControlStyleFullscreen; 
theMoviPlayer.view.transform = CGAffineTransformConcat(theMoviPlayer.view.transform, CGAffineTransformMakeRotation(M_PI_2)); 
UIWindow *backgroundWindow = [[UIApplication sharedApplication] keyWindow]; 
[theMoviPlayer.view setFrame:backgroundWindow.frame]; 
[backgroundWindow addSubview:theMoviPlayer.view]; 
[theMoviPlayer play]; 
+0

Hoạt động! Cảm ơn nhiều. Tôi khá mới phát triển iOS. Whats lý do mà cách tiếp cận của tôi đã không làm việc như mong đợi? – krackmoe

+0

Nếu bạn muốn sử dụng UIWebView, bạn có thể sử dụng HTML5 để phát video nếu bạn cảm thấy thoải mái hơn với video đó. Tôi sẽ đính kèm mã bên dưới. – ApolloSoftware

+0

đính kèm nó như là một câu trả lời nếu bạn muốn chơi xung quanh với nó. – ApolloSoftware

0

Vì bạn đang sử dụng MPMoviePlayerController và không UIWebView bạn có thể đặt mp4 hoặc tập tin của bạn trong Tài nguyên và XCode/iOS sẽ tìm thấy nó Hãy chắc chắn rằng thư mục/nhóm mà tập tin dưới màu vàng không phải là màu xanh da trời Bạn không muốn nó là một đường dẫn tương đối. Sao chép các mục vào đích được chọn, Tùy chọn thư mục đầu tiên được chọn và quan trọng nhất là thêm vào mục tiêu!

Ok thử đoạn code dưới đây:

NSBundle *bundle = [NSBundle mainBundle]; 
NSString *moviePath = [bundle pathForResource:@"disc" ofType:@"mp4"]; 
NSURL *movieURL = [[NSURL fileURLWithPath:moviePath] retain]; 

theMoviPlayer = [[MPMoviePlayerController alloc] initWithContentURL:movieURL]; 
theMoviPlayer.controlStyle = MPMovieControlStyleFullscreen; 
theMoviPlayer.view.transform = CGAffineTransformConcat(theMoviPlayer.view.transform, CGAffineTransformMakeRotation(M_PI_2)); 
UIWindow *backgroundWindow = [[UIApplication sharedApplication] keyWindow]; 
[theMoviPlayer.view setFrame:backgroundWindow.frame]; 
[backgroundWindow addSubview:theMoviPlayer.view]; 
[theMoviPlayer play]; 
+1

Cũng nên là nhận được đường dẫn sử dụng 'NSBundle' – Wain

+0

đã đồng ý, Wain .... – ApolloSoftware

+0

Tôi đã chỉnh sửa bài đăng chính của mình, vui lòng xem lại nó – krackmoe

1

Sử dụng HTML5 như tôi đã hứa ở trên:

NSString *videoTitle = @"disc.mp4"; 
    NSURL *baseURL = [NSURL fileURLWithPath:[[NSBundle mainBundle] bundlePath]]; 
    NSString *playPath = [NSString stringWithFormat:@"<center><video width=\"640\" height=\"480\" controls><source src=\"%@\" media=\"all and (max-width:1024px)\"></video></center>",videoTitle]; 


    [webView loadHTMLString:playPath baseURL:baseURL]; 

này sẽ chơi ở giải 640x480, nhưng nếu bạn đã quen thuộc với các thẻ video HTML5, bạn có thể tùy chỉnh khá nặng nề.

+0

Cảm ơn, rất vui được biết, ngay cả khi tôi không cần nó ngay bây giờ! – krackmoe

+0

Không nghi ngờ gì. Tôi sử dụng HTML5 để phát lại hầu hết thời gian. Một cái gì đó về nó làm cho nó cảm thấy sạch hơn. Plus với WebKit nó cho phép tùy biến nhiều hơn so với khung đa phương tiện của iOS. – ApolloSoftware

8

Thêm MediaPlayer Khung

nhập nó vào tập tin của bạn

#import <MediaPlayer/MediaPlayer.h> 

Tạo một đối tượng của MPMoviePlayerController

MPMoviePlayerController * moviePlayer; 

ghi mã này, nơi bạn muốn chơi video

NSString *filepath = [[NSBundle mainBundle] pathForResource:@"spacetest.mp4" ofType:nil]; 
NSURL *fileURL = [NSURL fileURLWithPath:filepath]; 
moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:fileURL]; 
[self.view addSubview:moviePlayer.view]; 
moviePlayer.fullscreen = YES; 
[moviePlayer play]; 
Các vấn đề liên quan