ios - AVAudioPlayer无法播放音频的问题

 mobiledu2502870587 发布于 2022-11-01 19:27

今天开始做录音的功能。点击recordbtn开始录音,再次点击,则停止录音,点击play键播放录音
然后就遇到问题了。
点击play键后,[self.audioPlayer prepareToPlay] 总是为no,即使把这个去掉,强制[self.audioPlayer play];也没有效果,请问是怎么回事?
另:用player播放其他音频是可以的,作为对比测试,播放了一首歌曲,没问题,就是播放自己录制的音频出问题,是不是录制的时候就出错了?
附代码:

-(void)record{
    NSLog(@"recording");
    AVAudioSession *audioSession  = [AVAudioSession sharedInstance];
    [audioSession setCategory:AVAudioSessionCategoryRecord error:nil];
    [audioSession setActive:YES error:nil];
    NSString *tripReportAudioPath = [self audioRecordingPath];
    NSError *err = nil;
    NSURL *tripReportAudioURL = [NSURL URLWithString:tripReportAudioPath];
    AVAudioRecorder *tripReportRecorder = [[AVAudioRecorder alloc]initWithURL:tripReportAudioURL settings:[self audioRecordingSetting] error:&err];
    if (tripReportRecorder  != nil) {
        tripReportRecorder.delegate = self;
        if ([tripReportRecorder prepareToRecord] && self.recordBtn.selected == NO) {
            self.recordBtn.selected = YES;
            [tripReportRecorder record];
        }else if (self.recordBtn.selected == YES){
            self.recordBtn.selected = NO;
            [tripReportRecorder stop ];
        }
    }else{
        NSLog(@"record err:%@",[err description]);
    }
}



//生成录音文件地址
-(NSString *)audioRecordingPath{
    NSString *result = nil;
    NSArray *folders = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *docdir = [folders objectAtIndex:0];
    result = [docdir stringByAppendingPathComponent:@"tripReport.caf"];
    return result;
}
//录音器的相关设置
-(NSDictionary *)audioRecordingSetting{
    NSDictionary *result = nil;
    NSMutableDictionary *settings = [[NSMutableDictionary alloc]init ];
    //录制的音频格式
    [settings setValue:[NSNumber numberWithInteger:kAudioFormatMPEG4AAC] forKey:AVFormatIDKey];
    //采样率
    [settings setValue:[NSNumber numberWithFloat:44100.0f] forKey:AVSampleRateKey];
    //信道数
    [settings setValue:[NSNumber numberWithInteger:1] forKey:AVNumberOfChannelsKey];
    //录音的质量
    [settings setValue:[NSNumber numberWithInteger:AVAudioQualityHigh] forKey:AVEncoderAudioQualityKey];
    [settings setValue:[NSNumber numberWithInt:12800] forKey:AVEncoderBitRateKey];
    [settings setValue:[NSNumber numberWithInt:16] forKey:AVLinearPCMBitDepthKey];

    result = [NSDictionary dictionaryWithDictionary:settings];
    return result;
}




-(void)play{

    AVAudioSession *audioSession  = [AVAudioSession sharedInstance];

    [audioSession setCategory:AVAudioSessionCategoryPlayback           
    error:nil];
     [audioSession setActive:YES error:nil];

    NSError *playBackError = nil;
    NSError *readingError = nil;
    NSString *audioPath = [self audioRecordingPath];
    //        NSString *audioPath  = [[NSBundle mainBundle]pathForResource:@"Love" ofType:@"mp3"];
    NSLog(@"path:%@",audioPath);
    NSURL *url = [NSURL URLWithString:audioPath];
    NSData *fileData = [NSData dataWithContentsOfFile:audioPath options:NSDataReadingMapped error:&readingError];
    self.audioPlayer = [[AVAudioPlayer alloc]initWithData:fileData error:&playBackError];





    if (self.audioPlayer != nil) {
        NSLog(@"finishi3");
        self.audioPlayer.delegate = self;
        self.audioPlayer.volume = 50.0;

        if ([self.audioPlayer prepareToPlay]) {
            NSLog(@"finishi4");
            UInt32 doChangeDefaultRoute = 1;
            AudioSessionSetProperty(kAudioSessionProperty_OverrideCategoryDefaultToSpeaker, sizeof(doChangeDefaultRoute), &doChangeDefaultRoute);
            [self.audioPlayer play];
        }
    }else{
        NSLog(@"error:%@",[playBackError description]);
    }

}

3 个回答
  • 题主 您这个问题是怎么解决的 我现在也遇到了同样的问题

    2022-11-12 01:54 回答
  • 题主解决了吗,我也碰到了,需要帮忙啊,谢谢.

    2022-11-12 01:54 回答
  • 看下自己录制的音频是否保存成功,如果播放音频正确,那么问题肯定就是出在录制上面。

    检查

    进入沙河,用电脑播放器试试,看是否可以播放,是不是文件损坏了?还有你要确定,你的代码播放器尝试播放一个网络音频,看是否成功播放。这样可以定位到到底哪里出问题了

    • 录制音频是否成功保存到沙盒

    • 录制的音频是否保存了正确的格式

    2022-11-12 01:54 回答
撰写答案
今天,你开发时遇到什么问题呢?
立即提问
热门标签
PHP1.CN | 中国最专业的PHP中文社区 | PNG素材下载 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有