ios 语音 扬声器


#import "ViewController.h"

#import <AVFoundation/AVFoundation.h>


@interface ViewController ()<AVAudioPlayerDelegate>


@property (nonatomic, strong) NSURL *recordedFile;//store path

@property (nonatomic, strong) AVAudioPlayer *player;//play

@property (nonatomic, strong) AVAudioRecorder *recorder;

@property (nonatomic, strong) NSString *recoderName;

@property (weak, nonatomic) IBOutlet UIButton *soundBtn;

@property (nonatomic, strong) NSMutableDictionary *paramters;


@end


@implementation ViewController


- (NSMutableDictionary *)paramters

{

    if(!_paramters)

    {

        self.paramters = [NSMutableDictionary new];

        //设置录音格式

        [self.paramters setValue:[NSNumber numberWithInt:kAudioFormatMPEG4AAC] forKey:AVFormatIDKey];

        //设置录音采样率

        [self.paramters setValue:[NSNumber numberWithFloat:8000] forKey:AVSampleRateKey];

        //录音的质量

        [self.paramters setValue:[NSNumber numberWithInt:AVAudioQualityHigh] forKey:AVEncoderAudioQualityKey];

        //线性采样位数  8162432

        [self.paramters setValue:[NSNumber numberWithInt:16] forKey:AVLinearPCMBitDepthKey];

        //录音通道数  1 2

        [self.paramters setValue:[NSNumber numberWithInt:1] forKey:AVNumberOfChannelsKey];

    }

    return _paramters;

}


- (void)viewDidLoad {

    [super viewDidLoad];

    [self.soundBtn addTarget:self action:@selector(recordingSound) forControlEvents:UIControlEventTouchDown];

    [self.soundBtn addTarget:self action:@selector(endSound) forControlEvents:UIControlEventTouchUpInside];


}


- (void)recordingSound

{

    [self setRecordPath];

    self.recorder = [[AVAudioRecorder alloc] initWithURL:self.recordedFile settings:self.paramters error:nil];

    [[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryRecord error:nil];

    //缓冲录音

    [self.recorder prepareToRecord];

    //开始录音

    [self.recorder record];

    //反复录音需要设置为空

    self.player = nil;

}


- (void)endSound

{

    [self.recorder stop];

    self.recorder = nil;

}


- (void)setRecordPath

{

    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];

    [formatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];

    NSString *dateStr = [formatter stringFromDate:[NSDate date]];

    NSString *path = [[NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, true) lastObject] stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.aac", dateStr]];

    self.recoderName = [NSString stringWithFormat:@"%@.aac", dateStr];

    self.recordedFile = [NSURL fileURLWithPath:path];

    NSLog(@"%@", path);

}



- (IBAction)play:(UIButton *)sender

{

    //初始化播放器的时候如下设置

    UInt32 sessionCategory = kAudioSessionCategory_MediaPlayback;

    AudioSessionSetProperty(kAudioSessionProperty_AudioCategory,

                            sizeof(sessionCategory),

                            &sessionCategory);

    UInt32 audioRouteOverride = kAudioSessionOverrideAudioRoute_Speaker;

    AudioSessionSetProperty (kAudioSessionProperty_OverrideAudioRoute,

                             sizeof (audioRouteOverride),

                             &audioRouteOverride);

    AVAudioSession *audioSession = [AVAudioSession sharedInstance];

    //默认情况下扬声器播放

    [audioSession setCategory:AVAudioSessionCategoryPlayback error:nil];

    [audioSession setActive:YES error:nil];

    //建议播放之前设置yes,播放结束设置no,这个功能是开启红外感应

    [[UIDevice currentDevice] setProximityMonitoringEnabled:YES];

    //添加监听

    [[NSNotificationCenter defaultCenter] addObserver:self

                                             selector:@selector(sensorStateChange:)

                                                 name:@"UIDeviceProximityStateDidChangeNotification"

                                               object:nil];

    NSError *playerError;

    self.player = [[AVAudioPlayer alloc] initWithContentsOfURL:self.recordedFile error:&playerError];

    self.player.delegate = self;

    //开始播放

    [self.player play];

}


-(void)sensorStateChange:(NSNotificationCenter *)notification;

{

    //如果此时手机靠近面部放在耳朵旁,那么声音将通过听筒输出,并将屏幕变暗(省电啊)

    if ([[UIDevice currentDevice] proximityState] == YES)

    {

        [[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayAndRecord error:nil];

    }

    else

    {

        [[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error:nil];

    }

}

@end


有疑问的可以直接联系我  应该是没问题  我测试了半天

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值