您的位置:首页 > 移动开发 > IOS开发

ios20-录制音频(播放,停止,开始录制)

2013-06-10 19:50 155 查看
1,

//
// ios20_audioRecordAppDelegate.h
// ios20-audioRecord
//
// Created by on 13-6-10.
// Copyright 2013年 __MyCompanyName__. All rights reserved.
//

#import
<UIKit/UIKit.h>

@interface ios20_audioRecordAppDelegate :
UIResponder <UIApplicationDelegate>

@property (strong,
nonatomic) UIWindow *window;

@end

//
// ios20_audioRecordAppDelegate.m
// ios20-audioRecord
//
// Created by on 13-6-10.
// Copyright 2013年 __MyCompanyName__. All rights reserved.
//

#import
"ios20_audioRecordAppDelegate.h"
#import
"audioRecord.h"

@implementation ios20_audioRecordAppDelegate

@synthesize window =
_window;

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary
*)launchOptions
{

self.window = [[UIWindow
alloc] initWithFrame:[[UIScreen
mainScreen] bounds]];

// Override point for customization after application launch.

audioRecord *audio=[[audioRecord
alloc]init];

self.window.rootViewController=audio;

self.window.backgroundColor = [UIColor
whiteColor];
[self.window
makeKeyAndVisible];

return YES;
}

- (void)applicationWillResignActive:(UIApplication *)application
{

/*
Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message)
or when the user quits the application and it begins the transition to the background state.
Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
*/
}

- (void)applicationDidEnterBackground:(UIApplication *)application
{

/*
Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state
in case it is terminated later.
If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
*/
}

- (void)applicationWillEnterForeground:(UIApplication *)application
{

/*
Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
*/
}

- (void)applicationDidBecomeActive:(UIApplication *)application
{

/*
Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
*/
}

- (void)applicationWillTerminate:(UIApplication *)application
{

/*
Called when the application is about to terminate.
Save data if appropriate.
See also applicationDidEnterBackground:.
*/
}

@end

2.

//
// audioRecord.h
// ios20-audioRecord
//
// Created by on 13-6-10.
// Copyright 2013年 __MyCompanyName__. All rights reserved.
//

#import
<UIKit/UIKit.h>
#import
<AVFoundation/AVFoundation.h>

@interface audioRecord :
UIViewController
{

UILabel *label;

//定义专门进行录制的类AVAudioRecoder

AVAudioRecorder *recoder;

//播放

AVAudioPlayer *player;

}
@property(nonatomic,retain)
AVAudioRecorder *recoder;
@property(nonatomic,retain)
AVAudioPlayer *player;
@end

//
// audioRecord.m
// ios20-audioRecord
//
// Created by on 13-6-10.
// Copyright 2013年 __MyCompanyName__. All rights reserved.
//

#import
"audioRecord.h"

@implementation audioRecord
@synthesize recoder,player;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{

self = [super
initWithNibName:nibNameOrNil bundle:nibBundleOrNil];

if (self) {

// Custom initialization
}

return self;
}

- (void)didReceiveMemoryWarning
{

// Releases the view if it doesn't have a superview.
[super
didReceiveMemoryWarning];

// Release any cached data, images, etc that aren't in use.
}
-(void)loadView{

UIView *view=[[UIView
alloc]initWithFrame:[UIScreen
mainScreen].applicationFrame];
view.backgroundColor=[UIColor
orangeColor];

self.view=view;

label=[[UILabel
alloc]initWithFrame:CGRectMake(90,
40,
160, 40)];

label.text=@"等待录制声音";

[self.view
addSubview:label];

UIButton *button=[UIButton
buttonWithType:UIButtonTypeRoundedRect];
button.frame=CGRectMake(90,
100, 100,
40);
[button
setTitle:@"开始录制" forState:UIControlStateNormal];
[button
addTarget:self
action:@selector(startPlayer)
forControlEvents:UIControlEventTouchUpInside];
[self.view
addSubview:button];

UIButton *button2=[UIButton
buttonWithType:UIButtonTypeRoundedRect];
button2.frame=CGRectMake(90,
160, 100,
40);
[button2
setTitle:@"停止录制" forState:UIControlStateNormal];
[button2
addTarget:self
action:@selector(stopPlayer)
forControlEvents:UIControlEventTouchUpInside];
[self.view
addSubview:button2];

UIButton *button3=[UIButton
buttonWithType:UIButtonTypeRoundedRect];
button3.frame=CGRectMake(90,
220, 100,
40);
[button3
setTitle:@"播放录制" forState:UIControlStateNormal];
[button3
addTarget:self
action:@selector(startRecord)
forControlEvents:UIControlEventTouchUpInside];
[self.view
addSubview:button3];

}
-(void)startPlayer{

//设置label状态显示
显示为正在录制

label.textColor = [UIColor
redColor];

label.text =
@"录制中...";

label.textAlignment =
UITextAlignmentCenter;

//判断当前的录制状态和播放状态

if (recoder.isRecording) {
[recoder
stop];
}

if (player.isPlaying) {
[player
stop];
}

NSError *err = nil;

//设定录制信息
[[AVAudioSession
sharedInstance]
setCategory:AVAudioSessionCategoryRecord
error:&err];
[[AVAudioSession
sharedInstance] setActive:YES
error:&err];

//设置采样的详细数据

NSMutableDictionary *settings = [NSMutableDictionary
dictionary];
[settings
setValue:[NSNumber
numberWithInt:kAudioFormatLinearPCM]
forKey:AVFormatIDKey];
[settings
setValue:[NSNumber
numberWithFloat:44100.0]
forKey:AVSampleRateKey];
//采样率
[settings
setValue:[NSNumber
numberWithInt:1]

forKey:AVNumberOfChannelsKey];//通道的数目
[settings
setValue:[NSNumber
numberWithInt:16]
forKey:AVLinearPCMBitDepthKey];//采样位数
默认 16
[settings
setValue:[NSNumber
numberWithBool:NO]
forKey:AVLinearPCMIsBigEndianKey];//大端还是小端
是内存的组织方式
[settings
setValue:[NSNumber
numberWithBool:NO]
forKey:AVLinearPCMIsFloatKey];//采样信号是整数还是浮点数

//定义路径,设定要保存的位置 /BDEIDJDFDSF-SDfDS4232/document

NSString *dir = [NSHomeDirectory()
stringByAppendingPathComponent:@"documents"];

//设定路径

NSString *savePath = [NSString
stringWithFormat:@"%@/testAudio.aif",dir];

NSLog(@"savaPath:%@",savePath);

//定义URL

NSURL *fileUrl = [NSURL
fileURLWithPath:savePath];

if (err) {

NSLog(@"录制之前配置出错了!");

return;
}

//初始化了录制的类

recoder = [[AVAudioRecorder
alloc] initWithURL:fileUrl
settings:settings error:&err];

//开始录制
[recoder
record];

}
-(void)stopPlayer{

//设置label状态显示
显示为正在录制

label.textColor = [UIColor
greenColor];

label.text =
@"已停止...";

label.textAlignment =
UITextAlignmentCenter;

//正在录制的时候,要停止录制,正在播放的时候,要停止播放

if (recoder.isRecording) {
[recoder
stop];
}

if(player.isPlaying){

[player
stop];
}
}
-(void)startRecord{

//设置label状态显示
显示为正在录制

label.textColor = [UIColor
purpleColor];

label.text =
@"正在播放...";

label.textAlignment =
UITextAlignmentCenter;

NSError *err = nil;

//获得录制的文件的路径

//定义路径,设定要保存的位置 /BDEIDJDFDSF-SDfDS4232/document

NSString *dir = [NSHomeDirectory()
stringByAppendingPathComponent:@"documents"];

//设定路径

NSString *savePath = [NSString
stringWithFormat:@"%@/testAudio.aif",dir];

//定义URL

NSURL *fileUrl = [NSURL
fileURLWithPath:savePath];

//设定后台播放
[[AVAudioSession
sharedInstance]
setCategory:AVAudioSessionCategoryPlayback
error:&err];

//设定为激活状态
[[AVAudioSession
sharedInstance] setActive:YES
error:&err];

//使用播放器进行播放

player = [[AVAudioPlayer
alloc] initWithContentsOfURL:fileUrl
error:&err];

[player
play];

}

#pragma mark - View lifecycle

/*
// Implement loadView to create a view hierarchy programmatically, without using a nib.
- (void)loadView
{
}
*/

/*
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad
{
[super viewDidLoad];
}
*/

- (void)viewDidUnload
{
[super
viewDidUnload];

// Release any retained subviews of the main view.

// e.g. self.myOutlet = nil;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{

// Return YES for supported orientations

return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

@end
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐