//緩存
// ViewController.m框架
// 音樂播放oop
//atom
// Created by DC017 on 15/12/28.url
// Copyright © 2015年 DC017. All rights reserved.spa
//.net
#import "ViewController.h"3d
//引入音樂播放器框架orm
#import <AVFoundation/AVFoundation.h>get
@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