虽然Media Player 框架也可实现音乐播放功能,具体参考如下文章:
iOS 6编程-使用MPMoviePlayerController类实现视频播放器
但是Apple推荐使用 AV Foundation框架来实现音频播放功能。另外,AV Foundation框架还提供了录音功能,可以在App中之间录制声音文件。
我们基于AV Foundation框架,实现了一个简单的音乐播放器MusicPlayer,App运行界面如下所示:
示例App的功能是,播放在项目本地的多个 MP3 音频文件,示例App中添加了2首侃侃的歌曲:嘀嗒 和 网络情缘。EntLib.com Team Leader 最爱的曲目之一。
简单的旋律,沙哑的声音。
低沉、婉转、略带忧伤和沧桑。
淡淡唱来,象水一样穿越你的心灵。
Xcode项目采用Single View Application模板,项目名称MusicPlayer,类前缀MusicPlayer。
开发环境:Xcode 4.5 + iOS 6 iPhone 模拟器
使用AV Foundation框架需要注意的几个问题:
(1) 项目中引入AVFoundation框架;
(2) 在项目相应头文件中,需要import 2个接口文件,分别为:
#import <AVFoundation/AVFoundation.h>
#import <CoreAudio/CoreAudioTypes.h>
(3) 在项目相应头文件需要遵守协议 AVAudioPlayerDelegate,并将播放器的delegate属性设置为self;
(4) 实现协议方法audioPlayerDidFinishPlaying:successfully:负责处理音频播放结束之后的处理,如继续播放下一首歌曲等等;
本示例程序音乐播放器MusicPlayer的全部源代码如下所示。
MusicPlayerViewController.h 头文件源代码:
#import <UIKit/UIKit.h>
#import <AVFoundation/AVFoundation.h>
#import <CoreAudio/CoreAudioTypes.h>
@interface MusicPlayerViewController : UIViewController <AVAudioPlayerDelegate>
@property (strong, nonatomic) AVAudioPlayer *audioPlayer;
@property (strong, nonatomic) IBOutlet UIButton *musicButton;
@property (strong, nonatomic) IBOutlet UILabel *songLabel;
- (IBAction)playMusic:(id)sender;
@end
MusicPlayerViewController.m 实现文件源代码:
//
// MusicPlayerViewController.m
// MusicPlayer
//
// Created by EntLib.com on 12-10-12.
// Copyright (c) 2012年 EntLib.com. All rights reserved.
//
#import "MusicPlayerViewController.h"
@interface MusicPlayerViewController ()
@end
@implementation MusicPlayerViewController
NSArray *audioArray;
NSString *audioFile;
int audioIndex;
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
audioArray = [NSArray arrayWithObjects:@"嘀嗒",@"网络情缘", nil];
audioIndex = 0;
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (void)playAudio:(int)audioIndex{
audioFile = [audioArray objectAtIndex:audioIndex];
self.songLabel.text = [[NSString alloc]initWithFormat:@"正在播放:%@", audioFile ];
NSURL *audioFileURL = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:audioFile ofType:@"mp3"]];
self.audioPlayer=[[AVAudioPlayer alloc] initWithContentsOfURL:audioFileURL error:nil];
self.audioPlayer.delegate = self;
[self.audioPlayer play];
}
- (IBAction)playMusic:(id)sender {
if ([self.musicButton.titleLabel.text isEqualToString:@"播放音乐"]) {
[self playAudio:0];
[self.musicButton setTitle:@"暂停" forState:UIControlStateNormal];
}else if([self.musicButton.titleLabel.text isEqualToString:@"播放"]){
[self.audioPlayer play];
[self.musicButton setTitle:@"暂停" forState:UIControlStateNormal];
}else {
[self.audioPlayer pause];
[self.musicButton setTitle:@"播放" forState:UIControlStateNormal];
}
}
- (void)audioPlayerDidFinishPlaying:(AVAudioPlayer *)player successfully:(BOOL)flag{
// play the next audio - 播放下一首歌曲
if (audioIndex < audioArray.count) {
audioIndex++;
}
if(audioIndex == audioArray.count){
audioIndex = 0;
}
[self playAudio:audioIndex];
}
@end