MPMoviePlayerController 不播放本地文件

Posted

技术标签:

【中文标题】MPMoviePlayerController 不播放本地文件【英文标题】:MPMoviePlayerController Not playing Local file 【发布时间】:2014-01-17 03:12:17 【问题描述】:
 NSString *path = [[NSBundle mainBundle] pathForResource:@"PTCL" ofType:@"mp4"];    
  NSURL *videoURL = [NSURL URLWithString:path];    
        MPMoviePlayerController *moviePlayerController = [[MPMoviePlayerController alloc] initWithContentURL:videoURL];    
        [moviePlayerController.view setFrame:self.view.frame];
        [self.view addSubview:moviePlayerController.view]; 
        moviePlayerController.controlStyle=MPMediaTypeAnyVideo;
        moviePlayerController.fullscreen = YES;
        [moviePlayerController prepareToPlay];
        [moviePlayerController play];

 NSString *path = [[NSBundle mainBundle] pathForResource:@"PTCL" ofType:@"mp4"];   

path=[NSString stringWithFormat:@"file:/%@",path];   
NSURL *videoURL = [NSURL URLWithString:path];

MPMoviePlayerController *moviePlayerController = [[MPMoviePlayerController alloc] initWithContentURL:videoURL];

[moviePlayerController.view setFrame:self.view.frame];
[self.view addSubview:moviePlayerController.view];

moviePlayerController.shouldAutoplay=YES;
moviePlayerController.controlStyle=MPMediaTypeAnyVideo;
moviePlayerController.fullscreen = YES;
[moviePlayerController prepareToPlay];
[moviePlayerController play];

file:///Users/utkal/Library/Application%20Support/iPhone%20Simulator/7.0.3/Applications/F0B3AD63-7E46-4069-8845-8B0C05425CD2/CosMos.app/PTCL.mp4

我正在获取第一个代码的这个 videoURL 路径

file:///Users/utkal/Library/Application%20Support/iPhone%20Simulator/7.0.3/Applications/F0B3AD63-7E46-4069-8845-8B0C05425CD2/CosMos.app/PTCL.mp4

我正在获取第二个代码的这个 videoURL 路径。

我也用过

NSURL *videoURL = [[NSURL alloc]init];
    videoURL = [[NSBundle mainBundle] URLForResource:@"PTCL" withExtension:@"mp4"];

但我的视频播放器总是显示文件正在加载,但没有任何反应。 我知道我在哪里做错了,但我的运气不好。 请纠正我的错误或告诉我是否有其他播放本地视频文件的方法。请。

【问题讨论】:

请尝试:NSURL * videoURL = [NSURL fileURLWithPath:path]; @Horst 这也不起作用。我也有同样的问题。 如果你解决了这个问题告诉我,我也有同样的问题。 任何解决方案? 【参考方案1】:

也许为时已晚,但我会尝试以下方法:

MPMoviePlayerViewController *movieVC = [[MPMoviePlayerViewController alloc] initWithContentURL:file.location];
movieVC.moviePlayer.movieSourceType = MPMovieSourceTypeFile;
movieVC.moviePlayer.fullscreen = YES;
movieVC.moviePlayer.allowsAirPlay = YES;
movieVC.moviePlayer.controlStyle = MPMovieControlStyleFullscreen;
[self presentMoviePlayerViewControllerAnimated:movieVC];

注意“movieVC.movi​​ePlayer.movi​​eSourceType = MPMovieSourceTypeFile;”

【讨论】:

对我来说还不算太晚;)这实际上为我解决了问题。【参考方案2】:

我改用了这个简单的解决方案:

NSURL *url = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"video" ofType:@"mp4"]];
MPMoviePlayerViewController* viewController = [[MPMoviePlayerViewController alloc] initWithContentURL:url];
[self presentMoviePlayerViewControllerAnimated:viewController];

【讨论】:

【参考方案3】:
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *dataPath = [documentsDirectory stringByAppendingPathComponent:@"CapturedMedia"];
NSString *filePath = [dataPath stringByAppendingPathComponent:@"/itemVideo.mp4"];
_moviePlayer =[[MPMoviePlayerViewController alloc] initWithContentURL:[NSURL fileURLWithPath:filePath]];
[[_moviePlayer view] setFrame:[[self view] bounds]];
[[_moviePlayer moviePlayer] prepareToPlay];
[[_moviePlayer moviePlayer] setShouldAutoplay:YES];
[[_moviePlayer moviePlayer] setControlStyle:2];
[[_moviePlayer moviePlayer] setRepeatMode:MPMovieRepeatModeNone];
[self presentMoviePlayerViewControllerAnimated:_moviePlayer];

您在 .h 文件中设置 MPMoviePlayerViewController 的属性。

【讨论】:

【参考方案4】:

MPMoviePlayerController 现已弃用。所以我使用了AVPlayerViewController。并编写了以下代码:

NSURL *videoURL = [NSURL fileURLWithPath:filePath];
AVPlayer *player = [AVPlayer playerWithURL:videoURL];
AVPlayerViewController *playerViewController = [AVPlayerViewController new];
playerViewController.player = player;
//[playerViewController.player play];//Used to Play On start
[self presentViewController:playerViewController animated:YES completion:nil];

请不要忘记导入以下框架:

#import <AVFoundation/AVFoundation.h>
#import <AVKit/AVKit.h>

希望这有帮助..

【讨论】:

【参考方案5】:

您的电影播放器​​代码看起来不错。路径有问题。只要尝试明确地去路径。并检查该文件是否存在。您可以查看以下 SO 问题供您参考:

How to play video using MPMoviePlayerController?

【讨论】:

是的,我的文件是他们的路径。【参考方案6】:

游戏有点晚了,但这是我的完整代码

在 *.h 中

@interface v1SupportTable : UITableViewController

    MPMoviePlayerController *moviePlayer1;

在 *.m 中

- (IBAction) playVideoBtn


        NSURL *videoURL = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"sample_movie" ofType:@"mp4"]];
        NSLog(@"videoURL: %@ ...", videoURL);

        moviePlayer1 =[[MPMoviePlayerController alloc] initWithContentURL:videoURL];


        [[moviePlayer1 view] setFrame: [self.view bounds]];  // frame must match parent view
        [self.view addSubview: [moviePlayer1 view]];
        [moviePlayer1 setFullscreen:YES]; 


        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(doneButtonClicked) name:MPMoviePlayerWillExitFullscreenNotification object:nil];

        [moviePlayer1 play];


    


    -(void)playMediaFinished:(NSNotification*)theNotification
    

        moviePlayer1=[theNotification object];
        [[NSNotificationCenter defaultCenter] removeObserver:self name:MPMoviePlayerPlaybackDidFinishNotification object:moviePlayer1];
        [moviePlayer1.view removeFromSuperview];

        //when finished dimiss window
        [moviePlayer1 stop];
        [moviePlayer1.view removeFromSuperview];

    

    -(void)doneButtonClicked
    
        //[self.navigationController setNavigationBarHidden:NO animated:NO];
        [moviePlayer1 stop];
        [moviePlayer1.view removeFromSuperview];

    

【讨论】:

以上是关于MPMoviePlayerController 不播放本地文件的主要内容,如果未能解决你的问题,请参考以下文章

MPMoviePlayerController 不播放本地视频

MPMoviePlayerController 随机不播放电影

MPMoviePlayerController 音频/视频不同步

MPMoviePlayerController 不播放本地文件

iOS:MPMoviePlayerController 完成按钮不起作用

MPMoviePlayerController 播放本地文件,但不通过 HTTP 远程播放