Playing video from NSData in objective c

别说谁变了你拦得住时间么 提交于 2019-12-24 10:13:10

问题


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!


回答1:


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

STEP 1:Add the AVKit framework in Target->BuilPhases->Link Binary With Libraries

STEP 2:Then import in ViewController.Now it looks like below.

ViewController.h

#import <AVKit/AVKit.h>

STEP 3:Declare the AVPlayerViewController as Strong in 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 the path from cache and convert to URL.Then finally play the video

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


来源:https://stackoverflow.com/questions/44093958/playing-video-from-nsdata-in-objective-c

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!