Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/mongodb/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Objective c 如何在AVPlayer中播放NSData?_Objective C_Avplayer - Fatal编程技术网

Objective c 如何在AVPlayer中播放NSData?

Objective c 如何在AVPlayer中播放NSData?,objective-c,avplayer,Objective C,Avplayer,我使用同源poolid从AWS服务器下载了视频NSData。将NSData转换为NSURL后,尝试在AVPlayer中播放NSData。但它不是在玩 在接口中,我定义了 @property (nonatomic, strong) AVPlayer *avPlayer; 这是密码 NSData *data = [UserProfileInfo getVideoData]; NSLog(@"%lu", (unsigned long)data.length); NSString *dataStri

我使用同源poolid从AWS服务器下载了视频
NSData
。将
NSData
转换为
NSURL
后,尝试在
AVPlayer
中播放
NSData
。但它不是在玩

在接口中,我定义了

@property (nonatomic, strong) AVPlayer *avPlayer;
这是密码

NSData *data = [UserProfileInfo getVideoData];
NSLog(@"%lu", (unsigned long)data.length);

NSString *dataString = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
_avPlayer = [AVPlayer playerWithURL:[NSURL URLWithString:dataString]];

_avPlayer.actionAtItemEnd = AVPlayerActionAtItemEndNone;
AVPlayerLayer *videoLayer = [AVPlayerLayer playerLayerWithPlayer:_avPlayer];
videoLayer.frame = videoPreviewView.bounds;
videoLayer.videoGravity = AVLayerVideoGravityResizeAspectFill;
[videoPreviewView.layer addSublayer:videoLayer];
[videoPreviewView.layer setOpacity:1.0];
[_avPlayer play];
实际上,我在这里获取数据,但当我将其转换为
NSString

NSString *dataString = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
然后我在数据字符串中得到
nil

请找人帮忙。

您需要将NSDATA保存在文档目录中,并将其路径提供给AVPlayer以便播放

    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
            NSString *documentsDirectory = [paths objectAtIndex:0];
            NSString *myPathDocs = [documentsDirectory stringByAppendingPathComponent:@"myVideo.mp4"];
            [UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
            BOOL isFileWriteComplete = [yourNSdata writeToFile:myPathDocs atomically:YES];
    if(isFileWriteComplete)
    {
_avPlayer = [AVPlayer playerWithURL:[NSURL URLWithString:myPathDocs]];
_avPlayer.actionAtItemEnd = AVPlayerActionAtItemEndNone;
AVPlayerLayer *videoLayer = [AVPlayerLayer playerLayerWithPlayer:_avPlayer];
videoLayer.frame = videoPreviewView.bounds;
videoLayer.videoGravity = AVLayerVideoGravityResizeAspectFill;
[videoPreviewView.layer addSublayer:videoLayer];
[videoPreviewView.layer setOpacity:1.0];
[_avPlayer play];
    }

添加您的UserProfileInfo数据UserProfileInfo是我的模型类,我已在其中存储下载的NSData。我使用getVideoData方法获取数据变量中的NSData。