如何从iPod的歌曲库复制到应用程序,并用AVAudioPlayer玩吗? [英] How to copy songs from iPod Library to app and play with AVAudioPlayer?

查看:176
本文介绍了如何从iPod的歌曲库复制到应用程序,并用AVAudioPlayer玩吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

虽然是重复的,但我想用 AVAudioPlayer 从图书馆的iPod播放歌曲。因此,是否有可能将其拷贝到应用程序后,图书馆的iPod歌曲复制?

Though a duplicate but i want to play song using AVAudioPlayer from iPod Library. So Is it possible to copy iPod Library songs after copying it to app?

我有一个code,乐曲转换为CAF格式,然后我能够用播放 AVAudioPlayer 。但是转换需要很长的时间。那么,有没有其他方法可以让我做到这一点?

I have one code which converts the song to caf format and then I am able to play using AVAudioPlayer. But Conversion takes too long time. So is there any other way I can do that?

推荐答案

以下code将读取它的URL iPod的歌曲库,并将其复制到磁盘使用..
注意,这code不会与m4as工作,因为从iPod库中的URL进行m4as不包含头文件,为您将需要使用AVAssetExporter到标题和音乐数据写入磁盘。

The following code will read the ipod library song from its url and copy it to disk for use.. note this code wont work with m4as because the URLs from ipod library for m4as dont contains headers, for that you will need to use AVAssetExporter to write the header and music data to disk.

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



    }

这篇关于如何从iPod的歌曲库复制到应用程序,并用AVAudioPlayer玩吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆