AVAssetReader/Writer only working on certain devices?
===

I was using [this](http://www.subfurther.com/blog/2010/12/13/from-ipod-library-to-pcm-samples-in-far-fewer-steps-than-were-previously-necessary/) tutorial to get a song from the music library to my specified directory.  It works fine for me, but other people are reporting that the song   never gets there. At first I thought there were device limitations, but it seems like people with the same device as me are having the problem. It is sort of annoying because I can't recreate their problem. 

Here is my code:

    {
    	NSURL *assetURL = [song valueForProperty:MPMediaItemPropertyAssetURL];
        AVURLAsset *songAsset = [AVURLAsset URLAssetWithURL:assetURL options:nil];
        
        NSError *assetError = nil;
        AVAssetReader *assetReader = [[AVAssetReader assetReaderWithAsset:songAsset
                                                                    error:&assetError]
                                      retain];
        if (assetError) {
            NSLog (@"error: %@", assetError);
            return;
        }
        
        AVAssetReaderOutput *assetReaderOutput = [[AVAssetReaderAudioMixOutput 
                                                   assetReaderAudioMixOutputWithAudioTracks:songAsset.tracks
                                                   audioSettings: nil]
                                                  retain];
        if (! [assetReader canAddOutput: assetReaderOutput]) {
            NSLog (@"can't add reader output... die!");
            return;
        }
        [assetReader addOutput: assetReaderOutput];
        
        
        NSString *exportPath = @"Library/PreferenceBundles/MyAlarmSettings.bundle/export.m4a";
        if ([[NSFileManager defaultManager] fileExistsAtPath:exportPath]) {
    		[[NSFileManager defaultManager] removeItemAtPath:exportPath error:nil];
    	}
    
        NSURL *exportURL = [NSURL fileURLWithPath:exportPath];
        AVAssetWriter *assetWriter = [[AVAssetWriter assetWriterWithURL:exportURL
                                                               fileType:AVFileTypeAppleM4A
                                                                  error:&assetError]
                                      retain];
        if (assetError) {
            NSLog (@"error: %@", assetError);
            return;
        }
        AudioChannelLayout channelLayout;
        memset(&channelLayout, 0, sizeof(AudioChannelLayout));
        channelLayout.mChannelLayoutTag = kAudioChannelLayoutTag_Stereo;
        
        NSDictionary *outputSettings = [NSDictionary dictionaryWithObjectsAndKeys:
                                        [ NSNumber numberWithInt: kAudioFormatMPEG4AAC], AVFormatIDKey,
                                        [ NSNumber numberWithInt: 2 ], AVNumberOfChannelsKey,
                                        [ NSNumber numberWithFloat: 44100.0 ], AVSampleRateKey,
                                        [ NSData dataWithBytes:&channelLayout length: sizeof( AudioChannelLayout ) ], AVChannelLayoutKey,
                                        [ NSNumber numberWithInt: 320000 ], AVEncoderBitRateKey,
                                        nil];                
                                              
        
        AVAssetWriterInput *assetWriterInput = [[AVAssetWriterInput assetWriterInputWithMediaType:AVMediaTypeAudio
                                                                                   outputSettings:outputSettings]
                                                retain];
        if ([assetWriter canAddInput:assetWriterInput]) {
            [assetWriter addInput:assetWriterInput];
        } else {
            NSLog (@"can't add asset writer input... die!");
            return;
        }
        
        assetWriterInput.expectsMediaDataInRealTime = NO;
        
        [assetWriter startWriting];
        [assetReader startReading];
        
        AVAssetTrack *soundTrack = [songAsset.tracks objectAtIndex:0];
        CMTime startTime = CMTimeMake (0, soundTrack.naturalTimeScale);
        [assetWriter startSessionAtSourceTime: startTime];
        
        __block UInt64 convertedByteCount = 0;
        
        dispatch_queue_t mediaInputQueue = dispatch_queue_create("mediaInputQueue", NULL);
        [assetWriterInput requestMediaDataWhenReadyOnQueue:mediaInputQueue 
                                                usingBlock: ^ 
         {
             // NSLog (@"top of block");
             while (assetWriterInput.readyForMoreMediaData) {
                 CMSampleBufferRef nextBuffer = [assetReaderOutput copyNextSampleBuffer];
                 if (nextBuffer) {
                     // append buffer
                     [assetWriterInput appendSampleBuffer: nextBuffer];
                     //				NSLog (@"appended a buffer (%d bytes)", 
                     //					   CMSampleBufferGetTotalSampleSize (nextBuffer));
                     convertedByteCount += CMSampleBufferGetTotalSampleSize (nextBuffer);
                     // oops, no
                     // sizeLabel.text = [NSString stringWithFormat: @"%ld bytes converted", convertedByteCount];
                     
                     NSNumber *convertedByteCountNumber = [NSNumber numberWithLong:convertedByteCount];
                     [self performSelectorOnMainThread:@selector(updateSizeLabel:)
                                            withObject:convertedByteCountNumber
                                         waitUntilDone:NO];
                 } else {
                     // done!
                                
                     NSString *songtitle =  [[NSString alloc]initWithFormat:@"Alarm sound set to '%@' \n \n Press OK to respring. ",[song valueForProperty:MPMediaItemPropertyTitle]];
                     
                     UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Done!" message:songtitle delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
                     [alert show];
                     [alert release];
                     
                     
                     
                     [assetWriterInput markAsFinished];
                     [assetWriter finishWriting];
                     [assetReader cancelReading];
                     NSDictionary *outputFileAttributes = [[NSFileManager defaultManager]
                                                           attributesOfItemAtPath:exportPath
                                                           error:nil];
                     NSLog (@"done. file size is %llu",
                            [outputFileAttributes fileSize]);
                     NSNumber *doneFileSize = [NSNumber numberWithLong:[outputFileAttributes fileSize]];
                     [self performSelectorOnMainThread:@selector(updateCompletedSizeLabel:)
                                            withObject:doneFileSize
                                         waitUntilDone:NO];
                     // release a lot of stuff
                     
                     
                     [assetReader release];
                     [assetReaderOutput release];
                     [assetWriter release];
                     [assetWriterInput release];
                     [exportPath release];
                     break;
                 }
             }
             
         }];
        
        NSLog (@"bottom of convertTapped:");
    }

