1

I am trying to dynamically add .aac chunks (usually 10 seconds) during the playback to AVQueuePlayer to mimic the AVPlayer hls playback.

However it plays songs one after another as I wished, there is about 200ms to 400 ms gap between songs.

following question seems to have the answer it to this question but it doesn't work for me.

AVQueuePlayer playback without gap and freeze

I have a singleton player and a singleton avplayeritem to keep track of the last item. then with following code I can successfully play the songs with gap.

 NSURL  *url = [NSURL URLWithString:[urlString stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]]];
    //NSData *urlData = [NSData dataWithContentsOfURL:url];

    GlobalPlayer *singleton=[GlobalPlayer sharedManager];
    //AVPlayerItem *item=[AVPlayerItem playerItemWithURL:url];

    AVURLAsset *asset = [[AVURLAsset alloc] initWithURL:url options:nil];
    NSArray *keys = [NSArray arrayWithObject:@"playable"];

    if (singleton.lastItem==nil) {
        //initilize player
        //NSArray *playerArray=[[NSArray alloc] initWithObjects:item, nil];
        //singleton.globPlayer=[[AVQueuePlayer alloc] initWithItems:playerArray];
        //[singleton.globPlayer play];

        singleton.globPlayer=[[AVQueuePlayer alloc] init];
        singleton.lastItem=[AVPlayerItem alloc];

        __block AVPlayerItem *playerItem;
        [asset loadValuesAsynchronouslyForKeys:keys completionHandler:^()
         {
             dispatch_async(dispatch_get_main_queue(), ^
                            {
                                playerItem = [[AVPlayerItem alloc] initWithAsset:asset];
                                [singleton.globPlayer insertItem:playerItem afterItem:nil];
                                singleton.lastItem=playerItem;
                                [singleton.globPlayer play];
                            });

         }];

    }
    else
    {
        //[singleton.globPlayer insertItem:item afterItem:singleton.lastItem];
        __block AVPlayerItem *playerItem;
        [asset loadValuesAsynchronouslyForKeys:keys completionHandler:^()
        {
            dispatch_async(dispatch_get_main_queue(), ^
                           {
                               playerItem = [[AVPlayerItem alloc] initWithAsset:asset];
                               [singleton.globPlayer insertItem:playerItem afterItem:singleton.lastItem];
                                singleton.lastItem=playerItem;
                           });

        }];

    }

Do I have to decode the chunks then feed the PCM to AVQueuePlayer? If that is the only solution how do I feed NSData that holds pcm to the AVPlayerItem ?

Community
  • 1
  • 1
Mord Fustang
  • 1,523
  • 4
  • 40
  • 71
  • @matt I wouldn't be asking the question if it was working, would I? There must be something; because AVPlayer can play this hls stream url `http://playlists.ihrhls.com/c5/1469/playlist.m3u8` without any problem there are no gaps between .aac chunks. So I manually parse this hls stream and add the .acc chunks to `AVQueuePlayer` but then there is a gap in between chunks. My first guess was it was because the way it decodes the chunks or the way chunks are encoded problem but I am not sure. – Mord Fustang Apr 08 '15 at 14:38
  • Did you try to feed PCM files directly to player? If you think decoding is the problem decode the files first then send them to the queue , of course if you don't mind the hassle – SpaceDust__ Apr 09 '15 at 18:56

0 Answers0