将音频文件转换为字节数组iPhone时出现OSError -40 [英] OSError -40 while converting an audio file to Byte array iPhone

查看:145
本文介绍了将音频文件转换为字节数组iPhone时出现OSError -40的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在iOS中使用以下代码将mp3文件转换为字节数组。但是,在进入while循环时,它给出err = -40(OSError = -40)。谁能帮忙。或者,请让我知道如何将mp3 / wav文件转换为bytearray。参考-如何转换WAV / CAF文件的示例数据到字节数组?

I am using the following code in iOS to convert an mp3 file to byte array. However, on entering the while loop, it gives err = -40 (OSError = -40) . Can anyone please help. Or kindly let me know how i can convert an mp3/wav file to bytearray. Reference - How to convert WAV/CAF file's sample data to byte array?

 NSString *urlHere = [[NSBundle mainBundle] pathForResource:@"r2d2" ofType:@"mp3"];
 CFURLRef url = (CFURLRef)[NSURL fileURLWithPath:urlHere];


AudioFileID audioFile;
OSStatus err = AudioFileOpenURL(url, kAudioFileReadPermission, 0, &audioFile);
// get the number of audio data bytes
UInt64 numBytes = 0;
UInt32 dataSize = sizeof(numBytes);
err = AudioFileGetProperty(audioFile, kAudioFilePropertyAudioDataByteCount, &dataSize, &numBytes);

unsigned char *audioBuffer = (unsigned char *)malloc(numBytes);

UInt32 toRead = numBytes;
UInt64 offset = 0;
unsigned char *pBuffer = audioBuffer;
while(true) {
    err = AudioFileReadBytes(audioFile, true, offset, &toRead, &pBuffer);
    if (kAudioFileEndOfFileError == err) {
        // cool, we're at the end of the file
        break;
    } else if (noErr != err) {
        // uh-oh, some error other than eof
        break;
    }
    // advance the next read offset
    offset += toRead;
    // advance the read buffer's pointer
    pBuffer += toRead;
    toRead = numBytes - offset;
    if (0 == toRead) {
        // got to the end of file but no eof err
        break;
    }
}


推荐答案

I有完全相同的问题。
您需要做的就是删除 pBuffer 变量旁边的& 符号,因为它已经是一个指针:

I had the exact same problem. All you need to do is to remove the & sign next to the pBuffer variable, since it is already a pointer:

err = AudioFileReadBytes(audioFile, true, offset, &toRead, pBuffer);

这篇关于将音频文件转换为字节数组iPhone时出现OSError -40的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆