Avplayeritem always with unknown status

I found a very strange problem,

when downloading an mp3 file from local storage,

avplayeritem is always in an unknown state and cannot be played if the file is placed in the document folder.

method 1:

//AVAsset *asset = [[AVURLAsset alloc] initWithURL:_URL options:nil]; //AVPlayerItem *playerItem = [[AVPlayerItem alloc] initWithAsset:asset]; AVPlayerItem *playerItem = [[AVPlayerItem alloc] initWithURL:_URL]; //line 1 while (playerItem.status!=AVPlayerItemStatusReadyToPlay && playerItem.status!=AVPlayerItemStatusFailed) { NSLog(@"avplayer: %@ status: %d", playerItem, playerItem.status); [[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate distantFuture]]; } //line 2 NSLog(@"avplayer status: %d item: %@", playerItem.status, _URL); NSArray *metadata = [playerItem.asset commonMetadata]; 

method 2:

 AVAsset *asset = [[AVURLAsset alloc] initWithURL:_URL options:nil]; AVPlayerItem *playerItem = [[AVPlayerItem alloc] initWithAsset:asset]; /* AVPlayerItem *playerItem = [[AVPlayerItem alloc] initWithURL:_URL]; //line 1 while (playerItem.status!=AVPlayerItemStatusReadyToPlay && playerItem.status!=AVPlayerItemStatusFailed) { NSLog(@"avplayer: %@ status: %d", playerItem, playerItem.status); [[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate distantFuture]]; } //line 2 */ NSLog(@"avplayer status: %d item: %@", playerItem.status, _URL); NSArray *metadata = [playerItem.asset commonMetadata]; 

conclusion:

 2015-03-19 00:42:49.243 goodnightfm[6737:353911] avplayer: <AVPlayerItem: 0x7fdc3bbd5c00, asset = <AVURLAsset: 0x7fdc3bbd8a50, URL = file:///Users/galenzhao/Library/Developer/CoreSimulator/Devices/123A77A7-DC61-4795-8D9A-E71002E261DA/data/Containers/Data/Application/4CCEED79-1D5A-4D00-BC5E-FC52BD5393F9/Documents/CMStorage/70efdf2ec9b086079795c442636b55fb>> status: 0 

but way2 code works well if the mp3 file was in the application bundle, even the status is still suck unknown

 2015-03-19 00:45:35.977 goodnightfm[6783:355678] avplayer status: 0 item: file:///Users/galenzhao/Library/Developer/CoreSimulator/Devices/123A77A7-DC61-4795-8D9A-E71002E261DA/data/Containers/Bundle/Application/F2446C29-49F3-4B92-A7B0-7EFCC1A19274/goodnightfm.app/demo5.mp3 

I’m sure that the file in the application package and the document are the same, it can be played in any other software,

using way2, the only difference between the bundle & document file was the file

if an mp3 file is located in a package, this function can return metadata

 NSArray *metadata = [playerItem.asset commonMetadata]; 

but return null using document url

+5
source share
1 answer

First you need to add an observer to the player element in order to check its state accordingly:

 [playerItem addObserver:self forKeyPath:@"status" options:NSKeyValueObservingOptionInitial | NSKeyValueObservingOptionNew context:nil]; 

Or add an observer to the AVPlayer object:

 [player addObserver:self forKeyPath:@"currentItem.status" options:NSKeyValueObservingOptionInitial | NSKeyValueObservingOptionNew context:nil]; 

Then add an observer method as follows:

 - (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context { if ([object isKindOfClass:[AVPlayerItem class]] && [keyPath isEqualToString:@"status"]) { AVPlayerItem *playerItem = (AVPlayerItem *)object; NSLog(@"avplayer: %@ status: %d", playerItem, playerItem.status); } else if ([object isKindOfClass:[AVPlayer class]] && [keyPath isEqualToString:@"currentItem.status"]) { AVPlayer *player = (AVPlayer *)object; NSLog(@"avplayer: %@ status: %d", player.currentItem, player.currentItem.status); } } 

If you still haven’t received the game in the game, make sure that the audio file really exists on the path to the document directory specified in the URL.

+1
source

Source: https://habr.com/ru/post/1215611/


All Articles