gpt4 book ai didi

iphone - AVPlayer 不是从 NSCachesDirectory 播放?

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:41:31 25 4
gpt4 key购买 nike

您好,我正在尝试从 NSCachesDirectory 播放视频文件。 Player 没有从 videoView 层的 NSCachesDirectory 加载文件。

是否可以从缓存目录内存中播放视频?请建议我...

我试过这个..

NSArray *myPathList = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);  
NSString *cachesDir = [myPathList objectAtIndex:0];
NSString *songPath = [[NSString alloc] initWithString: [cachesDir stringByAppendingPathComponent:[NSString stringWithFormat:@"Song.%@", downloadURL]]];

NSURL *pathurl = [[NSURL alloc] initFileURLWithPath:songPath];
NSLog(@"--pathurl---%@",pathurl);
avPlayer =[AVPlayer playerWithURL:pathurl];
self.avPlayerLayer =[AVPlayerLayer playerLayerWithPlayer:avPlayer];

if ( !([avPlayer status] == AVPlayerStatusReadyToPlay) && (dataReceivedSoFar.length >10000))
{

avPlayerLayer.frame =CGRectMake(0, 0, 320, 450);

[[self.videoView layer] addSublayer:avPlayerLayer];

[avPlayer play];

NSLog(@"Video IS Playing");

[alert dismissWithClickedButtonIndex:0 animated:YES];

}

最佳答案

KVO 方法。添加观察者:

self.player = [AVPlayer playerWithURL:url];
[self.player addObserver:self forKeyPath:@"status" options:0 context:&PlayerStatusContext];

观察值

- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object
change:(NSDictionary *)change context:(void *)context {

if (context == &PlayerStatusContext) {
AVPlayer *thePlayer = (AVPlayer *)object;
if ([thePlayer status] == AVPlayerStatusFailed) {
NSError *error = [self.player error];
// Respond to error: for example, display an alert sheet.
return;
}
// Deal with other status change if appropriate.
if ([thePlayer status] == AVPlayerStatusReadyToPlay) {
[self play];
}
}
// Deal with other change notifications if appropriate.
[super observeValueForKeyPath:keyPath ofObject:object
change:change context:context];
return;
}

播放:

- (IBAction)play:sender {
[self.player play];
}

编辑:正如 awolCZ 所说,AVPlayer 无法播放部分下载的文件。但是您可以使用类似 http://test.com/test.mp3 的 URL。

关于iphone - AVPlayer 不是从 NSCachesDirectory 播放?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18843265/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com