在线播放音乐AVPlayer的使用

参考资料:
https://www.cnblogs.com/kenshincui/p/4186022.html
https://www.jianshu.com/p/de418c21d33c
利用AVPlayer可以播放后端提供的.mp3和.mp4文件,例:
http://market.linlizhu.com/music2.mp3,
http://bos.nj.bpc.baidu.com/tieba-smallvideo/11772_3c435014fb2dd9a5fd56a57cc369f6a0.mp4

avplayer支持的格式如下:
支持视频格式: WMV,AVI,MKV,RMVB,RM,XVID,MP4,3GP,MPG …
支持音频格式: MP3,WMA,RM,ACC,OGG,APE,FLAC,FLV…
支持外部字幕: smi,srt,ass,sub,txt…

1,导入Foundation/Foundation.h 、AVFoundation/AVFoundation.h
2,定义应用到的全局变量
@property (nonatomic,strong)AVPlayer * player;
@property (strong, nonatomic) AVPlayer *avPlayer;
@property (strong, nonatomic) AVPlayerItem *avPlayerItem;
3,贴代码
-(void)initPlayer{

NSURL *url = [NSURL URLWithString:@"http://market.linlizhu.com/music2.mp3"];
self.avPlayerItem = [AVPlayerItem playerItemWithURL:url];
self.avPlayer = [AVPlayer playerWithPlayerItem:self.avPlayerItem];
[self.avPlayer play];

}

注意:要在plist文件里面添加
key NSAppTransportSecurity key
dict
key NSAllowsArbitraryLoads key
true
dict

这里写图片描述
完成

你可能感兴趣的:(音频播放器)