注意:如果要允许http协议需要在plist中添加App Transport Security Settings参数 不需要给值
音频播放
//先要导入AVFoundation库
#import "AVFoundation/AVFoundation.h"
//使用 prepareToPlay来解码音频
[[[NSURLSession sharedSession] dataTaskWithURL:[NSURL URLWithString:@"https://dl.stream.qqmusic.qq.com/C400000zafyN1u3l5u.m4a?vkey=CA1F8D05815FDE6E80D6F7A025F15F0408FFC1753D9C5B0E236AD394D3DFF59EBD9171D8DAADCADE78384E9C53BDA0502768F8546394886F&guid=1862941358&uin=0&fromtag=66"] completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
player = [[AVAudioPlayer alloc] initWithData:data error:nil];
[player prepareToPlay];
player.numberOfLoops = 1;
player.volume = 0.5;
}] resume];
//播放 暂停等
- (IBAction)vlaumeChange:(UISlider *)sender {
[player setVolume:sender.value/100.0 fadeDuration:1];
}
- (IBAction)pause:(UIButton *)sender {
[player pause];
}
- (IBAction)stop:(UIButton *)sender {
[player stop];
}
- (IBAction)play:(UIButton *)sender {
[player play];
}
视频播放
//先引入MediaPlayer/MediaPlayer.h库
//根据NSUrl创建MPMoviePlayerController
//然后布置player的view 然后解码资源
player = [[MPMoviePlayerController alloc] initWithContentURL:[NSURL URLWithString:str]];
player.view.frame = CGRectMake(0, 0, 200, 200);
[self.view addSubview:player.view];
[player prepareToPlay];