2017-02-09 4 views
1

AVPlayerMPMoviePlayerControllerの両方を試して自分のデバイスに保存されているビデオを再生しようとしています。 ビデオUR1はfile:///var/mobile/Containers/Data/Application/73695F3E-9351-447B-BB8A-0B4A62CE430F/Documents/08-03-201711:48:50.3gpです。今問題は、私は空白の画面が表示されます。iOSでビデオを再生

AVPlayer *player = [AVPlayer playerWithURL:fileURL]; 
AVPlayerViewController *playerViewController = [AVPlayerViewController new]; 
[player pause]; 
[player play]; 
playerViewController.player = player; 

[self addChildViewController: playerViewController]; 
[self.view addSubview: playerViewController.view]; 
//[playerViewController.player play];//Used to Play On start 
[self presentViewController:playerViewController animated:YES completion:nil]; 

私は問題がリンクしていると思います。私がやっていることは、ローカルディレクトリのリンクを取得し、そのリンクに名前を追加することです。

+0

uがiOSの10の上または下にそれをテストしていますか? – Tejas

+0

http://stackoverflow.com/a/40192431/6656894この回答を参考にしてください –

+0

もう一つの良い答えhttps://stackoverflow.com/a/9802543/444966 – iutinvg

答えて

4

私はあなたに良い解決策を提供します。それは完璧に動作します。

ViewController.h

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

@interface ViewController : UIViewController 
@property (strong, nonatomic) AVPlayerViewController *playerViewController; 
- (IBAction)actionPlayVideo:(id)sender; 

@end 

ViewController.m

#import "ViewController.h" 

@interface ViewController(){ 
    NSURL *vedioURL; 
} 

@end 

@implementation ViewController 
@synthesize playerViewController; 

- (IBAction)actionPlayVideo:(id)sender{ 
    NSString *fullpath = [[self documentsDirectory] stringByAppendingPathComponent:@"yourdate.3gp"]; 
    vedioURL =[NSURL fileURLWithPath:fullpath]; 
    AVPlayerItem* playerItem = [AVPlayerItem playerItemWithURL:vedioURL]; 
    AVPlayer* playVideo = [[AVPlayer alloc] initWithPlayerItem:playerItem]; 
    playerViewController = [[AVPlayerViewController alloc] init]; 
    playerViewController.player = playVideo; 
    playerViewController.player.volume = 0; 
    playerViewController.view.frame = self.view.bounds; 
    [self.view addSubview:playerViewController.view]; 
    [playVideo play]; 
} 
-(NSString *)documentsDirectory{ 
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
    NSString *documentsDirectory = [paths objectAtIndex:0]; 
    return documentsDirectory; 
} 
+0

ありがとう、それは動作します – taimur

+0

ようこそtaimur :-) – user3182143

0

使用このコード

NSURL *videoURL = [NSURL fileURLWithPath:filePath]; 
//filePath may be from the Bundle or from the Saved file Directory, it is just the path for the video 
    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> 

あなたは、ローカルファイルを再生するためにMPMoviePlayerControllerを使用することができます。

  1. あなたのviewControllerに#importを追加してください。

  2. デスクトップで作成したビデオファイルをxcodeにドラッグアンドドロップします。

  3. ローカルビデオのパスを取得します。

    NSString thePath = [[NSBundle mainBundle] pathForResource:@ "yourVideo" ofType:@ "MOV"]; NSURL theurl = [NSURL fileURLWithPath:thePath];

  4. パスでmoviePlayerを初期化します。

    self.movi​​ePlayer = [[MPMoviePlayerController alloc] initWithContentURL:theurl]; [self.movi​​ePlayer.view setFrame:CGRectMake(40、197、240、160)]; [self.movi​​ePlayer prepareToPlay]; [self.movi​​ePlayer setShouldAutoplay:NO]; //そして、ドキュメントを見ることができる他のオプション。 [self.view addSubview:self.movi​​ePlayer.view];

  5. 再生後に行われるべきであるかを管理するには[[NSNotificationCenter defaultCenter] addObserver:自己セレクタ:@selector(playBackFinished :)名:MPMoviePlayerPlaybackDidFinishNotificationオブジェクト:のMoviePlayer]。

// playBackFinishedはあなた自身の方法になります。

参考:https://stackoverflow.com/a/9802543/4033273

+0

再生ビデオのMPMoviePlayerControllerを使用する必要はありません。あなたはまたavplayerでビデオを再生することができます –

+0

MPMoviePlayerなしでも、あなたが確認することができる答えが記載されています。代わりの –

+0

私のビデオのURLは:file:/// var/mobile/Containers/Data/Application/B3B93026-5CA4-4119-A206-06BF06A0DD75/Documents/08-03-201711:38:25.mp4 – taimur

0

ファイルのURLを間違っているので、それが起こることがあります。

答えのパスを取得するには、以下のコードを試してください。

あなたのMacに動画がある場合は、まずそれをあなたのプロジェクトにコピーしてください。ちょうどその動画をXcodeに送ってください。

NSString *filepath = [[NSBundle mainBundle] pathForResource:@"08-03-201711:21:67" ofType:@"3gp"]; 

NSURL *fileURL = [NSURL fileURLWithPath:filepath]; 

そして、私はあなたのコード、空白の画面では、サブビュー/ childviewcontrollerとして層にそれAVPlayer追加することではなくによって引き起こされるかもしれレビューとして** AVPlayer

AVPlayer *player = [AVPlayer playerWithURL:fileURL]; 
AVPlayerViewController *playerViewController = [AVPlayerViewController new]; 
[player pause]; 
[player play]; 
playerViewController.player = player; 

[self addChildViewController: playerViewController]; 
[self.view addSubview: playerViewController.view]; 

にこのパスを使用します。だから私のコードを試してください!

+0

@taimur試してみてください。 – KTang

+0

お返事ありがとうございます – taimur

+0

それは動作しませんか? – KTang

関連する問題