推荐代码:
复制代码#import "YYViewController.h"
#import <AVFoundation/AVFoundation.h>
@interface YYViewController ()
@property(nonatomic,strong)AVAudioPlayer *player;
- (IBAction)play;
- (IBAction)pause;
- (IBAction)stop;
@end
复制代码
@implementation YYViewController
#pragma mark-懒加载
-(AVAudioPlayer *)player
{
if (_player==Nil) {
//1.音频文件的url路径
NSURL *url=[[NSBundle mainBundle]URLForResource:@"235319.mp3" withExtension:Nil];
//2.创建播放器(注意:一个AVAudioPlayer只能播放一个url)
self.player=[[AVAudioPlayer alloc]initWithContentsOfURL:url error:Nil];
//3.缓冲
[self.player prepareToPlay];
}
return _player;
}
- (void)viewDidLoad
{
[super viewDidLoad];
}
- (IBAction)play {
//开始播放/继续播放
[self.player play];
}
- (IBAction)pause {
//暂停
[self.player pause];
}
- (IBAction)stop {
//停止
//注意:如果点击了stop,那么一定要让播放器重新创建,否则会出现一些莫名其面的问题
[self.player stop];
self.player=Nil;
}
@end
四、播放多个文件
点击,url,按住common建查看。











