标签:eve elf 苹果 懒加载 ror Once bundle device ber
- (void)viewDidLoad { [super viewDidLoad]; //设置音乐的后台播放,注意background mode中需要勾选上 AVAudioSession *session = [AVAudioSession sharedInstance]; [session setCategory:AVAudioSessionCategoryPlayback error:nil]; } //开始播放 - (IBAction)player:(id)sender { [self.player play]; } //暂停播放 - (IBAction)pause:(id)sender { [self.player pause]; } //停止播放 - (IBAction)stop:(id)sender { [self.player stop]; } //前进5s - (IBAction)qianJin5s:(id)sender { self.player.currentTime += 5; } //后退5s - (IBAction)houTui5s:(id)sender { self.player.currentTime -= 5; } //快速播放 - (IBAction)faster:(id)sender { self.player.rate = 2; } //播放一次 - (IBAction)playOnce:(id)sender { self.player.numberOfLoops = 0; } //播放3次 - (IBAction)playThirst:(id)sender { self.player.numberOfLoops = 2; } //循环播放 - (IBAction)playAllTheTime:(id)sender { self.player.numberOfLoops = -1; } //获取一些基础信息 - (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event { NSLog(@"deviceCurrentTime=%f",self.player.deviceCurrentTime); NSLog(@"currentTime=%f",self.player.currentTime); NSLog(@"duration=%f", self.player.duration); NSLog(@"settings=%@", self.player.settings); }
//听筒播放
- (IBAction)tingTongPlay:(id)sender {
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayAndRecord withOptions:0 error:nil];
}
//扬声器播放
- (IBAction)outSpeakerPlayer:(id)sender {
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayAndRecord withOptions:AVAudioSessionCategoryOptionDefaultToSpeaker error:nil];
}
//懒加载
- (AVAudioPlayer *)player {
if (!_player) {
//获取播放的路径 paomo.mp3 2018-11-27 10_36_51 1.wav
NSURL *path = [[NSBundle mainBundle] URLForResource:@"paomo.mp3" withExtension:nil];
//根据路径创建播放对象
AVAudioPlayer *player = [[AVAudioPlayer alloc] initWithContentsOfURL:path error:nil];
//如果这个属性不设置的话,那么不能设置倍速播放的功能
player.enableRate = YES;
//准备播放
[player prepareToPlay];
_player = player;
}
return _player;
}
AVAudioPlayer 这个框架,去看看苹果开发文档,还比较容易理解,此处只是贴上代码作为一个记录而已。
标签:eve elf 苹果 懒加载 ror Once bundle device ber
原文地址:https://www.cnblogs.com/lyz0925/p/11772539.html