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
?