iphone - Save the The Stream From URL -
in project connecting url , watching video mpmovieplayerviewcontroller. not enough me. want save file iphone. have buttons. 1 watch video, other save video. when push button watch watchng it. unable save it. want able watch video later. in view want see saved videos etc. there 1 can me or can show way ? have tried following code phrase when code started, works while (probably download time), when time save bad exc_bad_access error .thanks every one.
here code .
cfstringref *docsdirectory = (cfstringref)[nstemporarydirectory() stringbyappendingpathcomponent: @"recordedfile.mp4"]; nsstring *tempath=nstemporarydirectory(); nsstring *tempfile=[tempath stringbyappendingpathcomponent:@"recode.mp4"]; nslog(@" dosya adi madi %@",docsdirectory); nsfilemanager *filemanager = [nsfilemanager defaultmanager]; nserror *error = [[nserror alloc] init]; [filemanager removeitematpath:docsdirectory error:&error]; nsurl *url = [nsurl urlwithstring:@"http://video.teknomart.com.tr/3-13-2.mp4"]; nsmutableurlrequest *liverequest = [[nsmutableurlrequest alloc] initwithurl:url]; [liverequest setcachepolicy:nsurlrequestreloadignoringcachedata]; [liverequest setvalue:@"headervalue" forhttpheaderfield:@"headerfield"]; nsurlresponse *response; nsdata *mydata = [nsurlconnection sendsynchronousrequest:liverequest returningresponse:&response error:&error]; nsdata *mydata2 = [nsdata datawithcontentsofurl:[nsurl urlwithstring:@"http://video.teknomart.com.tr/3-13-2.mp4"]]; nsstring *mystring=[[nsstring alloc] initwithdata:mydata encoding:nsasciistringencoding]; nslog(@"gelen sey %@",mystring); [mystring writetofile:tempfile writetofile:tempfile automatically:yes encoding:nsasciistringencoding error:nil]; [mystring release]; return true;
-(void)viewdidload { nsarray *paths = nssearchpathfordirectoriesindomains(nsdocumentdirectory, nsuserdomainmask, yes); nsstring *documentsdirectory = [paths objectatindex:0]; if (!documentsdirectory) { nslog(@"documents directory not found!"); } nsarray *mywords = [songnamestring componentsseparatedbycharactersinset:[nscharacterset charactersetwithcharactersinstring:@"/"]]; nsstring *appfile = [documentsdirectory stringbyappendingpathcomponent:[mywords lastobject]]; nslog(@"%@",[mywords lastobject]); nsurl *url = [nsurl fileurlwithpath:appfile]; nslog(@"%@",url); [[uiapplication sharedapplication] setstatusbarhidden:yes]; self.navigationcontroller.navigationbarhidden=yes; currenttimer=[nstimer scheduledtimerwithtimeinterval:1 target:self selector:@selector(showcurrenttime) userinfo:nil repeats:yes]; alarmtimelabel.text =alarmtimestring; alarmsonglabel.text = [mywords lastobject] ; [self performselector:@selector(loadvideoinbackground)]; //[nsthread detachnewthreadselector:@selector(loadvideoinbackground) totarget:self withobject:nil]; } -(void)loadvideoinbackground { nsarray *paths = nssearchpathfordirectoriesindomains(nsdocumentdirectory, nsuserdomainmask, yes); nsstring *documentsdirectory = [paths objectatindex:0]; if (!documentsdirectory) { nslog(@"documents directory not found!"); } nsstring *appfile; nsarray *mywords = [songnamestring componentsseparatedbycharactersinset:[nscharacterset charactersetwithcharactersinstring:@"/"]]; appfile = [documentsdirectory stringbyappendingpathcomponent:[mywords lastobject]]; nsfilemanager *filemgr=[nsfilemanager defaultmanager]; if (![filemgr fileexistsatpath:appfile]) { alarmcanplay = false; nsurl *imageurl = [[[nsurl alloc] initwithstring:songnamestring]autorelease]; nsurlrequest *imagerequest = [nsurlrequest requestwithurl:imageurl cachepolicy:nsurlrequestreturncachedataelseload timeoutinterval:120.0]; imageconnection = [[nsurlconnection alloc] initwithrequest:imagerequest delegate:self]; if(imageconnection) { videodata = [[nsmutabledata data] retain]; } } } - (void)connection:(nsurlconnection *)connection didreceiveresponse:(nsurlresponse *)response { // method called when server has determined // has enough information create nsurlresponse // can called multiple times, example in case of // redirect, each time reset data. // receiveddata declared method instance elsewhere [videodata setlength:0]; } - (void)connection:(nsurlconnection *)connection didreceivedata:(nsdata *)data { // append new data receiveddata // receiveddata declared method instance elsewhere //nslog(@"%d",[data size]); [videodata appenddata:data]; } - (void)connection:(nsurlconnection *)connection didfailwitherror:(nserror *)error { // release data object [videodata release]; // inform user nslog(@"connection failed! error - %@ %@", [error localizeddescription], [[error userinfo] objectforkey:nserrorfailingurlstringkey]); //workinprogress = no; } - (void)connectiondidfinishloading:(nsurlconnection *)connection { nsarray *paths = nssearchpathfordirectoriesindomains(nsdocumentdirectory, nsuserdomainmask, yes); nsstring *documentsdirectory = [paths objectatindex:0]; if (!documentsdirectory) { nslog(@"documents directory not found!"); } nsstring *appfile; nsarray *mywords = [songnamestring componentsseparatedbycharactersinset:[nscharacterset charactersetwithcharactersinstring:@"/"]]; appfile = [documentsdirectory stringbyappendingpathcomponent:[mywords lastobject]]; [videodata writetofile:appfile atomically:yes]; alarmcanplay = true; }
Comments
Post a Comment