Ios 显示进度条,直到加载音频文件在设备中不起作用

Ios 显示进度条,直到加载音频文件在设备中不起作用,ios,audio,progress-bar,Ios,Audio,Progress Bar,在我的应用程序中,我有从服务器下载的音频文件,我想在应用程序中播放音频,直到它加载音频文件,我想显示进度条,但它在刺激器中工作,但在我的设备中不工作 我的密码 -(NSString *)urlencode:(NSString *)str { NSString *encodeString=(NSString *)CFBridgingRelease(CFURLCreateStringByAddingPercentEscapes(NULL, (CFStringRef)str, N

在我的应用程序中,我有从服务器下载的音频文件,我想在应用程序中播放音频,直到它加载音频文件,我想显示进度条,但它在刺激器中工作,但在我的设备中不工作

我的密码

  -(NSString *)urlencode:(NSString *)str
   {
      NSString *encodeString=(NSString *)CFBridgingRelease(CFURLCreateStringByAddingPercentEscapes(NULL, (CFStringRef)str, NULL, (CFStringRef)@"", kCFStringEncodingUTF8));
      return encodeString;
   }
 - (void)viewDidLoad
   {
     [super viewDidLoad];

     NSString *strurl=[self urlencode:audiourl];
     NSURL *url=[NSURL URLWithString:strurl];
     NSURLRequest *req=[NSURLRequest requestWithURL:url];
     NSURLConnection *con=[NSURLConnection connectionWithRequest:req delegate:self];

if (con) {

      datas=[[NSMutableData alloc]init];

     }
   spinner =  [MBProgressHUD showHUDAddedTo:self.view animated:YES];

   spinner.mode = MBProgressHUDModeCustomView;

   [spinner setLabelText:@"audio buffering....."];

   [spinner setLabelFont:[UIFont systemFontOfSize:15]];

   [spinner show:YES];

  }
我已经做了类似计算的文件大小取决于文件大小,它将显示像音频缓冲

我的计算代码

   - (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];


        float progress =  (float)[datas length]/(float)self.length;
         NSLog(@"%f",progress);

         float check=progress*100;
   if (check==100) {
      [spinner hide:YES];

       [spinner removeFromSuperViewOnHide];

      }   
    }
我的音频播放和暂停代码

  - (void)connectionDidFinishLoading:(NSURLConnection *)connection{

      NSData *data = datas;
      NSLog(@"%@",data);
      _audioPlayer = [[AVAudioPlayer alloc] initWithData:data error:nil];
     _audioPlayer.delegate=self;

   }

 - (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)erro
  {
      NSLog(@"fail");
   }


 - (IBAction)play:(id)sender {
        UIButton *btn = (UIButton *)sender;
      if (self.isPlaying) {
          [btn setImage:[UIImage imageNamed:@"button_pause.png"] forState:UIControlStateNormal];
          NSLog(@"Pausing Music");
         [_audioPlayer pause];
         self.isPlaying = NO;        
       }
    else {
        [btn setImage:[UIImage imageNamed:@"button_play.png"] forState:UIControlStateNormal];
        NSLog(@"Playing music");
        [self.audioPlayer play];
        self.isPlaying = YES;
       }

    }
我在刺激器中使用了上面的f9,但在计算了文件大小后,我的设备不工作,而且它仍然显示一些数字,并且没有播放音频文件。请告诉我上面代码中的错误


谢谢。

发布的代码的实际问题是什么?您是否已通过调试器运行了它,并检查了值是否符合预期。顺便说一句-你最好通过查看下载字节数是否等于预期字节来检查是否完成,而不是查看某个不精确的计算浮点值是否正好等于100。@r它是否在刺激器中工作正常,但它不工作我的设备这是我的问题你没有得到检查==100,对吗?正如rmaddy所说,为什么不比较expectedDatalength==self.lengthdirectly@user3793919我明白了。在实际设备上运行时,需要提供有关问题的详细信息。使用调试器,看看问题出在哪里。@rmaddy您在设备中运行时是对的,它不会进入我的if条件,也不会检查等于100。请告诉我我在哪里完成了工作]