//
// ViewController.m
// 音乐播放
//
// Created by DC017 on 15/12/28.
// Copyright © 2015年 DC017. All rights reserved.
//
#import "ViewController.h"
//引入音乐播放器框架
#import <AVFoundation/AVFoundation.h>
@interface ViewController ()<AVAudioPlayerDelegate>
@property(nonatomic,strong)AVAudioPlayer *audioPlayer;
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
[self.audioPlayer play];
_audioPlayer.delegate=self;
}
//get方法
-(AVAudioPlayer *)audioPlayer{
NSLog(@"get 方法被调用");
if (!_audioPlayer) {
NSLog(@"播放器准备启动,开始实例化");
//1获取音乐文件路径 注意:这里获取的时相对路经
NSString * strurl=[[NSBundle mainBundle] pathForResource:@"逍遥叹" ofType:@"mp3"];
NSURL * url=[NSURL fileURLWithPath:strurl];
//初始化音乐播放器
NSError * error;
_audioPlayer=[[AVAudioPlayer alloc]initWithContentsOfURL:url error:&error];
//设置播放属性
_audioPlayer.numberOfLoops=0;//0表示为不循环,负数表示为无线循环
//获取总秒数duration
NSLog(@"总秒数%f",_audioPlayer.duration);
[_audioPlayer prepareToPlay];//加载音频到缓存
}
return _audioPlayer;
}
-(void)audioPlayerDidFinishPlaying:(AVAudioPlayer *)player successfully:(BOOL)flag{
NSLog(@"音乐播放完成");
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end