在目标C中播放来自NSData的视频 [英] Playing video from NSData in objective c

查看:238
本文介绍了在目标C中播放来自NSData的视频的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将视频的NSdata存储在缓存中.我想在AVPlayer上播放此视频(如果ios有,则在其他播放器上播放),但无法理解我应以哪种格式转换NSData以便可以在AVplayer上播放.我不想将其保存在本地,然后将文件路径提供给avplayer播放视频. 请暗示还有其他方法可以播放来自NSData的视频.我已经花了3天时间,但仍未得出任何结论.请提出您的建议.任何帮助或建议都将不胜感激.谢谢!!

I have video's NSdata stored in my cache .I want to play this on AVPlayer (or some other player if ios has) but not able to understand that in which format shoul i convert NSData so that it can be played on AVplayer.I dont want to save it locally and then give filepath's to avplayer to play video. Kindly suggest is there any other way to play video from NSData.I have already spent 3 days on it but coudn't reach to any conclusion.Kindly give your suggestion.Any help or suggestion would be appreciated.Thanks in advance!

推荐答案

我为您提供了从缓存中获取NSData并播放视频的解决方案.

I give you the solution which gets the NSData from cache and plays the video.

步骤1:在Target-> BuilPhases-> Link Binary With Libraries中添加AVKit框架

第2步:然后将其导入ViewController.现在看起来像下面这样.

ViewController.h

ViewController.h

#import <AVKit/AVKit.h>

步骤3:在ViewController.h中将AVPlayerViewController声明为强"

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

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

@end

步骤4:从缓存中获取路径并转换为URL.然后最终播放视频

ViewController.m

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;
}

这篇关于在目标C中播放来自NSData的视频的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆