IOS 基于AV Foundation框架开发简单音乐播放器

虽然Media Player 框架也可实现音乐播放功能,具体参考如下文章:

iOS 6编程(19)-使用MPMoviePlayerController类实现视频播放器

但是Apple推荐使用 AV Foundation框架来实现音频播放功能。另外,AV Foundation框架还提供了录音功能,可以在App中之间录制声音文件。

  • AVAudioRecorder — 录音功能;
  • AVAudioPlayer — 播放音频功能;

我们基于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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值