線上播放音樂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
完成