在 objective c 中播放来自 NSData 的视频
Playing video from NSData in objective c
我有视频的 NSData 并存储在我的缓存中。我想在 AVPlayer(或其他播放器,如果 iOS 有)上播放,但无法理解我应该将 NSData 转换为哪种格式,以便它可以在 AVplayer 上播放。我不想将其保存在本地,然后将文件路径提供给 AVplayer 来播放视频。
请建议是否有任何其他方法可以播放来自 NSData.I 的视频已经花了 3 天,但无法达到任何 conclusion.Kindly 给你的 suggestion.Any 帮助或建议是 appreciated.Thanks提前!
我给你从缓存中获取 NSData 并播放视频的解决方案。
STEP 1:Add Target->BuilPhases->Link Binary With Libraries
中的 AVKit 框架
STEP 2:Then 导入 ViewController.Now 如下所示。
ViewController.h
#import <AVKit/AVKit.h>
STEP 3:Declare AVPlayerViewController 在 ViewController.h
中表现出色
#import <UIKit/UIKit.h>
#import <AVKit/AVKit.h>
@interface ViewController : UIViewController
@property (strong, nonatomic) AVPlayerViewController *playerViewController;
- (IBAction)actionPlayVideo:(id)sender;
@end
STEP 4:Get缓存中的路径并转换为URL.Then最终播放视频
ViewController.m
#import "ViewController.h"
@interface ViewController (){
NSURL *vedioURL;
}
@end
@implementation ViewController
@synthesize playerViewController;
- (IBAction)actionPlayVideo:(id)sender{
NSString *strNSDataPath = [[self getNSDataFromCache] stringByAppendingPathComponent:@"YourSavedNSDataName"];
vedioURL =[NSURL fileURLWithPath:strNSDataPath];
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 *)getNSDataFromCache{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
NSString *cachesDirectory = [paths objectAtIndex:0];
return cachesDirectory;
}
我有视频的 NSData 并存储在我的缓存中。我想在 AVPlayer(或其他播放器,如果 iOS 有)上播放,但无法理解我应该将 NSData 转换为哪种格式,以便它可以在 AVplayer 上播放。我不想将其保存在本地,然后将文件路径提供给 AVplayer 来播放视频。 请建议是否有任何其他方法可以播放来自 NSData.I 的视频已经花了 3 天,但无法达到任何 conclusion.Kindly 给你的 suggestion.Any 帮助或建议是 appreciated.Thanks提前!
我给你从缓存中获取 NSData 并播放视频的解决方案。
STEP 1:Add Target->BuilPhases->Link Binary With Libraries
中的 AVKit 框架STEP 2:Then 导入 ViewController.Now 如下所示。
ViewController.h
#import <AVKit/AVKit.h>
STEP 3:Declare AVPlayerViewController 在 ViewController.h
中表现出色#import <UIKit/UIKit.h>
#import <AVKit/AVKit.h>
@interface ViewController : UIViewController
@property (strong, nonatomic) AVPlayerViewController *playerViewController;
- (IBAction)actionPlayVideo:(id)sender;
@end
STEP 4:Get缓存中的路径并转换为URL.Then最终播放视频
ViewController.m
#import "ViewController.h"
@interface ViewController (){
NSURL *vedioURL;
}
@end
@implementation ViewController
@synthesize playerViewController;
- (IBAction)actionPlayVideo:(id)sender{
NSString *strNSDataPath = [[self getNSDataFromCache] stringByAppendingPathComponent:@"YourSavedNSDataName"];
vedioURL =[NSURL fileURLWithPath:strNSDataPath];
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 *)getNSDataFromCache{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
NSString *cachesDirectory = [paths objectAtIndex:0];
return cachesDirectory;
}