objective c - Uploading itunes audio to server in IOS -


can use audio itunes fetched using mpmediapickercontroller , upload server? have uploaded recorded audio server without issues. on receiving asset url mpmediapickercontroller, couldn't convert upload server.

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

mpmediaitempropertyasseturl private url of ipod-library point url-based avfoundation object, can't directly upload url object server. have first export mpmediaitem document directory or temp directory , upload server.

in swift 3

// mark:- convert itunes song 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] avmetadataitem).value?.copy(with: nil) as? string)!         }          //export mediaitem temp directory         let exporturl = url(fileurlwithpath: nstemporarydirectory())             .appendingpathcomponent(songname)             .appendingpathextension("m4a")          exporter?.outputurl = exporturl          // 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             }         })     } 

this function return temp directory url of exported mpmediaitem, can upload server.

self.exportitunessong(asseturl: index as! url, completionhandler: { (filepath) in         if (filepath != nil) {             //upload mediaitem server filepath            [self serviceuploadaudio:filepath!];        } }) 

in objective - c

#pragma mark convert itunes song 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;      // 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];         }     }]; 

Comments

Popular posts from this blog

php - Vagrant up error - Uncaught Reflection Exception: Class DOMDocument does not exist -

vue.js - Create hooks for automated testing -

Add new key value to json node in java -