2014-01-17 5 views
6
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]; 

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

私は最初のコードのために、このvideURLパスを取得していますMPMoviePlayerControllerは、ローカルファイルを再生していない

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

私はこの2番目のコードのvideURLパスを取得しています。

私はALOS

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

を使用しています。しかし、私のビデオプレーヤーは、常にファイルがロードされると、何も起こらないことを示しています。 私はどこで私が間違っているのか知っていますが、私の幸運です。 私の間違いを訂正してください、またはローカルビデオファイルを再生する別の方法があれば教えてください。

enter image description here

+4

@interface v1SupportTable : UITableViewController { MPMoviePlayerController *moviePlayer1; } 

してみてください:NSURL * videoURL = [NSURL fileURLWithPath:パス]; – Horst

+0

@Horstそれはどちらも動作しません。私は同じ問題を抱えている。 – user623396

+0

あなたにこの問題を教えてもらっても同じ問題があります。 –

答えて

0

ムービープレーヤーのためのあなたのコードは罰金を探しています。パスに何か問題があります。明示的にパスに移動してみてください。そのファイルが存在するかどうかを確認します。私が代わりにこのシンプルなソリューションを使用

How to play video using MPMoviePlayerController?

+0

私のファイルはパスです。 –

2

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

は多分手遅れですが、私のようなものしようとするだろう:

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]; 
をあなたはあなたの参照のためのSO質問以下を参照してくださいすることができます

「movieVC.movi​​ePlayer.movi​​eSourceType = MPMovieSourceTypeFile;」に注意してください。

+0

私にとってはあまりにも遅くない;)これは実際に私のためにそれを解決した。 – schnabler

2
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のプロパティを設定します。

2

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> 

希望これは...

0

少し遅れてゲームするのに役立ちますが、ここで私の完全なコード

だし、次のコードを書か

*。H * .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]; 

    } 
関連する問題