objective c - How to retrieve Downloaded content in iOS? -
in app i've downloaded mp3 file , stored giving file name write abc.mp3, while storing file path shown this:
/var/mobile/containers/data/application/9afc500a-91d5-421b-8a5f-19a239456353/documents/abc.mp3
and when play path is
file:///var/mobile/containers/data/application/9afc500a-91d5-421b-8a5f-19a239456353/documents/abc.mp3
the code play is:
nsurl* songurl = [nsurl fileurlwithpath:songurlstring]; mediaplayer = [[avplayer alloc]initwithurl:url ]; //this avplayer instance [mediaplayer play];
the code download is:
-
(void)downloadcontent:(nsstring*)stringurl{ stringurl = [stringurl stringbyreplacingoccurrencesofstring:@" " withstring:@"%20"]; currenturl = stringurl; nsurl *url = [nsurl urlwithstring:stringurl]; nsurlrequest *therequest = [nsurlrequest requestwithurl:url cachepolicy:nsurlrequestreloadignoringlocalcachedata timeoutinterval:60]; receiveddata = [[nsmutabledata alloc] initwithlength:0]; nsurlconnection * connection = [[nsurlconnection alloc] initwithrequest:therequest delegate:self startimmediately:yes]; } - (void) connection:(nsurlconnection *)connection didreceiveresponse:(nsurlresponse *)response { [uiapplication sharedapplication].networkactivityindicatorvisible = yes; progress.hidden = no; [receiveddata setlength:0]; expectedbytes = [response expectedcontentlength]; } - (void) connection:(nsurlconnection *)connection didreceivedata:(nsdata *)data { [receiveddata appenddata:data]; float progressive = (float)[receiveddata length] / (float)expectedbytes; progress.value = progressive; nslog(@"%f",progressive); } - (void) connection:(nsurlconnection *)connection didfailwitherror:(nserror *)error { [uiapplication sharedapplication].networkactivityindicatorvisible = no; } - (nscachedurlresponse *) connection:(nsurlconnection *)connection willcacheresponse: (nscachedurlresponse *)cachedresponse { return nil; } - (void) connectiondidfinishloading:(nsurlconnection *)connection { nsarray *paths = nssearchpathfordirectoriesindomains(nsdocumentdirectory, nsuserdomainmask, yes); nsstring *documentsdirectory = [paths objectatindex:0]; nslog(@"%@",documentsdirectory); nsstring *pdfpath = [documentsdirectory stringbyappendingpathcomponent:@"abc.mp3"]; nslog(@"succeeded! received %lu bytes of data",(unsigned long)[receiveddata length]); [uiapplication sharedapplication].networkactivityindicatorvisible = no; [receiveddata writetofile:pdfpath atomically:yes]; progress.hidden = yes; }
so problem not playing content. don't know whether content written file or not.
you "given" arbitrary documents folder system (that nsdocumentdirectory
) can't guarantee it's full exact path. solve this, should, "build" path every time need file it, same way did when wrote file:
//build path nsarray *paths = nssearchpathfordirectoriesindomains(nsdocumentdirectory, nsuserdomainmask, yes); nsstring *documentsdirectory = [paths objectatindex:0]; nsstring *path = [documentsdirectory stringbyappendingpathcomponent:@"abc.mp3"]; //then can do: nsurl* songurl = [nsurl fileurlwithpath: path]; mediaplayer = [[avplayer alloc]initwithurl:url ]; [mediaplayer play];
note used code use build path when downloading file.
Comments
Post a Comment