iOS开发之AVAudioPlayer 音频播放

http://www.cnblogs.com/VincentXue/archive/2012/09/03/2668729.html

要给工程中添加音频,首先要导入音频的框架 AVFoundation.framework

然后新建一个类继承于UIViewController, 我这里就叫FirstVC.
首先在 AppDelegate.m中初始化根视图

复制代码
 1 //
 2 //  AppDelegate.m
 3 //  YinPinShiPin
 4 //
 5 //  Created by VincentXue on 12-9-3.
 6 //  Copyright (c) 2012年 VincentXue. All rights reserved.
 7 //
 8 
 9 #import "AppDelegate.h"
10 #import "FirstVC.h"
11 @implementation AppDelegate
12 
13 - (void)dealloc
14 {
15     [_window release];
16     [super dealloc];
17 }
18 
19 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
20 {
21     self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
22     // Override point for customization after application launch.
23     
24     FirstVC *firstVC = [[FirstVC alloc] init];
25     self.window.rootViewController = firstVC;
26     [firstVC release];
27     
28     self.window.backgroundColor = [UIColor whiteColor];
29     [self.window makeKeyAndVisible];
30     return YES;
31 }
复制代码

然后在FirstVC.h中导入AVFoundation框架  

复制代码
 1 //
 2 //  FirstVC.h
 3 //  YinPinShiPin
 4 //
 5 //  Created by VincentXue on 12-9-3.
 6 //  Copyright (c) 2012年 VincentXue. All rights reserved.
 7 //
 8 
 9 #import <UIKit/UIKit.h>
10 
11 //要想使用封装好的音频类,导入框,导入类头文件,缺一不可;
12 #import <AVFoundation/AVFoundation.h>
13 
14 @interface FirstVC : UIViewController<AVAudioPlayerDelegate>
15 {
16     AVAudioPlayer *avAudioPlayer;   //播放器player
17     UIProgressView *progressV;      //播放进度
18     UISlider *volumeSlider;         //声音控制
19     NSTimer *timer;                 //监控音频播放进度
20 }
21 
22 @end
复制代码

然后在FirstVC.m里的viewDidLoad方法里填写代码   你需要导入一个音频才可以播放 像添加图片一样,直接拖到工程里就可以了

复制代码
 1 - (void)viewDidLoad
 2 {
 3     [super viewDidLoad];
 4     // Do any additional setup after loading the view.
 5     //初始化三个button
 6     UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
 7     [button setFrame:CGRectMake(100, 100, 60, 40)];
 8     [button setTitle:@"Play" forState:UIControlStateNormal];
 9     [button addTarget:self action:@selector(play) forControlEvents:UIControlEventTouchUpInside];
10     [self.view addSubview:button];
11     
12     UIButton *button1 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
13     [button1 setFrame:CGRectMake(100, 150, 60, 40)];
14     [button1 setTitle:@"pause" forState:UIControlStateNormal];
15     [button1 addTarget:self action:@selector(pause) forControlEvents:UIControlEventTouchUpInside];
16     [self.view addSubview:button1];
17     
18     UIButton *button2 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
19     [button2 setFrame:CGRectMake(100, 200, 60, 40)];
20     [button2 setTitle:@"stop" forState:UIControlStateNormal];
21     [button2 addTarget:self action:@selector(stop) forControlEvents:UIControlEventTouchUpInside];
22     [self.view addSubview:button2];
23     
24     //从budle路径下读取音频文件  轻音乐 - 萨克斯回家 这个文件名是你的歌曲名字,mp3是你的音频格式
25     NSString *string = [[NSBundle mainBundle] pathForResource:@"轻音乐 - 萨克斯回家" ofType:@"mp3"];
26     //把音频文件转换成url格式
27     NSURL *url = [NSURL fileURLWithPath:string];
28     //初始化音频类 并且添加播放文件
29     avAudioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:nil];
30     //设置代理
31     avAudioPlayer.delegate = self;
32     
33     //设置初始音量大小
34    // avAudioPlayer.volume = 1;
35     
36     //设置音乐播放次数  -1为一直循环
37     avAudioPlayer.numberOfLoops = -1;
38     
39     //预播放
40     [avAudioPlayer prepareToPlay];
41     
42     //初始化一个播放进度条
43     progressV = [[UIProgressView alloc] initWithFrame:CGRectMake(20, 50, 200, 20)];
44     [self.view addSubview:progressV];
45     [progressV release];
46     
47     //用NSTimer来监控音频播放进度
48     timer = [NSTimer scheduledTimerWithTimeInterval:0.1 target:self
49                                                      selector:@selector(playProgress)
50                                                      userInfo:nil repeats:YES];
51     //初始化音量控制
52     volumeSlider = [[UISlider alloc] initWithFrame:CGRectMake(20, 70, 200, 20)];
53     [volumeSlider addTarget:self action:@selector(volumeChange)
54                        forControlEvents:UIControlEventValueChanged];
55     //设置最小音量
56     volumeSlider.minimumValue = 0.0f;
57     //设置最大音量
58     volumeSlider.maximumValue = 10.0f;
59     //初始化音量为多少
60     volumeSlider.value = 5.0f;
61     
62     [self.view addSubview:volumeSlider];
63     [volumeSlider release];
64 
65     //声音开关控件(静音)
66     UISwitch *swith = [[UISwitch alloc] initWithFrame:CGRectMake(100, 20, 60, 40)];
67     [swith addTarget:self action:@selector(onOrOff:) forControlEvents:UIControlEventValueChanged];
68     //默认状态为打开
69     swith.on = YES;
70     [self.view addSubview:swith];
71     [swith  release];
72 }
复制代码

相应的自定义方法代码如下

复制代码
 1     //播放
 2 - (void)play
 3 {  
 4     [avAudioPlayer play];
 5 }
 6     //暂停
 7 - (void)pause
 8 {
 9     [avAudioPlayer pause];
10 }
11     //停止
12 - (void)stop
13 {
14     avAudioPlayer.currentTime = 0;  //当前播放时间设置为0
15     [avAudioPlayer stop];
16 }
17 //播放进度条
18 - (void)playProgress
19 {
20     //通过音频播放时长的百分比,给progressview进行赋值;
21     progressV.progress = avAudioPlayer.currentTime/avAudioPlayer.duration;
22 }
23 //声音开关(是否静音)
24 - (void)onOrOff:(UISwitch *)sender
25 {
26     avAudioPlayer.volume = sender.on;
27 }
28 
29 //播放音量控制
30 - (void)volumeChange
31 {
32    avAudioPlayer.volume = volumeSlider.value;
33 }
34 
35 //播放完成时调用的方法  (代理里的方法),需要设置代理才可以调用
36 - (void)audioPlayerDidFinishPlaying:(AVAudioPlayer *)player successfully:(BOOL)flag
37 {
38     [timer invalidate]; //NSTimer暂停   invalidate  使...无效;
39 }
复制代码

最后别忘了释放内存

复制代码
1 - (void)dealloc
2 {
3     [avAudioPlayer release];
4     [progressV release];
5     [volumeSlider release];
6     [timer release];
7     [super dealloc];
8 }
复制代码

当然,你也可以再定义一个UISlider来控制播放进度.

最后运行起来的就是这个样子


你可能感兴趣的:(iOS开发之AVAudioPlayer 音频播放)