播放和录制音频

AVFoundation控件可以实现一般音频播放和录制。

AVAudioPlayer音频播放类,用于播放大于5秒钟声音,可以播放本地声音,但是不能播放网络媒体文件。能够播放、 暂停、循环和跳过等操作。

AVAudioRecorder音频录制类。

实例AVAudioPlayer

wps_clip_image-30534


添加 AVFoundation.framework 框架

AvplayerViewController.h文件

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

@interface AvplayerViewController : UIViewController <AVAudioPlayerDelegate>

 {
    AVAudioPlayer * player;
}

- (IBAction) stopSong: (id) sender;
- (IBAction) playSong: (id) sender;

@end

AvplayerViewController.m

#import "AvplayerViewController.h"

@implementation AvplayerViewController

- (IBAction) playSong: (id) sender 

{
    NSError *error = nil;
    player = [[AVAudioPlayer alloc] initWithContentsOfURL: 
                 [NSURL fileURLWithPath:[[NSBundle mainBundle] 
                    pathForResource:@"charleston1925_64kb" ofType:@"mp3"]] error:&error];
    player.delegate = self;
    if(error) 

    {
        NSLog(@"%@",[error description]); 
        [error release];
    }
    [player play];
}

- (IBAction) stopSong: (id) sender 

{
    [player stop];
}

- (void)audioPlayerDidFinishPlaying:(AVAudioPlayer *)player successfully:(BOOL)flag 

{
    NSLog(@"播放完成。");    
}

- (void)audioPlayerDecodeErrorDidOccur:(AVAudioPlayer *)player error:(NSError *)error 

{
    NSLog(@"播放错误发生: %@", [error localizedDescription]);    
}

- (void)dealloc 

{
    [player release];
    [super dealloc];
}

@end

AVAudioPlayer委托

AVAudioPlayerDelegate委托对象提供了两个主要方法:

audioPlayerDidFinishPlaying:successfully:

audioPlayerDecodeErrorDidOccur:error:

AVAudioRecorder

新建实例:Recorder

wps_clip_image-7998


RecorderViewController.h文件

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

@interface RecorderViewController : UIViewController
{
    AVAudioRecorder *recorder;
    AVAudioPlayer *player;
    UILabel *label;
}

@property (retain, nonatomic) AVAudioRecorder * recorder;
@property (retain, nonatomic) AVAudioPlayer * player;
@property (retain, nonatomic) IBOutlet UILabel *label;

-(IBAction)recordPushed:(id)sender;
-(IBAction)playPushed:(id)sender;
-(IBAction)stopPushed:(id)sender;

@end


-(NSString *)documentsDirectory
{
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,                                                        NSUserDomainMask, YES); 
    return [paths objectAtIndex:0];
}

音频录制方法

-(IBAction)recordPushed:(id)sender 
{
    label.text = @"recode...";
    if([recorder isRecording])
        return;
    
    if([player isPlaying])
        [player stop];

    NSError *error = nil;
    [[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryRecord  error:&error];
    [[AVAudioSession sharedInstance] setActive:YES error:&error];

    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];//采样信号是整数还是浮点数
    
    NSString *filePath = 
    [NSString stringWithFormat:@"%@/rec_audio.caf", [self documentsDirectory]];
    NSURL *fileUrl = [NSURL fileURLWithPath:filePath];
    
    //[self setRecorder:nil];
    recorder = [[AVAudioRecorder alloc] initWithURL:fileUrl settings:settings error:&error];
    
   // [recorder setMeteringEnabled:YES];
    [recorder record];
}


// 音频播放方法
-(IBAction)playPushed:(id)sender
{
    label.text = @"play...";
    
    if([recorder isRecording])
        [recorder stop];


    if([player isPlaying])
        [player stop];
    
    NSString *filePath = 
    [NSString stringWithFormat:@"%@/rec_audio.caf", [self documentsDirectory]];
    NSURL *fileUrl = [NSURL fileURLWithPath:filePath];
    NSError *error = nil;
   // [self setPlayer:nil];
    [[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback
                                           error:&error];
    [[AVAudioSession sharedInstance] setActive:YES error:&error];
    player = [[AVAudioPlayer alloc] initWithContentsOfURL:fileUrl error:&error];
   // [player setMeteringEnabled:YES];
    [player play];
}


// 音频停止方法
-(IBAction)stopPushed:(id)sender
{
    label.text = @"stop...";
    if([recorder isRecording])
        [recorder stop];

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

 







评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值