使用AVPlayer实现在线音频播放注意问题

//(oneplayer为单例)

+(OnePlayer*)onePlayer

{

    static OnePlayer * player = nil;

    static dispatch_once_t once;

    dispatch_once(&once, ^{

        player = [[OnePlayer alloc]init];

    });

    return player;

}

 

//初始化

//url初始化

-(OnePlayer*)initWithMyUrl:(NSURL*)url

{

    

    [self isCurrentPlayingUrl:url];

    if (_isPlaying) {

        [self pause];

    }

    [self replaceCurrentItemWithPlayerItem:[self creatPlayerItemWithMyUrl:url]];

 

    //提示网络

//    [[ShareManger defoutManger] judgeNetStatusAndAlert];

    self.isPlyed = NO;

    self.failPlay = NO;

    

    [self start];

    

    return self;

}

 

//初始化item

-(void)initWithaPlayerItem:(AVPlayerItem*)item

{

    if (self.currentItem.status != AVPlayerStatusFailed) {

        if (self.currentItem) {

            [self removeObserverFromPlayerItem:self.currentItem];

            [self removeNotification];

        }

    }

    

    [self replaceCurrentItemWithPlayerItem:item];

    [self addNotification];

}

 

 

//创建item

-(AVPlayerItem*)creatPlayerItemWithMyUrl:(NSURL*)url

{

    _playingUrl = url;

    

    if (self.currentItem.status != AVPlayerStatusFailed) {

        if (self.currentItem) {

            [self removeObserverFromPlayerItem:self.currentItem];

            [self removeNotification];

        }

    }

    

    AVPlayerItem * playerItem = [[AVPlayerItem alloc]initWithURL:url];

    

    [self addObserberToPlayerItem:playerItem];

    [self addNotification];

    

    return playerItem;

}

 

//改变item

-(void)changeToItemWithMyUrl:(NSURL*)url

{

    _playingUrl = url;

    

    if (self.currentItem.status != AVPlayerStatusFailed) {

        if (self.currentItem) {

            [self removeObserverFromPlayerItem:self.currentItem];

            [self removeNotification];

        }

    }

 

 

//重写getter方法

-(AVPlayerLayer*)playerLayer

{

    if (!_playerLayer) {

        _playerLayer = [AVPlayerLayer playerLayerWithPlayer:self];

    }

    return _playerLayer;

}

 

//第一次开始播放

-(void)start

{

    [super play];

}

 

//重写播放方法

-(void)play

{

    if (!_isPlaying) {

        [super play];

        _isPlaying = YES;

    }

}

 

//重写暂停方法

-(void)pause

{

    if (_isPlaying) {

        [super pause];

        _isPlaying = NO;

    }

}

 

 

 

//判断新的URL是不是现在播放的URL

-(BOOL)isCurrentPlayingUrl:(NSURL*)url

{

    NSString * urlStr = [url absoluteString];

    NSString * isPlayingStr = [_playingUrl absoluteString];

    if ([urlStr isEqualToString:isPlayingStr]) {

        return YES;

    }else{

        return NO;

    }

}

 

    

    AVPlayerItem * playerItem = [[AVPlayerItem alloc]initWithURL:url];

    

    [self addObserberToPlayerItem:playerItem];

    

    [self replaceCurrentItemWithPlayerItem:playerItem];

    

    [self addNotification];

}

 

转载于:https://www.cnblogs.com/flyios/p/6590300.html

你可能感兴趣的:(使用AVPlayer实现在线音频播放注意问题)