2012-12-19 3 views
0

나는 안드로이드 앱을 아이폰에 이식하고있다. (안드로이드 버전을 기반으로 한 아이폰 앱을 더 좋아한다.) 커다란 비 압축 오디오 파일을 분할하고 결합해야한다.iOS의 NSOutputStream에 NSInputStream을 쓰는 동안 어떻게 읽습니까?

현재 모든 파일을 메모리에로드하고 분할하여 별도의 기능으로 결합합니다. 그것은 100MB + 파일과 충돌합니다.

나는이 녹음 (파일 1과 파일 2) 내가 파일 2가 FILE1 내부에 삽입 할 분할 위치를 가지고 :

이 그것을 할 필요가 새로운 프로세스입니다.

-file1 및 file2에 대한 입력 스트림과 출력 파일에 대한 출력 스트림을 만듭니다. 이 분리 점에 도달하고 내가 출력 파일에 모든 데이터를 기록 할 때까지 새 CAF 헤더

-rewrite

는 inputStream1에서 데이터를 - 읽기. 및 출력 스트림에 씁니다.

- inputStream2의 모든 데이터를 읽은 다음 출력 파일에 씁니다.

- inputStream1에서 나머지 데이터를 읽고 출력 파일에 씁니다. 여기에 프로세스에 대한 내 안드로이드 코드

입니다 :

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 함수이다 : 나는 아이폰 OS에서이 작업을 수행하려면 어떻게

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); 
     } 

    } 

} 

? 두 개의 NSInputStreams와 NSOutputStream에 대해 동일한 델리게이트를 사용하면 매우 엉망이 될 것입니다. 이 작업을 수행하는 방법에 대한 예를 본 사람이 있습니까?

답변

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;