iOS开发交流群:301058503
- 新建一个继承NSObject的类PlayModel
- 导入音视频播放需要的头文件
#import <Foundation/Foundation.h>
#import <MediaPlayer/MediaPlayer.h>
#import <AVFoundation/AVFoundation.h>
#import <AVKit/AVKit.h>
3.创建播放单例
+(PlayModel *)shareInstance{
static PlayModel *player = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
player = [[PlayModel alloc] init];
});
return player;
}
4.音频播放初始化
//音频
-(void)initAudioPlayerWithUrl:(NSURL *)url{
playerItem = [AVPlayerItem playerItemWithURL:url];
audioPlayer =[AVPlayer playerWithPlayerItem:playerItem];
[self start];
currentTime = 0;
// //支持后台播放 还要在background modes 勾选Audio,AirPlay
// AVAudioSession *session = [AVAudioSession sharedInstance];
// [session setActive:YES error:nil];
// [session setCategory:AVAudioSessi