標籤:
1.匯入代理<NSURLConnectionDataDelegate>
1 @interface ViewController ()<NSURLConnectionDataDelegate> 2 { 3 long long alllength; //下載總長度 4 long long currlenth; //當期下載長度 5 } 6 //存放下載的mp3 資料流 7 @property(nonatomic,strong)NSMutableData *msicDate; 8 //下載進度顯示 9 @property(nonatomic,strong)UIProgressView *msicProgress;10 //下載按鈕11 @property(nonatomic,strong)UIButton *but;12 //音樂播放13 @property(nonatomic,strong)AVAudioPlayer *player;
1 //添加下載圖片地址 2 NSString *path=@"http://pic1.nipic.com/2008-10-27/200810278105878_2.jpg"; 3 4 //調用下載圖片方法 5 [self addTheimg:path]; 6 7 //進度條設定 8 self.msicProgress =[[UIProgressView alloc]initWithFrame:CGRectMake(60, 40, 230, 10)]; 9 self.msicProgress.progress=0;10 //設定下載進度顏色11 self.msicProgress.progressTintColor=[UIColor colorWithRed:0.007 green:0.868 blue:0.000 alpha:1.000];12 //進度條背景顏色13 self.msicProgress.trackTintColor=[UIColor grayColor];14 15 //下載按鈕點擊下載16 self.but=[UIButton buttonWithType:UIButtonTypeCustom];17 _but.frame=CGRectMake(140, 55, 40, 20);18 [_but setTitleColor:[UIColor whiteColor]forState:UIControlStateNormal];19 [_but setTitle:@"下載" forState:UIControlStateNormal];20 _but.backgroundColor=[UIColor orangeColor];21 [_but addTarget:self action:@selector(plarmusic:) forControlEvents:UIControlEventTouchUpInside];22 23 [self.view addSubview:_msicProgress];24 [self.view addSubview:_but];
方法的實現
1 //按鈕點擊時間相應方法 2 -(void)plarmusic:(UIButton *)sender 3 { 4 //歌曲下載總的長度 5 alllength=0; 6 //當前擷取的量 7 currlenth = 0; 8 self.msicDate =[[NSMutableData alloc]init]; 9 //設定歌曲下載的地址10 NSString *path=@"http://yinyueshiting.baidu.com/data2/music/124345627/124104509190800128.mp3?xcode=cb12e87f8333e5370d9b8f0c677c76d2";11 NSURL *url=[NSURL URLWithString:path];12 //方式1 網路請求13 NSURLRequest *requst=[NSURLRequest requestWithURL:url];14 //方式215 //NSURLRequest *resq=[[NSURLRequest alloc]initWithURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:10.0];16 17 //使用代理進行請求 建立串連伺服器18 [NSURLConnection connectionWithRequest:requst delegate:self];19 }
1 //實現網路請求代理方法 2 -(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response 3 { 4 //擷取要下載的MP3 的長度,收到響應即可得知,不用下載完畢 5 //此代理方法只執行一次,擷取總的資料量 6 alllength =[response expectedContentLength]; 7 } 8 9 //擷取每次下載的資料量,此方法可執行多次10 -(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data11 {12 //將每次下載的資料放入總得資料區域內13 [self.msicDate appendData:data];14 //將本次擷取的資料長度追加到當期長度上15 currlenth +=data.length;16 //更新進度條17 self.msicProgress.progress=(float)currlenth/alllength;18 }19 20 //下載結束調用,播放音樂21 -(void)connectionDidFinishLoading:(NSURLConnection *)connection22 {23 NSError *error=nil;24 //下載完成後,初始化player 然後進行歌曲播放25 self.player = [[AVAudioPlayer alloc]initWithData:self.msicDate error:&error];26 [self.player play];27 }28 29 //請求錯誤調用30 -(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error31 {32 if (error) {33 NSLog(@"error == %@",error);34 }35 }
1 //調用圖片的方法 2 -(void)addTheimg:(NSString *)path 3 { 4 // 1.通過字串建立一個url 5 NSString *imgstr=path; 6 NSURL *imgurl=[NSURL URLWithString:imgstr]; 7 8 //2. 建立網路請求 9 NSURLRequest *requst=[NSURLRequest requestWithURL:imgurl cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:10.0];10 11 NSError *error=nil;12 //3. 連結的伺服器13 NSData *imgDate = [NSURLConnection sendSynchronousRequest:requst returningResponse:nil error:&error];14 if (error)15 {16 //如果錯誤,列印錯誤17 NSLog(@"%@",error);18 }else19 {20 //如果找到圖片,建立一個圖片視圖接收圖片21 UIImageView *img=[[UIImageView alloc]initWithFrame:CGRectMake(5, 20, 50, 50)];22 img.image=[UIImage imageWithData:imgDate];23 //將圖片顯示在視圖24 [self.view addSubview:img];25 }26 27 }
iOS 通過網路請求擷取圖片的下載歌曲