3

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?

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?

Mick MacCallum
  • 129,200
  • 40
  • 280
  • 281
DShah
  • 9,768
  • 11
  • 71
  • 127

2 Answers2

6

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



    }
Daniel
  • 22,363
  • 9
  • 64
  • 71
-2

You could just use the MPMusicPlayerController, which is built for including an iPod interface within an app. See the docs: http://developer.apple.com/library/ios/#documentation/mediaplayer/reference/MPMusicPlayerController_ClassReference/Reference/Reference.html.

If you really need to use the MPMediaPickerController (which is what I am assuming you are using to select songs from the users library), see this post for how to playback the songs: Using MPMediaItems with AVAudioPlayer

Community
  • 1
  • 1
Michael Frederick
  • 16,664
  • 3
  • 43
  • 58