AVFoundation.framwork框架 :用于播放音频文件
1. 导入框架 (省略)
2. 类中引入框架
#import "AVFoundation/AVFoundation.h"
3. 声明AVAudioPlayer 声音文件播放类
NSString *path = [[NSBundle mainBundle] pathForResource:@"chooseplayer"ofType:@"mp3"];
NSError *error;
if ([[NSFileManager defaultManager] fileExistsAtPath:path]) {
NSURL *musicURL = [NSURL fileURLWithPath:path];
audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:musicURL error:&error];
[audioPlayer setVolume:1.0];
[audioPlayer setNumberOfLoops:0];
[audioPlayer prepareToPlay];
}
4. 播放 声音
//Play background music
- (void)playMusic{
if (![audioPlayer isPlaying]) {
[audioPlayer play];
}
}
5. 停止播放
if ([audioPlayerisPlaying]) {
[audioPlayer stop];
[audioPlayersetCurrentTime:0];
}
---------- Zander