IOS request download songs for pictures via web

Source: Internet
Author: User

1. Import Agent <nsurlconnectiondatadelegate>

1 @interfaceViewcontroller () <NSURLConnectionDataDelegate>2 {3     Long LongAlllength;//Download Total length4     Long LongCurrlenth;//download length for current period5 }6 //storage of downloaded MP3 data streams7@property (nonatomic,strong) Nsmutabledata *msicdate;8 //Download Progress Display9@property (nonatomic,strong) Uiprogressview *msicprogress;Ten //Download Button One@property (nonatomic,strong) UIButton *But ; A //Music Playback -@property (Nonatomic,strong) Avaudioplayer *player;
1 //Add download image address2NSString *path=@"http://pic1.nipic.com/2008-10-27/200810278105878_2.jpg";3     4     //Call the download picture method5 [self addtheimg:path];6     7     //Progress bar Settings8Self.msicprogress =[[uiprogressview Alloc]initwithframe:cgrectmake ( -, +, the,Ten)];9self.msicprogress.progress=0;Ten     //set the download progress color OneSelf.msicprogress.progresstintcolor=[uicolor colorwithred:0.007Green0.868Blue0.000Alpha1.000]; A     //progress bar Background color -Self.msicprogress.tracktintcolor=[Uicolor Graycolor]; -      the     //download button click to download -self.but=[UIButton Buttonwithtype:uibuttontypecustom]; -_but.frame=cgrectmake ( $, -, +, -); - [_but settitlecolor:[uicolor whitecolor]forstate:uicontrolstatenormal]; +[_but Settitle:@"Download"Forstate:uicontrolstatenormal]; -_but.backgroundcolor=[Uicolor Orangecolor]; + [_but addtarget:self Action: @selector (Plarmusic:) forcontrolevents:uicontroleventtouchupinside]; A      at [Self.view addsubview:_msicprogress]; -[Self.view addsubview:_but];

Implementation of the method

1 //button click Time corresponding method2-(void) Plarmusic: (UIButton *) Sender3 {4     //total length of the song download5Alllength=0;6     //The amount currently acquired7Currlenth =0;8Self.msicdate =[[Nsmutabledata alloc]init];9     //set the address of the song downloadTenNSString *path=@"http://yinyueshiting.baidu.com/data2/music/124345627/124104509190800128.mp3?xcode= CB12E87F8333E5370D9B8F0C677C76D2"; OneNsurl *url=[Nsurl Urlwithstring:path]; A     //Mode 1 Network request -Nsurlrequest *requst=[Nsurlrequest Requestwithurl:url]; -     //Mode 2 the     //nsurlrequest *resq=[[nsurlrequest Alloc]initwithurl:url cachepolicy:nsurlrequestuseprotocolcachepolicy timeoutinterval:10.0]; -      -     //Create a connection server with a proxy request -[Nsurlconnection Connectionwithrequest:requstDelegate: self]; +}

1 //Implementing the Network Request Proxy method2-(void) Connection: (Nsurlconnection *) connection didreceiveresponse: (Nsurlresponse *) Response3 {4     //get the length of the MP3 you want to download, and receive a response to know that it is not finished downloading5     //This proxy method executes only once to get the total amount of data6Alllength =[Response expectedcontentlength];7 }8 9 //gets the amount of data that is downloaded each time this method executes multiple timesTen-(void) Connection: (Nsurlconnection *) connection didreceivedata: (NSData *) Data One { A     //put data for each download in the data area - [Self.msicdate Appenddata:data]; -     //append the length of the acquired data to the current length theCurrlenth + =data.length; -     //Update progress bar -Self.msicprogress.progress= (float) currlenth/alllength; - } +  - //download End call, play music +-(void) Connectiondidfinishloading: (Nsurlconnection *) Connection A { atNserror *error=Nil; -     //after the download is complete, initialize the player and then play the song -Self.player = [[Avaudioplayer alloc]initwithdata:self.msicdate error:&ERROR]; - [Self.player play]; - } -  in //Request Error Call --(void) Connection: (Nsurlconnection *) connection didfailwitherror: (Nserror *) Error to { +     if(Error) { -NSLog (@"error = =%@", error); the     } *}
1 //methods for calling pictures2-(void) Addtheimg: (NSString *) path3 {4     //1. Create a URL from a string5NSString *imgstr=path;6Nsurl *imgurl=[Nsurl Urlwithstring:imgstr];7     8     //2. Create a network request9Nsurlrequest *requst=[nsurlrequest Requestwithurl:imgurl Cachepolicy:nsurlrequestuseprotocolcachepolicy timeOutInterval:10.0];Ten      OneNserror *error=Nil; A     //3. Linked Servers -NSData *imgdate = [nsurlconnection sendsynchronousrequest:requst returningresponse:nil error:&ERROR]; -     if(Error) the     { -         //if error, print error -NSLog (@"%@", error); -}Else +     { -         //If a picture is found, create a picture view to receive the picture +Uiimageview *img=[[uiimageview Alloc]initwithframe:cgrectmake (5, -, -, -)]; AImg.image=[UIImage imagewithdata:imgdate]; at         //display a picture in a view - [Self.view addsubview:img]; -     } -  -}

IOS request download songs for pictures via web

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.