日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 综合教程 >内容正文

综合教程

IOS 实现 AAC格式 录音 录音后自动播放

發布時間:2023/12/13 综合教程 23 生活家
生活随笔 收集整理的這篇文章主要介紹了 IOS 实现 AAC格式 录音 录音后自动播放 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

廢話不說了 不知道aac可以百度一下 下面直接上代碼,一個h文件 一個m文件 搞定!

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

@interface AudioRecord : NSObject<AVAudioRecorderDelegate, AVAudioPlayerDelegate>


/**
 *  獲取單例對象
 */
+(AudioRecord *)shareAudioRecord;

/**
 *  將要錄音
 *
 *  @return <#return value description#>
 */
- (BOOL)canRecord;

/**
 *  停止錄音
 */
- (void)stopRecord;

/**
 *  開始錄音
 */
- (void)onStatrRecord;



/**
 *  初始化音頻檢查
 */
-(void)initRecordSession;


/**
 *  初始化文件存儲路徑
 *
 *  @return <#return value description#>
 */
- (NSString *)audioRecordingPath;


/**
 *  錄音器
 */
@property (nonatomic, retain) AVAudioRecorder *audioRecorder;


/**
 *  錄音播放器
 */
@property (nonatomic, retain) AVAudioPlayer *audioPlayer;



@end

下面是m文件

//
//  AudioRecord.m
//  audio



#import "AudioRecord.h"
@implementation AudioRecord : NSObject

+(AudioRecord *)shareAudioRecord{
    static AudioRecord *sharedAccountManagerInstance = nil;

    static dispatch_once_t predicate; dispatch_once(&predicate, ^{
        sharedAccountManagerInstance = [[self alloc] init];
    });
    return sharedAccountManagerInstance;
}




/**
 *  設置錄制的音頻文件的位置
 *
 *  @return <#return value description#>
 */
- (NSString *)audioRecordingPath{
    
    NSString *result = nil;
    NSArray *folders = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsFolde = [folders objectAtIndex:0];
    result = [documentsFolde stringByAppendingPathComponent:@"Recording.aac"];
    return (result);
    
}

/**
 *  在初始化AVAudioRecord實例之前,需要進行基本的錄音設置
 *
 *  @return <#return value description#>
 */
- (NSDictionary *)audioRecordingSettings{
    
    NSDictionary *settings = [[NSDictionary alloc] initWithObjectsAndKeys:
                              
                              [NSNumber numberWithFloat:44100.0],AVSampleRateKey ,    //采樣率 8000/44100/96000
                              
                              [NSNumber numberWithInt:kAudioFormatMPEG4AAC],AVFormatIDKey,  //錄音格式
                              
                              [NSNumber numberWithInt:16],AVLinearPCMBitDepthKey,   //線性采樣位數  8、16、24、32
                              
                              [NSNumber numberWithInt:2],AVNumberOfChannelsKey,      //聲道 1,2
                              
                              [NSNumber numberWithInt:AVAudioQualityLow],AVEncoderAudioQualityKey, //錄音質量
                              
                              nil];
    return (settings);
}

/**
 *  停止音頻的錄制
 *
 *  @param recorder <#recorder description#>
 */
- (void)stopRecordingOnAudioRecorder:(AVAudioRecorder *)recorder{
    AVAudioSession *session = [AVAudioSession sharedInstance];
    [session setCategory:AVAudioSessionCategoryPlayback error:nil];  //此處需要恢復設置回放標志,否則會導致其它播放聲音也會變小
    [session setActive:YES error:nil];
    [recorder stop];
}

/**
 *  @param recorder <#recorder description#>
 *  @param flag     <#flag description#>
 */
- (void)audioRecorderDidFinishRecording:(AVAudioRecorder *)recorder successfully:(BOOL)flag{
    

    if (flag == YES) {
        NSLog(@"錄音完成!");
        NSError *playbackError = nil;
        NSError *readingError = nil;
        NSData *fileData = [NSData dataWithContentsOfFile:[self audioRecordingPath] options:NSDataReadingMapped error:&readingError];
        
        AVAudioPlayer *newPlayer = [[AVAudioPlayer alloc] initWithData:fileData
                                                                 error:&playbackError];
        
        self.audioPlayer = newPlayer;
        
        if (self.audioPlayer != nil) {
            self.audioPlayer.delegate = self;
            if ([self.audioPlayer prepareToPlay] == YES &&
                [self.audioPlayer play] == YES) {
                NSLog(@"開始播放音頻!");
            } else {
                NSLog(@"不能播放音頻!");
            }
        }else {
            NSLog(@"播放失敗!");
        }
        
    } else {
        NSLog(@"錄音過程意外終止!");
    }
    self.audioRecorder = nil;
}


/**
 *  初始化音頻檢查
 */
-(void)initRecordSession
{
    AVAudioSession *session = [AVAudioSession sharedInstance];
    [session setCategory:AVAudioSessionCategoryPlayAndRecord error:nil];
    [session setActive:YES error:nil];
    
}

/**
 *  開始錄音
 */
- (void)onStatrRecord
{
    
    /**
     *  檢查權限
     */
    if (![self canRecord])
    {
        
        [[[UIAlertView alloc] initWithTitle:nil
                                    message:[NSString stringWithFormat:@"應用需要訪問您的麥克風。請啟用麥克風!"]
                                   delegate:nil
                          cancelButtonTitle:@"同意"
                          otherButtonTitles:nil] show];
        return;
    }
    
    [self initRecordSession];
    
    NSError *error = nil;
    NSString *pathOfRecordingFile = [self audioRecordingPath];
    NSURL *audioRecordingUrl = [NSURL fileURLWithPath:pathOfRecordingFile];
    AVAudioRecorder *newRecorder = [[AVAudioRecorder alloc]
                                    initWithURL:audioRecordingUrl
                                    settings:[self audioRecordingSettings]
                                    error:&error];
    self.audioRecorder = newRecorder;
    if (self.audioRecorder != nil) {
        self.audioRecorder.delegate = self;
        if([self.audioRecorder prepareToRecord] == NO){
            return;
        }
        
        if ([self.audioRecorder record] == YES) {
           
            NSLog(@"錄音開始!");
            
            [self performSelector:@selector(stopRecordingOnAudioRecorder:)
                       withObject:self.audioRecorder
                       afterDelay:10.0f];
            
        } else {
            NSLog(@"錄音失敗!");
            self.audioRecorder =nil;
        }
    } else {
        NSLog(@"auioRecorder實例錄音器失敗!");
    }
}

/**
 *  停止錄音
 */
- (void)stopRecord{
    
    if (self.audioRecorder != nil) {
        if ([self.audioRecorder isRecording] == YES) {
            [self.audioRecorder stop];
        }
        self.audioRecorder = nil;
    }
    
    if (self.audioPlayer != nil) {
        if ([self.audioPlayer isPlaying] == YES) {
            [self.audioPlayer stop];
        }
        self.audioPlayer = nil;
    }
}


/**
 *  將要錄音
 *
 *  @return <#return value description#>
 */
- (BOOL)canRecord
{
    __block BOOL bCanRecord = YES;
    if ([[[UIDevice currentDevice] systemVersion] compare:@"7.0"] != NSOrderedAscending)
    {
        AVAudioSession *audioSession = [AVAudioSession sharedInstance];
        if ([audioSession respondsToSelector:@selector(requestRecordPermission:)]) {
            
            [audioSession performSelector:@selector(requestRecordPermission:) withObject:^(BOOL granted) {
                
                if (granted) {
                    
                    bCanRecord = YES;
                    
                } else {
                    
                    bCanRecord = NO;
                    
                }
                
            }];
            
        }
    }
    return bCanRecord;
}



@end

轉載請注明:http://www.cnblogs.com/wangmars/ 以上也綜合網上大牛的智慧

感謝http://www.cnblogs.com/hanjun/archive/2012/10/30/2747159.html順便也解決了 錄音后播放聲音小的問題。



總結

以上是生活随笔為你收集整理的IOS 实现 AAC格式 录音 录音后自动播放的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。