KUMAR.A
KUMAR.A

Reputation: 127

How do i save the m4a file picked from MPMediaPickerController as NSData?

I am new to iPhone development. I am developing an iPhone App. In that i have used MPMediaController for picking a song. Then i convert that file as NSData and uploading it to server. My code Working fine when "mp3" file is selected, but i am facing problem when i have picked "m4a" file. The file gets converted into data but after testing the result data by playing in AVAudioPlayer, it's not playing. Please give me a solution or suggest me where i am going wrong.

My code is:

-(IBAction)selectMusicButtonPressed:(id)sender
{

     MPMediaPickerController *mediaPicker = [[MPMediaPickerController alloc] initWithMediaTypes:MPMediaTypeMusic];

        mediaPicker.delegate = self;
        mediaPicker.allowsPickingMultipleItems = NO;

    [self presentModalViewController:mediaPicker animated:YES];
}


- (void)mediaPicker: (MPMediaPickerController *)mediaPicker didPickMediaItems:(MPMediaItemCollection *)mediaItemCollection 
{


   NSURL *url;
   NSMutableData *songData;


    MPMediaItemCollection *collection=mediaItemCollection;//[allAlbumsArray objectAtIndex:0];
    item = [collection representativeItem];
    song_name=[item valueForProperty:MPMediaItemPropertyTitle];


    NSURL *assetURL = [item valueForProperty:MPMediaItemPropertyAssetURL];
    NSString *title=[item valueForProperty:MPMediaItemPropertyTitle];


    if (!assetURL) {

        NSLog(@"%@ has DRM",title);

    }

    else{



        url = [item valueForProperty: MPMediaItemPropertyAssetURL];

       NSString* AssetURL = [NSString stringWithFormat:@"%@",[item valueForProperty:MPMediaItemPropertyAssetURL]];




            url_string=[url absoluteString];

            AVURLAsset *songAsset = [AVURLAsset URLAssetWithURL:url options:nil];

            NSError * error = nil;
            AVAssetReader * reader = [[AVAssetReader alloc] initWithAsset:songAsset error:&error];

            AVAssetTrack * songTrack = [songAsset.tracks objectAtIndex:0];

            AVAssetReaderTrackOutput * output = [[AVAssetReaderTrackOutput alloc] initWithTrack:songTrack outputSettings:nil];

            [reader addOutput:output];
            [output release];

            songData = [[NSMutableData alloc] init];

            [reader startReading];


            while (reader.status == AVAssetReaderStatusReading)
            {
                // AVAssetReaderTrackOutput method

                AVAssetReaderTrackOutput * trackOutput = (AVAssetReaderTrackOutput *)[reader.outputs objectAtIndex:0];
                CMSampleBufferRef sampleBufferRef = [trackOutput copyNextSampleBuffer];

                if (sampleBufferRef)
                {

                    CMBlockBufferRef blockBufferRef = CMSampleBufferGetDataBuffer(sampleBufferRef);
                    size_t length = CMBlockBufferGetDataLength(blockBufferRef);

                    NSLog(@"Size of the song----%zu",length);

                    UInt8 buffer[length];
                    CMBlockBufferCopyDataBytes(blockBufferRef, 0, length, buffer);
                    NSData *data = [[NSData alloc] initWithBytes:buffer length:length];
                    // NSLog(@"song length is %zu",length);
                    // NSLog(@"data is..........%@",data);
                    [songData appendData:data];
                    [data release];
                    CMSampleBufferInvalidate(sampleBufferRef);
                    CFRelease(sampleBufferRef);
                }
            }


            //Testing the result Data

     AVAudioPlayer * player = [[AVAudioPlayer alloc] initWithData:songData] error:NULL]; 

             [player play];

}

Upvotes: 1

Views: 1614

Answers (1)

Andre Cytryn
Andre Cytryn

Reputation: 2705

In my old project I used to convert m4a files to NSData like this:

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];

NSURL *soundFileURL = [NSURL fileURLWithPath:[NSString stringWithFormat:@"%@/memo.m4a", documentsDirectory]];;
NSData *myData = [NSData dataWithContentsOfURL:soundFileURL];

return myData;

Upvotes: 1

Related Questions