//ConentCell.h
#import <UIKit/UIKit.h>@interface ConentCell : UITableViewCell<NSURLConnectionDataDelegate>
{
long long allLength,currentLength;
NSMutableData * allData;
UIProgressView * progress;
}//ConentCell.m
#import "ConentCell.h"
#import <AVFoundation/AVFoundation.h>@implementation ConentCell
static int a=0;
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
// Initialization code
NSArray * arr=[NSArray arrayWithObjects:@"http://zhangmenshiting.baidu.com/data2/music/7306551/7302495140400128.mp3?xcode=c0880abde3bf0952b5949db22586f5d7",@"http://music.baidu.com/data/music/file?link=http://zhangmenshiting.baidu.com/data2/music/20882281/20882278165600128.mp3?xcode=c44299c26a33d9ac553396c56deab3f1",@"http://music.baidu.com/data/music/file?link=http://zhangmenshiting.baidu.com/data2/music/23977117/2398208714040096.mp3?xcode=770acb99a10bb36756a00a6b01fdecfe", nil];
NSURL * url=[NSURL URLWithString:arr[a]];
a++;
//创建请求
NSURLRequest * request=[[NSURLRequest alloc] initWithURL:url cachePolicy:NSURLRequestReloadIgnoringLocalCacheData timeoutInterval:120];
//发送请求
[NSURLConnection connectionWithRequest:request delegate:self];
self.imageView.image=[UIImage imageNamed:@"icon57.png"];
progress=[[UIProgressView alloc] initWithFrame:CGRectMake(100, 80, 150, 50)];
[self addSubview:progress];
//[progress release];
}
return self;
}
-(void) connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
NSLog(@"接收请求,开始下载");
allLength=[response expectedContentLength];
currentLength=0;
if(allData)
{
[allData release];
}
NSLog(@"本音乐大小为:%lld",allLength);
allData=[[NSMutableData alloc] init];
}
-(void) connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
//long long length;
currentLength+=data.length;
NSLog(@"当前下载了:%lld",currentLength);
long long temp = currentLength *100;
temp = temp/allLength;//计算出百分比
progress.progress=temp/100;
NSLog(@"下载百分比为:%lld%@",temp,@"%");
[allData appendData:data];
}
-(void) connectionDidFinishLoading:(NSURLConnection *)connection
{
AVAudioPlayer * player=[[AVAudioPlayer alloc] initWithData:allData error:nil];
player.volume=1;
[player play];
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
[super setSelected:selected animated:animated];
// Configure the view for the selected state
}为什么输出加载的进度是100%没有过程了,也不会播放
#import <UIKit/UIKit.h>@interface ConentCell : UITableViewCell<NSURLConnectionDataDelegate>
{
long long allLength,currentLength;
NSMutableData * allData;
UIProgressView * progress;
}//ConentCell.m
#import "ConentCell.h"
#import <AVFoundation/AVFoundation.h>@implementation ConentCell
static int a=0;
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
// Initialization code
NSArray * arr=[NSArray arrayWithObjects:@"http://zhangmenshiting.baidu.com/data2/music/7306551/7302495140400128.mp3?xcode=c0880abde3bf0952b5949db22586f5d7",@"http://music.baidu.com/data/music/file?link=http://zhangmenshiting.baidu.com/data2/music/20882281/20882278165600128.mp3?xcode=c44299c26a33d9ac553396c56deab3f1",@"http://music.baidu.com/data/music/file?link=http://zhangmenshiting.baidu.com/data2/music/23977117/2398208714040096.mp3?xcode=770acb99a10bb36756a00a6b01fdecfe", nil];
NSURL * url=[NSURL URLWithString:arr[a]];
a++;
//创建请求
NSURLRequest * request=[[NSURLRequest alloc] initWithURL:url cachePolicy:NSURLRequestReloadIgnoringLocalCacheData timeoutInterval:120];
//发送请求
[NSURLConnection connectionWithRequest:request delegate:self];
self.imageView.image=[UIImage imageNamed:@"icon57.png"];
progress=[[UIProgressView alloc] initWithFrame:CGRectMake(100, 80, 150, 50)];
[self addSubview:progress];
//[progress release];
}
return self;
}
-(void) connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
NSLog(@"接收请求,开始下载");
allLength=[response expectedContentLength];
currentLength=0;
if(allData)
{
[allData release];
}
NSLog(@"本音乐大小为:%lld",allLength);
allData=[[NSMutableData alloc] init];
}
-(void) connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
//long long length;
currentLength+=data.length;
NSLog(@"当前下载了:%lld",currentLength);
long long temp = currentLength *100;
temp = temp/allLength;//计算出百分比
progress.progress=temp/100;
NSLog(@"下载百分比为:%lld%@",temp,@"%");
[allData appendData:data];
}
-(void) connectionDidFinishLoading:(NSURLConnection *)connection
{
AVAudioPlayer * player=[[AVAudioPlayer alloc] initWithData:allData error:nil];
player.volume=1;
[player play];
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
[super setSelected:selected animated:animated];
// Configure the view for the selected state
}为什么输出加载的进度是100%没有过程了,也不会播放
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货