實作一個錄音與播放的功能
1. 開啓一個新的專案
2. 在storyboard上加入兩個Button及一個Label
3. 加上AVFoundation Framework
4. 在mainViewController.h加入所需變數
#import <UIKit/UIKit.h>
#import <AVFoundation/AVFoundation.h>
@interface mainViewController : UIViewController
{
NSURL *recordedFile;
AVAudioPlayer *player;
AVAudioRecorder *recorder;
BOOL isRecording;
}
@property (weak, nonatomic) IBOutlet UIButton *recordButton;
@property (weak, nonatomic) IBOutlet UIButton *playButton;
@property (weak, nonatomic) IBOutlet UILabel *timeLabel;
@property(nonatomic)NSTimer *songTimer;
@property(nonatomic)NSTimer *playTimer;
@end
5.在mainViewController.m加入相關程式碼
#import "mainViewController.h"
@interface mainViewController ()
@end
@implementation mainViewController
@synthesize recordButton,playButton,timeLabel;
@synthesize songTimer, playTimer;
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
isRecording = NO;
[playButton setEnabled:NO];
playButton.titleLabel.alpha = 0.5;
recordedFile = [NSURL fileURLWithPath:[NSTemporaryDirectory() stringByAppendingString:@"RecordedFile"]];
AVAudioSession *session = [AVAudioSession sharedInstance];
NSError *sessionError;
[session setCategory:AVAudioSessionCategoryPlayAndRecord error:&sessionError];
if(session == nil)
NSLog(@"Error creating session: %@", [sessionError description]);
else
[session setActive:YES error:nil];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (IBAction)activeRecord:(id)sender {
if (playTimer!= nil){
[playTimer invalidate];
playTimer = nil;
}
//If the app is note recording, we want to start recording, disable the play button, and make the record button say "STOP"
if(!isRecording)
{
isRecording = YES;
[recordButton setTitle:@"停止" forState:UIControlStateNormal];
[playButton setEnabled:NO];
[playButton.titleLabel setAlpha:0.5];
recorder = [[AVAudioRecorder alloc] initWithURL:recordedFile settings:nil error:nil];
[recorder prepareToRecord];
[recorder record];
player = nil;
songTimer = [NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(timeRecorder) userInfo:nil repeats:YES]; //scheduledTimerWithTimeInterval 不可太小,模擬器速度撐不住。最好使用0.5,模擬器才不會卡卡的
}
//If the app is recording, we want to stop recording, enable the play button, and make the record button say "REC"
else
{
isRecording = NO;
[recordButton setTitle:@"錄音" forState:UIControlStateNormal];
[playButton setEnabled:YES];
[playButton.titleLabel setAlpha:1];
[songTimer invalidate];
songTimer = nil;
[recorder stop];
recorder = nil;
NSError *playerError;
player = [[AVAudioPlayer alloc] initWithContentsOfURL:recordedFile error:&playerError];
if (player == nil)
{
NSLog(@"ERror creating player: %@", [playerError description]);
}
}
}
- (IBAction)playSound:(id)sender {
if (songTimer != nil)
{
[songTimer invalidate];
songTimer = nil;
}
//If the track is playing, pause and achange playButton text to "Play"
if([player isPlaying])
{
[player pause];
[playButton setTitle:@"播放" forState:UIControlStateNormal];
[playTimer invalidate];
playTimer = nil;
}
//If the track is not player, play the track and change the play button to "Pause"
else
{
[player play];
[playButton setTitle:@"暫停" forState:UIControlStateNormal];
playTimer = [NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(timeLoader) userInfo:nil repeats:YES];
//scheduledTimerWithTimeInterval 不可太小,模擬器速度撐不住。最好使用0.5,模擬器才不會卡卡的
}
}
- (void)audioPlayerDidFinishPlaying:(AVAudioPlayer *)player successfully:(BOOL)flag
{
[self.playButton setTitle:@"Play" forState:UIControlStateNormal];
}
- (void) showTime:(NSTimeInterval ) time
{
int min;
int sec;
min = (NSInteger ) time/60;
sec = (NSInteger ) time%60;
NSString *showTimeText = [NSString stringWithFormat:@"%2.2d:%2.2d",min, sec];
timeLabel.text = showTimeText;
}
-(void)timeLoader
{
if (player.currentTime == 0)
[self showTime:player.duration];
else
[self showTime:player.currentTime];
if (player.currentTime > player.duration - 0.1)
{
[playButton setTitle:@"播放" forState:UIControlStateNormal];
[playTimer invalidate];
playTimer = nil;
}
}
-(void)timeRecorder
{
[self showTime:recorder.currentTime];
}
@end
6. 執行結果,錄音與播放。





沒有留言:
張貼留言