【问题标题】:objective c - convert mpmediaitem to ogg vorbis目标 c - 将 mpmediaitem 转换为 ogg vorbis
【发布时间】:2012-09-21 08:21:02
【问题描述】:

我找到(自定义我找到的方法)一种转换 MpMediaItem 并获取 mp3 文件的方法。可能这不是最好的方法,但它确实有效。

有没有办法获得 *.ogg 文件而不是 *.mp3 ?什么是正确的方法?

这是我用来获取 *.mp3 文件的方法:

    -(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];
}

【问题讨论】:

  • 如果您的 MP3 编码代码正常工作,这将是一个重大突破,因为普遍接受的事实是,如果没有像 lame 这样的第三方库,就不可能将音频编码到 MP3 ios中的格式。您确定这可以正常工作,并且不只是因为音频播放器的善意而播放,它接受 mp3 文件中的其他格式吗?

标签: objective-c ios xcode ogg mpmediaitem


【解决方案1】:

您可以使用libvorbis 将 PCM 数据编码为 Vorbis。

【讨论】:

  • 获取普通 mp3 的 ogg 文件需要多长时间?假设 mp3 歌曲的长度约为 3 分钟。我用来获取 mp3 文件的方法有时最多需要 2 秒。我担心滞后。谢谢
  • @SylBurlac 媒体转换的计算成本很高。您应该在后台线程中执行此操作。
  • 我是在后台线程中完成的,但是当我点击播放按钮时转换开始(我使用它通过 dlna 流式传输到仅支持 ogg 格式的设备),我担心点击播放按钮之间的时间间隔和设备开始播放之前的时间间隔。非常感谢您的回答
猜你喜欢
  • 2020-11-15
  • 2011-05-19
  • 2013-12-14
  • 2014-12-28
  • 2011-09-01
  • 2011-07-08
  • 2011-07-10
  • 2011-06-12
  • 2011-02-08
相关资源
最近更新 更多