以 iOS 播放视频
Play Video in iOS
我正在尝试播放存储在我设备上的视频,同时尝试 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];
我认为问题出在 link。我正在做的是获取本地目录 link 并将名称附加到 link.
编辑:MPMoviePlayerController 现已弃用。所以我使用了 AVPlayerViewController。并编写了以下代码:
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。
获取本地视频路径
NSStringthePath=[[NSBundle mainBundle] pathForResource:@"yourVideo" ofType:@"MOV"];
NSURLtheurl=[NSURL fileURLWithPath:thePath];
用你的路径初始化moviePlayer。
self.moviePlayer=[[MPMoviePlayerController 分配] 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(playBackFinished:) name:MPMoviePlayerPlaybackDidFinishNotification object:moviePlayer];
//playBackFinished 将是您自己的方法。
编辑 2:要处理 AVPlayerViewController
而不是 MPMoviePlayerController
的完成,请使用以下...
AVPlayerItem *playerItem = player.currentItem;
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(playBackFinished:) name:AVPlayerItemDidPlayToEndTimeNotification object:playerItem];
在此示例中,我在完成后关闭 AVPlayerViewController:
-(void)playBackFinished:(NSNotification *) notification {
// Will be called when AVPlayer finishes playing playerItem
[playerViewController dismissViewControllerAnimated:false completion:nil];
}
这可能是因为您的文件 url 不正确。
尝试以下代码来获取答案的路径。
如果您的 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 添加到层中而不是 subview/childviewcontroller。所以试试我的代码吧!
我会好好的 solution.It 完美无缺。
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;
}
我正在尝试播放存储在我设备上的视频,同时尝试 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];
我认为问题出在 link。我正在做的是获取本地目录 link 并将名称附加到 link.
编辑:MPMoviePlayerController 现已弃用。所以我使用了 AVPlayerViewController。并编写了以下代码:
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。
获取本地视频路径
NSStringthePath=[[NSBundle mainBundle] pathForResource:@"yourVideo" ofType:@"MOV"]; NSURLtheurl=[NSURL fileURLWithPath:thePath];
用你的路径初始化moviePlayer。
self.moviePlayer=[[MPMoviePlayerController 分配] 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(playBackFinished:) name:MPMoviePlayerPlaybackDidFinishNotification object:moviePlayer]; //playBackFinished 将是您自己的方法。
编辑 2:要处理 AVPlayerViewController
而不是 MPMoviePlayerController
的完成,请使用以下...
AVPlayerItem *playerItem = player.currentItem;
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(playBackFinished:) name:AVPlayerItemDidPlayToEndTimeNotification object:playerItem];
在此示例中,我在完成后关闭 AVPlayerViewController:
-(void)playBackFinished:(NSNotification *) notification {
// Will be called when AVPlayer finishes playing playerItem
[playerViewController dismissViewControllerAnimated:false completion:nil];
}
这可能是因为您的文件 url 不正确。
尝试以下代码来获取答案的路径。
如果您的 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 添加到层中而不是 subview/childviewcontroller。所以试试我的代码吧!
我会好好的 solution.It 完美无缺。
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;
}