7

Tôi đang sử dụng đoạn mã sau để phát video bằng MPMoviePlayerController, nhưng video không được phát. Bất cứ ai đó có thể trả lời tôi tại sao ?Cách phát video bằng MPMoviePlayerController?

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
NSString *documentsDirectory = [paths objectAtIndex:0]; 
NSString *filePath = [documentsDirectory stringByAppendingPathComponent:@"/one.mp4"]; 

NSString *mediaPath = [[[NSBundle mainBundle]resourcePath] stringByAppendingPathComponent:filePath]; 

    MPMoviePlayerController *moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:[NSURL fileURLWithPath:mediaPath]]; 

    [[moviePlayer view] setFrame:[[self view] bounds]]; 
    [[self view] addSubview: [moviePlayer view]]; 


    moviePlayer.scalingMode = MPMovieScalingModeAspectFit; 

    [moviePlayer play]; 
+0

Bạn đã chỉ định phần mở rộng của tệp chưa? –

Trả lời

20

Khá lạ, nhưng có vẻ như không sao nếu bạn tạo MPMoviePlayerController thành thuộc tính thay vì biến cục bộ. Có vẻ như có điều gì đó đang diễn ra đằng sau hậu trường. Tôi nghĩ nó liên quan đến ARC. Bạn đang sử dụng ARC?

Đó cũng là một vấn đề mà bạn đã quá nối con đường của bạn:

// You've already got the full path to the documents directory here. 
NSString *filePath = [documentsDirectory stringByAppendingPathComponent:@"/one.mp4"]; 
// Now you're appending the full path to the documents directory to your bundle path 
NSString *mediaPath = [[[NSBundle mainBundle]resourcePath] stringByAppendingPathComponent:filePath]; 

Khi tôi chạy mã của bạn trong mô phỏng, con đường trông như thế này:

/Users/mlong/Library/Application Support/iPhone Simulator/5.1/Applications/8CFB9B94-BD6A-442C-A525-573FE343506D/VidoePlayer.app/Người dùng/mlong/Library/Hỗ trợ ứng dụng/iPhone Simulator/5.1/Applications/8CFB9B94-BD6A-442C -A525-573FE343506D/Tài liệu/one.mp4

Nó nên chỉ là thế này:

/Users/mlong/Library/Application Support/iPhone Simulator/5.1/Applications/8CFB9B94-BD6A-442C-A525-573FE343506D/Documents/one.mp4

Vì vậy, chỉ cần xóa dòng này:

NSString *mediaPath = [[[NSBundle mainBundle]resourcePath] stringByAppendingPathComponent:filePath]; 

Và sau đó thay đổi instantiation nghe nhạc của bạn như thế này:

_moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:[NSURL fileURLWithPath:filePath]]; 

[[_moviePlayer view] setFrame:[[self view] bounds]]; 
[[self view] addSubview: [_moviePlayer view]]; 

_moviePlayer.scalingMode = MPMovieScalingModeAspectFit; 

[_moviePlayer play]; 

Vì vậy, bạn nên thêm MPMoviePlayerController làm thuộc tính của trình điều khiển chế độ xem có chứa.

+0

Dường như bạn có một mô tả rõ ràng nhất định. Tôi đã giải quyết vấn đề này từ lâu. Vấn đề là thiết lập khung và thêm preparetoPlay và toàn màn hình là YES. – Perseus

+0

@Perseus Bạn đã giải quyết chính xác điều này như thế nào? –

+0

Xin chào @Ancientar. Bạn có thể thấy trong bình luận ở trên mà tôi đã đề cập đến, tôi quên đặt frame cho MPMoviePlayerController và cũng đã thêm một thuộc tính prepareToPlay. Nó làm việc – Perseus

0

Thử hỏi bó của bạn trực tiếp và không thiết lập các đường dẫn tập tin bằng tay

NSString *path = [[NSBundle mainBundle] pathForResource:@"name" ofType:@"mov"]; 
-1
MPMoviePlayerController *moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:[NSURL fileURLWithPath:mediaPath]]; 
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(movieFinishedCallback:) name:MPMoviePlayerPlaybackDidFinishNotification object:moviePlayer]; 

hy vọng giúp nó

3

Được rồi, có một sự khác biệt lớn giữa các gói ứng dụng và thư mục tài liệu. Tôi đề nghị bạn hãy xem nó.

Trước hết, Video được lưu trữ ở đâu?

Nếu video của bạn nằm trong thư mục tài liệu, đừng thêm đường dẫn thư mục tài liệu vào đường dẫn gói.

Chỉ cần cố gắng với biến filePath:

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES); 
NSString *documentsDirectory = [paths objectAtIndex:0]; 
NSString *filePath = [documentsDirectory stringByAppendingPathComponent:@"/one.mp4"]; 

MPMoviePlayerController *moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:[NSURL filePath]]; 

Tuy nhiên, nếu tập tin là trong gói ứng dụng (bạn thêm nó vào dự án của bạn trong XCode), bạn nên sử dụng những gì có trong jinx phản ứng.

+0

Tôi cũng đã thử với FilePath, màn hình trở nên đen nhưng video không phát. – Perseus

+0

FYI, tôi sẽ không thêm các tệp vào dự án của mình, tôi muốn lấy nó từ thư mục Tài liệu chỉ – Perseus

+0

Bất kỳ ví dụ rất đơn giản nào về cách phát video bằng MPMoviePlayerController sẽ khá đủ. Tôi sẽ thực sự biết ơn bạn nếu bạn giúp tôi giải quyết vấn đề này – Perseus

1
moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:url]; 
     [moviePlayer.view setFrame:CGRectMake(//set rect frame)]; 
     moviePlayer.controlStyle = MPMovieControlStyleDefault; 
     moviePlayer.shouldAutoplay=YES; 
     moviePlayer.repeatMode = NO; 
     [moviePlayer setFullscreen:YES animated:NO]; 
     [moviePlayer prepareToPlay]; 
[self.view addsubview:movieplayer.view]; 

    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(MPMoviePlayerLoadStateDidChange:) name:MPMoviePlayerLoadStateDidChangeNotification object:nil]; 


- (void)MPMoviePlayerLoadStateDidChange:(NSNotification *)notification { 

    if ((moviePlayer.loadState & MPMovieLoadStatePlaythroughOK) == MPMovieLoadStatePlaythroughOK) { 
//add your code 


    } 
} 
-1

Tôi mất vài phút để gỡ lỗi sự cố nhưng câu trả lời khá đơn giản.Đây là thỏa thuận:

  • Nếu bạn muốn MPMoviePlayerViewController để chơi từ một URL web sử dụng này: NSURL * url = [NSURL URLWithString: @ "https://www.test.com/anyMovie.mp4"];

  • Và nếu bạn muốn MPMoviePlayerViewController để chơi nó từ App Bundle sử dụng này: NSString * moviePath = [[NSBundle mainBundle] pathForResource: @ "anyMovie" ofType: @ "m4v"]; NSURL * url = [NSURL fileURLWithPath: moviePath];

Phần còn lại của nó đi cùng, ngoại trừ bạn phải thiết lập thuộc tính này "movieSourceType" như sau:

MPMoviePlayerViewController *moviePlayerView = [[MPMoviePlayerViewController alloc] initWithContentURL:url]; 
moviePlayerView.moviePlayer.movieSourceType = MPMovieSourceTypeFile; 
[self presentViewController:moviePlayerView animated:YES completion:^{}]; 
0

Player được khởi tạo với URL của video mà chúng tôi muốn được chơi (Nó có thể là đường dẫn của tệp cục bộ của thiết bị hoặc URL trực tiếp.) Sau khi trình phát đó được thêm dưới dạng chế độ xem phụ của chế độ xem hiện tại.

Hỗ trợ định dạng video bằng lớp MPMoviePlayerController đang theo dõi

  1. mov .mpv .3gp .mp4

Tôi không chắc chắn có bao nhiêu bạn bài viết này sẽ giúp bạn ra ngoài. Tôi mới đến điều này. Tôi đã làm việc theo các hướng dẫn từng bước được cung cấp trong this article