2015-12-26 38 views
0

我嘗試使用下面的代碼保存音頻的片斷從URL流(例如,一個電臺廣播):目標C/IOS - AVAsset - 錯誤

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

我得到以下錯誤:

2015-12-26 14:17:11.523 SaveURL [4135:24621] Error Domain = AVFoundationErrorDomain Code = -11838「Operation Stopped」UserInfo = {NSLocalizedDescription = Operation停止,NSLocalizedFailureReason =該操作不支持這個媒體。}

這是因爲我試圖轉換MP3到M4A?

在此先感謝您的幫助。

回答

0

應用程序傳輸安全性將請求限制爲HTTPS,除非您明確允許。

+0

啊!沒想到 - 我現在要改變設置。 – Baz

+0

嗨保羅夫。我不是這樣的問題:應用程序傳輸安全設置 - 允許任意負載設置爲'是'。任何其他想法? – Baz

+0

好的,你的實際URL是什麼? http://streamedaudio.mp3似乎不是有效的。 – paulvs