IOS 基于AV Foundation框架开发简单音乐播放器
2014-08-18 17:06
501 查看
虽然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 头文件源代码:
MusicPlayerViewController.m 实现文件源代码:
@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];
}
}
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
相关文章推荐
- iOS 6编程-基于AV Foundation框架开发简单音乐播放器
- IOS 基于AV Foundation框架开发简单音乐播放器
- iOS开发网络篇—简单介绍ASI框架的使用
- 【iOS开发-114】ABAddressBook通讯录:增删改查的简单介绍,CF框架于F框架间的转换
- iOS 使用XMPP框架开发IM聊天模块,实现简单的文字聊天
- 基于Blue框架开发的简单网游
- iOS开发进阶 - 日志输出框架CocoaLumberjack与XcodeColors插件的简单使用(swift版)
- iOS开发网络篇—简单介绍ASI框架的使用
- iOS开发之优秀开源框架IQKeyboardManager,零行代码解决键盘遮挡问题,如此简单解决键盘遮挡问题,IQKeyboardManager源码分解
- 基于 WKWebView 的混合开发框架(网站生成ios应用)
- 简单搭建iOS开发项目框架
- IOS基于XMPP协议开发--XMPPFramewok框架(二):服务器连接
- iOS开发——多线程篇——快速生成沙盒目录的路径,多图片下载的原理、SDWebImage框架的简单介绍
- iOS开发网络—09简单介绍ASI框架的使用
- iOS开发中大部分App的网络数据交换是基于HTTP协议的。本文将简单介绍在Swift中使用HTTP进行网络请求的几种方法。
- 简单地基于cxf框架的webservice完整开发示例
- IOS基于XMPP协议开发--XMPPFramewok框架(三):用户注册
- iOS开发——UI进阶篇(十三)UITabBarController简单使用,qq主流框架
- iOS 对基于AFNetworking框架网络请求的简单封装
- iOS开发进阶 - 日志输出框架CocoaLumberjack与XcodeColors插件的简单使用(swift版)