ios - 显示微调器事件,直到加载音频文件 IOS7

标签 ios audio progress-bar spinner

您好,在我的应用程序中,我有音频 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];
}

Audio 是我从 UITableView 传递的音频 URL 变量,请在此处添加微调器事件。

谢谢。

最佳答案

试试这个代码...

- (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");
}

关于ios - 显示微调器事件,直到加载音频文件 IOS7,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24011495/

相关文章:

Progress Bar 的 Java Swing Worker - UI 长时间无响应

javascript - HTML5 进度最大值和值范围 : too large values?

C#进度条改变颜色

UINavigationController 和 UISearchBar 之间的 iOS 9 细线

ios - iOS应用程序中的音频文件

swift - 为什么按下按钮时无法播放声音?

php - 在html标签<audio>中回显字符串

jquery - iOS 上的 Bootstrap Modal 缩放问题

ios - openfire 消息存档管理中的服务不可用错误

ios - SpriteKit : How to detect touches on node that began outside of node