嗨,在我的应用程序中,我有音频URL
使用URL
来播放音频需要花费太多时间才能玩,但所以我想加载微调器或进度条,如加载显示为用户,直到此音频加载播放。
我有UITableView
的歌曲列表,当用户点击列表时会导航到另一个UIViewController
,现在它将播放歌曲我想向用户显示微调器或进度条知道音频正在加载请告诉我们如何做到这一点。
我的音频播放代码。
- (IBAction)play:(id)sender {
NSString *urlstr = audio;
urlstr = [urlstr stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSURL *url = [NSURL URLWithString:urlstr];
NSData *data = [NSData dataWithContentsOfURL:url];
// _audioPlayer = self;
_audioPlayer = [[AVAudioPlayer alloc] initWithData:data error:nil];
[_audioPlayer play];
}
音频是我的音频URL
变量,我正在UITableView
传递,请在此处如何添加微调器活动。
感谢。
答案 0 :(得分:1)
试试这段代码......
- (IBAction)play:(id)sender {
NSLog(@"%@",aad);
NSString *strurl=[self urlencode:aad];
NSURL *url=[NSURL URLWithString:strurl];
NSURLRequest *req=[NSURLRequest requestWithURL:url];
NSURLConnection *con=[NSURLConnection connectionWithRequest:req delegate:self];
if (con) {
datas=[[NSMutableData alloc]init];
}
[self.indicator startAnimating];
self.indicator.activityIndicatorViewStyle = UIActivityIndicatorViewStyleGray;
}
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response{
NSLog(@"%lld",[response expectedContentLength]);
self.length = [response expectedContentLength];
}
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data1{
[datas appendData:data1];
// NSLog(@"%f",(float)[datas length]);
// NSLog(@"%f",(float)self.length);
float progress = (float)[datas length]/(float)self.length;
NSLog(@"%f",progress);
// float check = [NSString stringWithFormat:@"%0.2f%%",progress*100];
float check=progress*100;
if (check==100) {
[self.indicator stopAnimating];
}
}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection{
NSData *data = datas;
NSLog(@"%@",data);
_audioPlayer = [[AVAudioPlayer alloc] initWithData:data error:nil];
self.audioPlayer.delegate=self;
[_audioPlayer play];
}
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)erro
{
NSLog(@"fail");
}