0

I'm trying to use the following code to save a snippet of audio from a url stream (e.g., a radio station broadcast):

float vocalStartMarker = 0.0;
    float vocalEndMarker = 20.0;

    NSURL *audioFileInput = [[NSURL alloc] initWithString: @"http://streamedaudio.mp3"];


    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsPath = [paths objectAtIndex:0]; //Get the docs directory
    NSString *filePath = [documentsPath stringByAppendingPathComponent:@"TestName.m4a"]; //Add the file name
    //[audioFileInput writeToFile:filePath atomically:YES]; //Write the file
    NSURL *audioFileOutput = [[NSURL alloc] initWithString: filePath];
    NSLog(@"%@", audioFileOutput);

    if (!audioFileInput || !audioFileOutput)
    {
        return NO;
    }

    [[NSFileManager defaultManager] removeItemAtURL:audioFileOutput error:NULL];
    AVAsset *asset = [AVAsset assetWithURL:audioFileInput];

    AVAssetExportSession *exportSession = [AVAssetExportSession exportSessionWithAsset:asset
                                                                            presetName:AVAssetExportPresetAppleM4A];

    if (exportSession == nil)
    {
        return NO;
    }

    CMTime startTime = CMTimeMake((int)(floor(vocalStartMarker * 100)), 100);
    CMTime stopTime = CMTimeMake((int)(ceil(vocalEndMarker * 100)), 100);
    CMTimeRange exportTimeRange = CMTimeRangeFromTimeToTime(startTime, stopTime);

    exportSession.outputURL = audioFileOutput;
    exportSession.outputFileType = AVFileTypeAppleM4A;
    exportSession.timeRange = exportTimeRange;

    [exportSession exportAsynchronouslyWithCompletionHandler:^
     {
         if (AVAssetExportSessionStatusCompleted == exportSession.status)
         {
             // It worked!
         }
         else if (AVAssetExportSessionStatusFailed == exportSession.status)
         {
                 NSLog(@"%@", exportSession.error);
             return;
         }
     }];

I get the following error:

2015-12-26 14:17:11.523 SaveURL[4135:24621] Error Domain=AVFoundationErrorDomain Code=-11838 "Operation Stopped" UserInfo={NSLocalizedDescription=Operation Stopped, NSLocalizedFailureReason=The operation is not supported for this media.}

Is this because i'm trying to convert an MP3 to M4A?

Thanks in advance for your help with this.

Baz
  • 41
  • 5

1 Answers1

0

Application Transport Security restricts requests to HTTPS unless you explicitly allow it.

paulvs
  • 11,963
  • 3
  • 41
  • 66
  • Ah! Didn't think of that -- i'll change the settings now. – Baz Dec 26 '15 at 14:55
  • Hi again Paulvs. I don't this is the issues: App Transport Security Settings -- Allow Arbitrary Loads is set to 'yes'. Any other ideas? – Baz Dec 26 '15 at 14:59