0

我正在将一个 Android 应用程序移植到 iPhone(更像是基于 Android 版本改进 iPhone 应用程序),我需要拆分和组合大型未压缩音频文件。

目前,我将所有文件加载到内存中并将它们拆分并组合成单独的函数。它会因 100MB 以上的文件而崩溃。

这是执行此操作所需的新过程:

我有两个录音(file1 和 file2)和一个拆分位置,我希望将 file2 插入到 file1 中。

- 为 file1 和 file2 创建输入流,为 outputfile 创建输出流。

-重写新的 CAF 标头

- 从 inputStream1 读取数据,直到它到达分割点,然后我将所有数据写入输出文件。并将其写入输出流。

- 从 inputStream2 读取所有数据并将其写入输出文件。

- 从 inputStream1 读取剩余数据并将其写入输出文件。

这是我的Android代码:

    File file1File = new File(file1);
    File file2File = new File(file2);

    long file1Length = file1File.length();
    long file2Length = file2File.length();

    FileInputStream file1ByteStream = new FileInputStream(file1);
    FileInputStream file2ByteStream = new FileInputStream(file2);
    FileOutputStream outputFileByteStream = new FileOutputStream(outputFile);


    // time = fileLength / (Sample Rate * Channels * Bits per sample / 8)
    // convert position to number of bytes for this function
    long sampleRate = eRecorder.RECORDER_SAMPLERATE;
    int channels = 1;
    long bitsPerSample = eRecorder.RECORDER_BPP;
    long bytePositionLength = (position * (sampleRate * channels * bitsPerSample / 8)) / 1000;



    //calculate total data size
            int dataSize = 0;
            dataSize = (int)(file1Length + file2Length);

            WriteWaveFileHeaderForMerge(outputFileByteStream, dataSize,
                    dataSize + 36,
                    eRecorder.RECORDER_SAMPLERATE, 1,
                    2 * eRecorder.RECORDER_SAMPLERATE);




    long bytesWritten = 0;

    int length = 0;

    //set limit for bytes read, and write file1 bytes to outputfile until split position reached
    int limit = (int)bytePositionLength;


    //read bytes to limit
    writeBytesToLimit(file1ByteStream, outputFileByteStream, limit);    
    file1ByteStream.close();


    file2ByteStream.skip(44);//skip wav file header
    writeBytesToLimit(file2ByteStream, outputFileByteStream, (int)file2Length);
    file2ByteStream.close();

    //calculate length of remaining file1 bytes to be written
    long file1offset = bytePositionLength;

    //reinitialize file1 input stream
    file1ByteStream = new FileInputStream(file1);

    file1ByteStream.skip(file1offset);
    writeBytesToLimit(file1ByteStream, outputFileByteStream, (int)file1Length);

    file1ByteStream.close();
    outputFileByteStream.close();

这是我的 writeBytesToLimit 函数:

private void writeBytesToLimit(FileInputStream inputStream, FileOutputStream outputStream, int byteLimit) throws IOException
{
    int bytesRead = 0;
    int chunkSize = 65536;
    int length = 0;
    byte[] buffer = new byte[chunkSize];
    while((length = inputStream.read(buffer)) != -1)
    {
        bytesRead += length;
        if(bytesRead >= byteLimit)
        {
            int leftoverBytes = byteLimit % chunkSize;              
            byte[] smallBuffer = new byte[leftoverBytes];
            System.arraycopy(buffer, 0, smallBuffer, 0, leftoverBytes);
            outputStream.write(smallBuffer);
            break;
        }

        if(length == chunkSize)
            outputStream.write(buffer);
        else
        {
            byte[] smallBuffer = new byte[length];
            System.arraycopy(buffer, 0, smallBuffer, 0, length);
            outputStream.write(smallBuffer);
        }

    }

}

我如何在 iOS 中做到这一点?对两个 NSInputStreams 和一个 NSOutputStream 使用相同的委托看起来会变得非常混乱。有没有人看过一个如何做到这一点的例子(并且做得干净)?

4

1 回答 1

2

我最终使用了 NSFileHandle。例如,这是我正在做的第一部分。

NSData *readData = [[NSData alloc] init];
NSFileHandle *reader1 = [NSFileHandle fileHandleForReadingAtPath:file1Path];
NSFileHandle *writer = [NSFileHandle fileHandleForWritingAtPath:outputFilePath];

//start reading data from file1 to split point and writing it to file
long bytesRead = 0;
while(bytesRead < splitPointInBytes)
{

    //read a chunk of data
    readData = [reader1 readDataOfLength:chunkSize];
    if(readData.length == 0)break;
    //trim data if too much was read
    if(bytesRead + readData.length > splitPointInBytes)
    {
        //get difference of read bytes and byte limit
        long difference = bytesRead + readData.length - splitPointInBytes;

        //trim data
        NSMutableData *readDataMutable = [NSMutableData dataWithData:readData];
        [readDataMutable setLength:readDataMutable.length - difference];
        readData = [NSData dataWithData:readDataMutable];

        NSLog(@"Too much data read, trimming");
    }

    //write data to output file
    [writer writeData:readData];

    //update byte counter
    bytesRead += readData.length;
}
long file1BytesWritten = bytesRead;
于 2012-12-20T22:34:20.197 回答