How to parse m3u file with AVPlayer

2019-06-16 07:31发布

问题:

I want to get track info from m3u file headers not from id3Tags. In AVPlayer's timedMetaData properties it is only id3Tag parsing is provided.

How can I get title, artist, url etc from following m3u file with AVPlayer

#EXTM3U
#EXT-X-ALLOW-CACHE:NO
#EXT-X-TARGETDURATION:11
#EXT-X-MEDIA-SEQUENCE:0
#EXTINF:10,title="Dark Horse",artist="Katy Perry / Juicy J",url="song_spot=\"M\" MediaBaseId=\"1971116\" itunesTrackId=\"0\" amgTrackId=\"-1\" amgArtistId=\"0\" TAID=\"35141\" TPID=\"23894643\" cartcutId=\"0729388001\" amgArtworkURL=\"http://assets.iheart.com/images/1080/MI0003667474\" length=\"00:03:32\" unsID=\"-1\""
http://chunks.ihrhls.com/1469/GqsS8fnz2S6-301028-9984.aac
#EXTINF:10,title="Dark Horse",artist="Katy Perry / Juicy J",url="song_spot=\"M\" MediaBaseId=\"1971116\" itunesTrackId=\"0\" amgTrackId=\"-1\" amgArtistId=\"0\" TAID=\"35141\" TPID=\"23894643\" cartcutId=\"0729388001\" amgArtworkURL=\"http://assets.iheart.com/images/1080/MI0003667474\" length=\"00:03:32\" unsID=\"-1\""
http://chunks.ihrhls.com/1469/spXO68wER45-301029-9984.aac
#EXTINF:10,title="Dark Horse",artist="Katy Perry / Juicy J",url="song_spot=\"M\" MediaBaseId=\"1971116\" itunesTrackId=\"0\" amgTrackId=\"-1\" amgArtistId=\"0\" TAID=\"35141\" TPID=\"23894643\" cartcutId=\"0729388001\" amgArtworkURL=\"http://assets.iheart.com/images/1080/MI0003667474\" length=\"00:03:32\" unsID=\"-1\""
http://chunks.ihrhls.com/1469/RSEeNpIOEHk-301029-10031.aac

回答1:

You can get the track information using AVURLAsset

AVURLAsset *asset = [[AVURLAsset alloc] initWithURL:[NSURL URLWithString:url] options:nil];
NSArray *keys = @[@"playable", @"tracks", @"duration"];

[asset loadValuesAsynchronouslyForKeys:keys completionHandler:^() {
     for (NSString *thisKey in keys) {
         NSError *error = nil;
         AVKeyValueStatus keyStatus = [asset statusOfValueForKey:thisKey error:&error];
         if (keyStatus == AVKeyValueStatusFailed) {
             //handle error
         }
     }
 }];

Also take a look at this answer to get title, album, etc.