本实例实现了AVAudioPlayer播放mp3歌曲文件,实现了播放、暂停、继续操作,音乐音量控制、播放进度显示,同时监听来电打断事件
一、控件初始化
class="dp-objc" start="1">
- - (void)viewDidLoad
- {
- [super viewDidLoad];
-
-
- 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"];
-
- NSURL *url = [NSURL fileURLWithPath:string];
-
- avAudioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:nil];
-
- avAudioPlayer.delegate = self;
-
-
-
-
-
- avAudioPlayer.numberOfLoops = -1;
-
-
- [avAudioPlayer prepareToPlay];
-
-
- progressV = [[UIProgressView alloc] initWithFrame:CGRectMake(20, 50, 200, 20)];
- [self.view addSubview:progressV];
-
-
-
- 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];
-
-
-
- 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];
-
- }
播放控制
-
- - (void)play
- {
- [avAudioPlayer play];
- }
-
- - (void)pause
- {
- [avAudioPlayer pause];
- }
-
- - (void)stop
- {
- avAudioPlayer.currentTime = 0;
- [avAudioPlayer stop];
- }
-
- - (void)playProgress
- {
-
- 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];
- }
二、调用播放器并监听来电打断事件
- - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
- {
- self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
-
-
- 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");
- }
-
- }
- }
代码是参考网上现有质料并稍加改动
源码下载地址:http://pan.baidu.com/s/1eP29x