gpt4 book ai didi

java - 如何在将 NSInputStream 写入 iOS 中的 NSOutputStream 时读取它?

转载 作者:行者123 更新时间:2023-11-28 22:43:04 30 4
gpt4 key购买 nike

我正在将一个 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 使用同一个委托(delegate)看起来会变得非常困惑。有没有人看过如何执行此操作(并清理干净)的示例?

最佳答案

我最终使用了 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;

关于java - 如何在将 NSInputStream 写入 iOS 中的 NSOutputStream 时读取它?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13943862/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com