iOS 在线音频播放

AVURLAsset *asset = [[AVURLAsset alloc] initWithURL:[NSURL URLWithString:musicUrl] options:nil];
AVPlayerItem *songitem = [[AVPlayerItem alloc] initWithAsset:asset];
self.player = [[AVPlayer alloc] initWithPlayerItem:songitem];

[songitem addObserver:self forKeyPath:@"status" options:NSKeyValueObservingOptionNew context:nil];


- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context {
    if ([keyPath isEqualToString:@"status"]) {
        AVPlayerItem *item = (AVPlayerItem *)object;
        //AVPlayerItemStatus *status = item.status;
        if (item.status == AVPlayerItemStatusReadyToPlay) {
            [self.player play];
            //对播放界面的一些操作,时间、进度等
        }
    }
}

记得在info设置NSAppTransportSecurity - NSAllowsArbitraryLoads - yes
AVAudioPlayer只可播放本地音频,不能播放网络音频

你可能感兴趣的:(iOS 在线音频播放)