我正在尝试播放存储在我的设备上的视频,同时尝试AVPlayer
和MPMoviePlayerController
。
视频URl是:file:///var/mobile/Containers/Data/Application/73695F3E-9351-447B-BB8A-0B4A62CE430F/Documents/08-03-201711:48:50.3gp
。现在问题是我得到了空白屏幕。
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 :(得分:8)
我会给你很好的解决方案。它完美无缺。
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;
}
答案 1 :(得分: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
播放本地文件。
添加Mediaplayer框架并在viewController中执行#import。
将您在桌面上创建的视频文件拖放到xcode中。
获取本地视频的路径。
NSString thePath = [[NSBundle mainBundle] pathForResource:@“yourVideo”ofType:@“MOV”]; NSURL theurl = [NSURL fileURLWithPath:thePath];
使用您的路径初始化moviePlayer。
self.moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:theurl]; [self.moviePlayer.view setFrame:CGRectMake(40,197,240,160)]; [self.moviePlayer prepareToPlay]; [self.moviePlayer setShouldAutoplay:NO]; //您可以通过文档查看其他选项。 [self.view addSubview:self.moviePlayer.view];
控制播放后要执行的操作:
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(playBackFinished :) name:MPMoviePlayerPlaybackDidFinishNotification object:moviePlayer];
// playBackFinished将是您自己的方法。
答案 2 :(得分:0)
由于您的文件网址不正确,可能会发生这种情况。
尝试以下代码来获取答案的路径。
如果你的mac中有视频,那么首先将它复制到你的项目中,然后将它吸毒到Xcode。
NSString *filepath = [[NSBundle mainBundle] pathForResource:@"08-03-201711:21:67" ofType:@"3gp"];
NSURL *fileURL = [NSURL fileURLWithPath:filepath];
并将此路径用于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];
**当我查看您的代码时,您可能会将AVPlayer添加到图层中而不是作为子视图/子视图控件添加到空白屏幕。所以试试我的代码!