本例項實現了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");
- }
- }
- }
程式碼是參考網上現有質料並稍加改動
原始碼下載地址:http://pan.baidu.com/s/1eP29x