Question

i found (customizing a method i found) a way to convert a MpMediaItem and to obtain a mp3 file. Probably it's not the best way but it's working.

Is there a way to obtain an *.ogg file instead of *.mp3 ? What would be the proper approach?

This is the method i use to get the *.mp3 file:

    -(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];
}
Was it helpful?

Solution

You can use libvorbis to encode PCM data to Vorbis.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top