#import "ViewController.h" #import <AVFoundation/AVFoundation.h> @interface ViewController () @property (nonatomic, strong) AVAudioPlayer *player; @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; // 音乐: 长的声音 歌曲 戏曲 // 1.1获取资源地址 NSURL *url = [[NSBundle mainBundle] URLForResource:@"童话.mp3" withExtension:nil]; NSError *error = nil; // 1.创建音乐播放的对象 AVAudioPlayer *player = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:&error]; if (error) { NSLog(@"创建音乐播放的对象出错:%@",error); return; } self.player = player; // 2.准备播放 [player prepareToPlay]; } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } - (IBAction)play:(id)sender { // 3.播放 [self.player play]; } - (IBAction)pause:(id)sender { // 暂停 [self.player pause]; } - (IBAction)stop:(id)sender { // 停止 [self.player stop]; }