如何从iPod库中复制歌曲到应用程序并使用AVAudioPlayer播放?

3

虽然重复了,但我想使用AVAudioPlayer从iPod Library播放歌曲。 所以,在将iPod Library歌曲复制到应用程序后,是否可以复制它?

我有一个代码,可以将歌曲转换为caf格式,然后我就可以使用AVAudioPlayer播放。 但转换需要太长时间。 所以我还有其他方法吗?

2个回答

6
以下代码将从其URL读取iPod库歌曲并复制到磁盘供使用。 请注意,此代码不适用于m4a文件,因为iPod库中m4a的URL不包含头文件,您需要使用AVAssetExporter将头文件和音乐数据写入磁盘。
 -(void)exportMP3:(NSURL*)url toFileUrl:(NSString*)fileURL
    {
        AVURLAsset *asset=[[[AVURLAsset alloc] initWithURL:url options:nil] autorelease];
        AVAssetReader *reader=[[[AVAssetReader alloc] initWithAsset:asset error:nil] autorelease];
        NSMutableArray *myOutputs =[[[NSMutableArray alloc] init] autorelease];
        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] autorelease] attributes:nil];
        }
        fileHandle=[NSFileHandle fileHandleForUpdatingAtPath:fileURL];    
        [fileHandle seekToEndOfFile];

        AVAssetReaderOutput *output=[myOutputs objectAtIndex:0];
         int totalBuff=0;
        while(TRUE)
        {
             CMSampleBufferRef ref=[output copyNextSampleBuffer];
            if(ref==NULL)
                break;
            //copy data to file
            //read next one
            AudioBufferList audioBufferList;
            NSMutableData *data=[[NSMutableData alloc] init];
            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];
                Float32 *frame = audioBuffer.mData;


                //  Float32 currentSample = frame[i];
                [data appendBytes:frame length:audioBuffer.mDataByteSize];

              //  written= fwrite(frame, sizeof(Float32), audioBuffer.mDataByteSize, f);
                ////NSLog(@"Wrote %d", written);

            }
            totalBuff++;
            CFRelease(blockBuffer);
            CFRelease(ref);
            [fileHandle writeData:data];
          //  //NSLog(@"writting %d frame for amounts of buffers %d ", data.length, audioBufferList.mNumberBuffers);
            [data release];
        }
      //  //NSLog(@"total buffs %d", totalBuff);
    //    fclose(f);
        [fileHandle closeFile];



    }

有关使用AVAssetExporter修复m4a文件头的任何提示? - bendytree
生成的 MP3 文件没有显示专辑封面,请帮忙解决。 - Fahad Jamal
@Daniel,你有没有想法,如何将专辑封面添加到生成的MP3文件中? - Fahad Jamal
@James,请尝试这段代码,我已经在我的博客上发布了它,链接在这里http://iphone.sanniv.com/2015/03/22/add-album-art-to-mp3-files-in-ios/。 - Sanniv

-2

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接