AVPlayer no se reproduce desde NSCachesDirectory?

Hola, estoy tratando de reproducir files de video de NSCachesDirectory. El reproductor no está cargando los files de NSCachesDirectory en la capa videoView.

¿Es posible reproducir video desde cacheDirectory Memory? Por favor, sugiérame …

He intentado esto …

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

Enfoque KVO Agregar observador:

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

Valor de observación

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

Jugando:

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

EDICIÓN: como awolCZ dijo que AVPlayer no puede reproducir files parcialmente descargados. Pero podría usar algo como http://test.com/test.mp3 para URL.

No estoy seguro de que [estado de avPlayer] == AVPlayerStatusReadyToPlay devuelva SÍ en este caso. AVPlayer demora un poco en cargar el elemento. Deberías poder jugar de inmediato, incluso si es una mejor manera de rastrear AVPlayer.status usando KVO.

Lukas

EDITAR: Pruébelo de esta manera (ganancia rápida):

 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]; avPlayerLayer.frame =CGRectMake(0, 0, 320, 450); [[self.videoView layer] addSublayer:avPlayerLayer]; [avPlayer play];