本实例实现了AVAudioPlayer播放mp3歌曲文件,实现了播放、暂停、继续操作,音乐音量控制、播放进度显示,同时监听来电打断事件
一、控件初始化
- - (void)viewDidLoad
- {
- [super viewDidLoad];
- // Do any additional setup after loading the view.
- //初始化三个button
- UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
- [button setFrame:CGRectMake(100, 100, 60, 40)];
- [button setTitle:@"Play" forState:UIControlStateNormal];
- [button addTarget:self action:@selector(play) forControlEvents:UIControlEventTouchUpInside];
- [self.view addSubview:button];
- UIButton *button1 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
- [button1 setFrame:CGRectMake(100, 150, 60, 40)];
- [button1 setTitle:@"pause" forState:UIControlStateNormal];
- [button1 addTarget:self action:@selector(pause) forControlEvents:UIControlEventTouchUpInside];
- [self.view addSubview:button1];
- UIButton *button2 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
- [button2 setFrame:CGRectMake(100, 200, 60, 40)];
- [button2 setTitle:@"stop" forState:UIControlStateNormal];
- [button2 addTarget:self action:@selector(stop) forControlEvents:UIControlEventTouchUpInside];
- [self.view addSubview:button2];
- NSString *string = [[NSBundle mainBundle] pathForResource:@"陶钰玉 - 深夜地下铁" ofType:@"mp3"];
- //把音频文件转换成url格式
- NSURL *url = [NSURL fileURLWithPath:string];
- //初始化音频类 并且添加播放文件
- avAudioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:nil];
- //设置代理
- avAudioPlayer.delegate = self;
- //设置初始音量大小
- // avAudioPlayer.volume = 1;
- //设置音乐播放次数 -1为一直循环
- avAudioPlayer.numberOfLoops = -1;
- //预播放
- [avAudioPlayer prepareToPlay];
- //初始化一个播放进度条
- progressV = [[UIProgressView alloc] initWithFrame:CGRectMake(20, 50, 200, 20)];
- [self.view addSubview:progressV];
- //[progressV release];
- //用NSTimer来监控音频播放进度
- timer = [NSTimer scheduledTimerWithTimeInterval:0.1
- target:self
- selector:@selector(playProgress) userInfo:nil
- repeats:YES];
- //初始化音量控制
- volumeSlider = [[UISlider alloc] initWithFrame:CGRectMake(20, 70, 200, 20)];
- [volumeSlider addTarget:self action:@selector(volumeChange)
- forControlEvents:UIControlEventValueChanged];
- //设置最小音量
- volumeSlider.minimumValue = 0.0f;
- //设置最大音量
- volumeSlider.maximumValue = 10.0f;
- //初始化音量为多少
- volumeSlider.value = 5.0f;
- [self.view addSubview:volumeSlider];
- //[volumeSlider release];
- //声音开关控件(静音)
- UISwitch *swith = [[UISwitch alloc] initWithFrame:CGRectMake(100, 20, 60, 40)];
- [swith addTarget:self action:@selector(onOrOff:) forControlEvents:UIControlEventValueChanged];
- //默认状态为打开
- swith.on = YES;
- [self.view addSubview:swith];
- //[swith release];
- }
播放控制
- //播放
- - (void)play
- {
- [avAudioPlayer play];
- }
- //暂停
- - (void)pause
- {
- [avAudioPlayer pause];
- }
- //停止
- - (void)stop
- {
- avAudioPlayer.currentTime = 0; //当前播放时间设置为0
- [avAudioPlayer stop];
- }
- //播放进度条
- - (void)playProgress
- {
- //通过音频播放时长的百分比,给progressview进行赋值;
- progressV.progress = avAudioPlayer.currentTime/avAudioPlayer.duration;
- }
- //声音开关(是否静音)
- - (void)onOrOff:(UISwitch *)sender
- {
- avAudioPlayer.volume = sender.on;
- }
- //播放音量控制
- - (void)volumeChange
- {
- avAudioPlayer.volume = volumeSlider.value;
- }
- //播放完成时调用的方法 (代理里的方法),需要设置代理才可以调用
- - (void)audioPlayerDidFinishPlaying:(AVAudioPlayer *)player successfully:(BOOL)flag
- {
- [timer invalidate]; //NSTimer暂停 invalidate 使...无效;
- }
二、调用播放器并监听来电打断事件
- - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
- {
- self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
- // Override point for customization after application launch.
- AVAudioSession *session = [AVAudioSession sharedInstance];
- [session setActive:YES error:nil];
- [session setCategory:AVAudioSessionCategoryPlayback error:nil];
- firstVC = [[FirstVC alloc] init];
- self.window.rootViewController = firstVC;
- [firstVC viewDidLoad];
- [firstVC play];
- self.window.backgroundColor = [UIColor whiteColor];
- [self.window makeKeyAndVisible];
- AudioSessionInitialize(NULL, NULL, interruptionListenner, (__bridge void*)self);
- return YES;
- }
- void interruptionListenner(void* inClientData, UInt32 inInterruptionState)
- {
- ZTAppDelegate* pTHIS = (__bridge ZTAppDelegate*)inClientData;
- if (pTHIS) {
- NSLog(@"interruptionListenner %lu", inInterruptionState);
- if (kAudioSessionBeginInterruption == inInterruptionState) {
- NSLog(@"Begin interruption");
- [pTHIS.self.firstVC pause];
- }
- else
- {
- NSLog(@"Begin end interruption");
- [pTHIS.self.firstVC play];
- NSLog(@"End end interruption");
- }
- }
- }
代码是参考网上现有质料并稍加改动