2017-07-28 117 views
0

我们可以使用从MPMediaPickerController获取的iTunes获取音频并上传到服务器吗?我已成功将录制的音频上传到服务器,没有任何问题。但从MPMediaPickerController收到资产网址时,我无法将其转换上传到服务器。将IOS音频上传到IOS的服务器

- (void)mediaPicker:(MPMediaPickerController *)mediaPicker didPickMediaItems:(MPMediaItemCollection *)mediaItemCollection 
{ 
    MPMediaItem *theChosenSong = [[mediaItemCollection items]objectAtIndex:0]; 
    NSString *songTitle = [theChosenSong valueForProperty:MPMediaItemPropertyTitle]; 
    NSLog(@"song Title: %@", songTitle); 
    NSURL *assetURL = [theChosenSong valueForProperty:MPMediaItemPropertyAssetURL]; 
    NSLog(@"assetURL: %@", assetURL); 
    AVURLAsset *songAsset = [AVURLAsset URLAssetWithURL:assetURL options:nil]; 
    NSURL *testURl = theChosenSong.assetURL; 
    [self serviceUploadAudio:testURl]; 
    [self dismissViewControllerAnimated:YES completion:nil]; 
} 

回答

0

MPMediaItemPropertyAssetURL是的iPod库的私人网址是指向基于URL的AVFoundation对象,所以你不能直接上传这个网址对象到服务器。 您必须先将此MPMediaItem导出到文档目录或临时目录,然后上传到服务器。

在斯威夫特3

// MARK:- Convert itunes song to avsset in temp location Methode. 
    func exportiTunesSong(assetURL: URL, completionHandler: @escaping (_ fileURL: URL?) ->()) { 

     let songAsset = AVURLAsset(url: assetURL, options: nil) 

     let exporter = AVAssetExportSession(asset: songAsset, presetName: AVAssetExportPresetAppleM4A) 

     exporter?.outputFileType = "com.apple.m4a-audio" 

     exporter?.metadata = songAsset.commonMetadata 

     let filename = AVMetadataItem.metadataItems(from: songAsset.commonMetadata, withKey: AVMetadataCommonKeyTitle, keySpace: AVMetadataKeySpaceCommon) 


     var songName = "Unknown" 

     if filename.count > 0 { 
      songName = ((filename[0] as AVMetadataItem).value?.copy(with: nil) as? String)! 
     } 

     //Export mediaItem to temp directory 
     let exportURL = URL(fileURLWithPath: NSTemporaryDirectory()) 
      .appendingPathComponent(songName) 
      .appendingPathExtension("m4a") 

     exporter?.outputURL = exportURL 

     // do the export 
     // (completion handler block omitted) 

     exporter?.exportAsynchronously(completionHandler: { 
      let exportStatus = exporter!.status 

      switch (exportStatus) { 
      case .failed: 
        let exportError = exporter?.error 
        print("AVAssetExportSessionStatusFailed: \(exportError)") 
        completionHandler(nil) 
        break 
      case .completed: 
        print("AVAssetExportSessionStatusCompleted") 
        completionHandler(exportURL) 
        break 
      case .unknown: 
        print("AVAssetExportSessionStatusUnknown") 
       break 
      case .exporting: 
        print("AVAssetExportSessionStatusExporting") 
       break 
      case .cancelled: 
        print("AVAssetExportSessionStatusCancelled") 
        completionHandler(nil) 
       break 
      case .waiting: 
        print("AVAssetExportSessionStatusWaiting") 
       break 
      } 
     }) 
    } 

这个函数返回的出口MPMediaItem临时目录的网址,你可以上传到服务器。

self.exportiTunesSong(assetURL: index as! URL, completionHandler: { (filePath) in 
     if (filePath != nil) { 

      //Upload mediaItem to server by filePath 
      [self serviceUploadAudio:filePath!]; 
     } 
}) 

在Objective - C

#pragma mark Convert itunes song to avsset in temp location Methode. 
-(void)exportiTunesSong:(NSURL*)assetURL fileURL:(void(^)(id))completionHandler { 

    AVURLAsset *songAsset = [AVURLAsset URLAssetWithURL:assetURL options:nil]; 
    AVAssetExportSession *exporter = [AVAssetExportSession exportSessionWithAsset:songAsset presetName:AVAssetExportPresetAppleM4A]; 

    exporter.outputFileType = @"com.apple.m4a-audio"; 

    exporter.metadata = songAsset.commonMetadata; 

    NSArray *filename = [AVMetadataItem metadataItemsFromArray:songAsset.commonMetadata withKey:AVMetadataCommonKeyTitle keySpace:AVMetadataKeySpaceCommon]; 


    NSString *songName = @"Unknown"; 

    if (filename.count > 0) { 
     AVMetadataItem *title = [filename objectAtIndex:0]; 
     songName = [title.value copyWithZone:nil]; 
    } 

    NSURL *exportURL = [[[NSURL fileURLWithPath:NSTemporaryDirectory()] URLByAppendingPathComponent:songName] URLByAppendingPathExtension:@"m4a"]; 

    exporter.outputURL = exportURL; 

    // do the export 
    // (completion handler block omitted) 

    [exporter exportAsynchronouslyWithCompletionHandler:^{ 

     switch (exporter.status) { 

      case AVAssetExportSessionStatusFailed: 
       NSLog(@"AVAssetExportSessionStatusFailed: %@",exporter.error); 
       completionHandler(nil); 
       break; 

      case AVAssetExportSessionStatusCompleted: 
       NSLog(@"AVAssetExportSessionStatusCompleted"); 
       completionHandler(exportURL); 
       break; 

      case AVAssetExportSessionStatusUnknown: 
       NSLog(@"AVAssetExportSessionStatusUnknown"); 
       break; 

      case AVAssetExportSessionStatusExporting: 
       NSLog(@"AVAssetExportSessionStatusExporting"); 
       break; 

      case AVAssetExportSessionStatusCancelled: 
       NSLog(@"AVAssetExportSessionStatusCancelled: %@",exporter.error); 
       completionHandler(nil); 
       break; 

      case AVAssetExportSessionStatusWaiting: 
       NSLog(@"AVAssetExportSessionStatusWaiting"); 
       break; 

     } 
    }]; 
} 

[self exportiTunesSong:assetURL fileURL:^(id filepath) { 
     if (filepath != nil) { 
      [self serviceUploadAudio:filepath]; 
     } 
    }];