iOS Dev.程序员首页投稿(暂停使用,暂停投稿)

Apple Watch音频录制,.wav转换.mp3,获取音频文

2016-03-30  本文已影响932人  Two_Seven
录音.png
1.音频录制

实现在Apple Watch上面的录制音频功能,其实难度不大,只需要调用WKInterfaceController的方法。完整的代码如下

 NSURL *url =  [[NSFileManager defaultManager] containerURLForSecurityApplicationGroupIdentifier:@"group.cn.seven.eg"];
 NSURL *fileUrl = [url URLByAppendingPathComponent:@"record.wav"];

 NSDictionary *audioOptions = [NSDictionary dictionaryWithObjectsAndKeys:@"发送",
                              WKAudioRecorderControllerOptionsActionTitleKey,
                              [NSNumber numberWithInt:30],
                              WKAudioRecorderControllerOptionsMaximumDurationKey, nil];

[self presentAudioRecorderControllerWithOutputURL:fileUrl preset:WKAudioRecorderPresetHighQualityAudio options:audioOptions completion:^(BOOL didSave, NSError * _Nullable error) {
            
            if (error) {
                return ;
            }
            
            if (didSave) {
                [[RAWETelecastManager sharedManager] sendRunnerTelecastUrl:fileUrl duration:0];
            }
        }];

需要注意的是想要在Watch上录制音频,必须打开App Groups这个功能,然后提供Groups路径给系统来存储文件。具体设置路径Capabilities->App Groups。工程中的Taget都需要在同一个Groups中。不然就算在Watch上面录制成功了在App上面也读取不到。
audioOptions参数是对录音功能的一些设置,我这里设置的是右上角的按钮Title和最大录音时长。

App Groups.png
录制音频成功后,就可以发送给App端了,具体的传送方法可以参考之前的这篇文章Watch开发中遇到的那些问题(WatchOS 2)
2. wav转换mp3

其实上面的方法支持wav/mp4/m4a三种录音格式,只需要在保存时提供相应的后缀名就可以了,研究类型转换是项目本身上传到服务器的文件是mp3类型,直接上代码。

+ (BOOL)convertFromWavToMp3WithData:(NSData *)data
{
    NSString *cachePath = [NSFileManager cacheDiretory];
    NSString *wavPath = [NSString stringWithFormat:@"%@/watchRecorder.wav",cachePath];
    NSString *mp3Path = [NSString stringWithFormat:@"%@/watchRecorder.mp3",cachePath];
    
    if (![data writeToURL:[NSURL fileURLWithPath:wavPath] atomically:YES]) {
        return NO;
    }
   
    int read, write;
    
    FILE *pcm = fopen([wavPath cStringUsingEncoding:NSUTF8StringEncoding], "rb");  //source
                                      //skip file header
    FILE *mp3 = fopen([mp3Path cStringUsingEncoding:NSUTF8StringEncoding], "wb");  //output
    
    if (!pcm || !mp3)
    {
        fclose(mp3);
        fclose(pcm);
        return NO;
    }
    
    const int PCM_SIZE = 8192;
    const int MP3_SIZE = 8192;
    short int pcm_buffer[PCM_SIZE*2];
    unsigned char mp3_buffer[MP3_SIZE];
    
    lame_t lame = lame_init();
    lame_set_in_samplerate(lame, 22050);
    lame_set_VBR(lame, vbr_default);
    lame_init_params(lame);
    
    do {
        read = (int)fread(pcm_buffer, 2*sizeof(short int), PCM_SIZE, pcm);
        if (read == 0)
            write = lame_encode_flush(lame, mp3_buffer, MP3_SIZE);
        else
            write = lame_encode_buffer_interleaved(lame, pcm_buffer, read, mp3_buffer, MP3_SIZE);
        
        fwrite(mp3_buffer, write, 1, mp3);
        
    } while (read != 0);
    
    lame_close(lame);
    fclose(mp3);
    fclose(pcm);

    return YES;
}

在调试上面的方法过程中,出现过音频被压缩的情况,后来发现是录制音频时的WKAudioRecorderPresetHighQualityAudio 参数与转换方法中的lame_set_in_samplerate(lame, 22050)参数造成的,这两个相对应的参数是我一次次试出来的,具体的原理还是不懂,只大概知道是音频赫兹之类的问题。所以如果恰好你了解这块,请给我解答一下,万分感谢。

3.获取音频文件的时长

在录制过程中,因为录音界面是系统的,没有找到相应的接口来获取录音时长,所以就查了一下相应获取时长的方法,还是直接上代码。

 AVURLAsset* audioAsset =[AVURLAsset URLAssetWithURL:[NSURL fileURLWithPath:mp3Path] options:nil];
                    
 CMTime audioDuration = audioAsset.duration;
                    
 float audioDurationSeconds =CMTimeGetSeconds(audioDuration);

其实都是些拿来就可以直接用的代码,坑我都趟过了,如果其他问题也可以留言问我,不过我可能不会。

上一篇下一篇

猜你喜欢

热点阅读