2012-09-21 10 views
2

에 mpmediaitem를 개조했다 나는 찾아 냈다 (나가 찾아내는 방법을 주문을 받아서 만드는) MpMediaItem를 개조하고 mp3 파일을 얻는 방법. 아마 그것은 최선의 방법은 아니지만 작동합니다.객관적인 c는 - ogg vorbis

* .mp3 대신 * .ogg 파일을 구할 수있는 방법이 있습니까? 적절한 접근 방법은 무엇입니까?

-(void)exportMP3:(NSURL*)url toFileUrl:(NSString*)fileURL 
{ 
    AVURLAsset *asset=[[AVURLAsset alloc] initWithURL:url options:nil]; 
    AVAssetReader *reader=[[AVAssetReader alloc] initWithAsset:asset error:nil]; 
    NSMutableArray *myOutputs =[[NSMutableArray alloc] init]; 
    for(id track in [asset tracks]) 
    { 
     AVAssetReaderTrackOutput *output=[AVAssetReaderTrackOutput assetReaderTrackOutputWithTrack:track outputSettings:nil]; 
     [myOutputs addObject:output]; 
     [reader addOutput:output]; 
    } 
    [reader startReading]; 
    NSFileHandle *fileHandle ; 
    NSFileManager *fm=[NSFileManager defaultManager]; 
    if(![fm fileExistsAtPath:fileURL]) 
    { 
     [fm createFileAtPath:fileURL contents:[[NSData alloc] init] attributes:nil]; 
    }else{ 
     NSURL *url = [NSURL URLWithString:fileURL]; 
     [fm removeItemAtURL:url error:nil]; 
     [fm createFileAtPath:fileURL contents:[[NSData alloc] init] attributes:nil]; 

    } 
    fileHandle=[NSFileHandle fileHandleForWritingAtPath:fileURL];  
    [fileHandle seekToEndOfFile]; 

    AVAssetReaderOutput *output=[myOutputs objectAtIndex:0]; 
    int totalBuff=0; 
    int test = 1; 
    while(test == 1) 
    { 
     CMSampleBufferRef ref=[output copyNextSampleBuffer]; 

     if(ref==NULL) 
      test = 0; 

     if(ref==NULL) 
      break; 
     //copy data to file 
     //read next one 
     AudioBufferList audioBufferList; 
     NSMutableData *data; 
     CMBlockBufferRef blockBuffer; 
     CMSampleBufferGetAudioBufferListWithRetainedBlockBuffer(ref, NULL, &audioBufferList, sizeof(audioBufferList), NULL, NULL, 0, &blockBuffer); 

     for(int y=0; y<audioBufferList.mNumberBuffers; y++) 
     { 
      AudioBuffer audioBuffer = audioBufferList.mBuffers[y]; 
      void *frame = audioBuffer.mData; 

      data = [[NSMutableData alloc] initWithBytes:frame length:audioBuffer.mDataByteSize]; 
     } 
     totalBuff++; 
     NSLog(@"\n%d\n",totalBuff); 

     int time = [self.mediaPlayer durationOfCurrentItem]; 

     if (totalBuff * 2 <= time + 1)   
      [fileHandle writeData:data]; 

    } 
    [fileHandle closeFile]; 
} 
+0

MP3 인코딩 코드가 올바르게 작동하면 [lame] (http://lame.sourceforge.net/)과 같은 제 3 자 라이브러리가 없으면 MP3 파일을 올바르게 인코딩 할 수 있습니다. ios에서 MP3 형식으로 오디오를 인코딩하는 것은 불가능합니다. mp3 파일 내부에서 다른 형식을 사용할 수있는 오디오 플레이어의 선의 때문에 단순히 작동하지 않으며 단순히 재생되지 않습니까? –

답변

3

당신은 보비스에 PCM 데이터를 인코딩하는 libvorbis를 사용할 수 있습니다

이 내가 *의 .MP3 파일을 얻기 위해 사용하는 방법입니다.

+0

평균 mp3에 대한 ogg 파일을 가져 오는 데 얼마나 걸릴까요? mp3 노래가 약 3 분 길이라고 가정 해 봅시다. mp3 파일을 가져 오는 데 사용하는 방법은 때로는 최대 2 초가 걸립니다. 나는 지연에 대해 우려하고있다. 감사합니다. – bursyllac

+0

@SylBurlac 미디어 변환은 계산 상으로 비쌉니다. 백그라운드 스레드에서이 작업을 수행해야합니다. –

+0

백그라운드 스레드에서 해봤지만 재생 버튼을 눌렀을 때 변환이 시작됩니다.이 버튼을 사용하면 dlna를 통해 ogg 형식 만 지원하는 장치로 스트리밍합니다. 재생 버튼을 누르는 시간 간격에 대해 걱정하고 있습니다. 및 장치가 재생되기 시작할 때까지의 시간 간격. 답변 해 주셔서 감사합니다. – bursyllac