Skip to content

Commit 001cb8b

Browse files
committed
Use AVQueuePlayer 's advanceToNext to replace AVPlayer's replacecurrent Item .
1 parent d3b9f56 commit 001cb8b

File tree

1 file changed

+39
-18
lines changed

1 file changed

+39
-18
lines changed

src/PlayerEngine.mm

Lines changed: 39 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@ @interface PlayerEngine ()
2121
PlayState _state;
2222
dispatch_source_t _timer;
2323
}
24-
@property (atomic,strong) AVPlayer *player;
25-
24+
//@property (atomic,strong) AVPlayer *player;
25+
@property (atomic,strong) AVQueuePlayer *player;
2626
@end
2727

2828
@implementation PlayerEngine
@@ -52,8 +52,11 @@ -(instancetype)init
5252

5353
_state = playstate_stopped;
5454

55-
self.player = [[AVPlayer alloc]init];
56-
self.player.actionAtItemEnd = AVPlayerActionAtItemEndPause;
55+
// self.player = [[AVPlayer alloc]init];
56+
// self.player.actionAtItemEnd = AVPlayerActionAtItemEndPause;
57+
58+
59+
5760

5861

5962
addObserverForEvent(self, @selector(playNext), EventID_track_stopped_playnext);
@@ -69,11 +72,7 @@ -(instancetype)init
6972
addObserverForEvent(self, @selector(actionPlayRandom), EventID_to_play_random);
7073

7174

72-
/* Observe the AVPlayer "rate" property to update the scrubber control. */
73-
[self.player addObserver:self
74-
forKeyPath:@"rate"
75-
options:NSKeyValueObservingOptionInitial | NSKeyValueObservingOptionNew
76-
context:AVPlayerDemoPlaybackViewControllerRateObservationContext];
75+
7776

7877

7978
NSNotificationCenter *d =[NSNotificationCenter defaultCenter];
@@ -287,20 +286,42 @@ -(NSTimeInterval)currentTime
287286
return CMTimeGetSeconds(time);
288287
}
289288

290-
-(BOOL)playURL:(NSURL *)url pauseAfterInit:(BOOL)pfi
289+
-(BOOL)playURL:(NSURL *)url pauseAfterInit:(BOOL)pauseAfterInit
291290
{
292291
AVURLAsset *asset = [AVURLAsset assetWithURL: url];
293292
AVPlayerItem *item = [AVPlayerItem playerItemWithAsset: asset automaticallyLoadedAssetKeys:@[@"playable",@"duration"]];
294-
295-
@try {
296-
[_player replaceCurrentItemWithPlayerItem: item ];
297-
} @catch (NSException *exception) {
298-
NSLog(@"replaceCurrentItemWithPlayerItem: %@",exception);
299-
} @finally {
293+
294+
if (_player == nil) {
295+
_player = [AVQueuePlayer queuePlayerWithItems:@[item]];
296+
297+
/* Observe the AVPlayer "rate" property to update the scrubber control. */
298+
[self.player addObserver:self
299+
forKeyPath:@"rate"
300+
options:NSKeyValueObservingOptionInitial | NSKeyValueObservingOptionNew
301+
context:AVPlayerDemoPlaybackViewControllerRateObservationContext];
302+
303+
304+
if (!pauseAfterInit)
305+
[_player play];
306+
}
307+
else{
308+
309+
if ([_player canInsertItem:item afterItem:nil]) {
310+
[_player insertItem:item afterItem: nil ];
311+
if ([_player items].count == 1) {
312+
[_player play];
313+
}
314+
else{
315+
[_player advanceToNextItem];
316+
}
317+
}
318+
else{
319+
NSLog(@"can not insert to play queue");
320+
}
321+
300322
}
301323

302-
if (pfi == false)
303-
[_player play];
324+
304325

305326
ProgressInfo *info = [[ProgressInfo alloc]init];
306327
info.total = CMTimeGetSeconds( item.duration);

0 commit comments

Comments
 (0)