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

时间:2014-07-07 04:56:17

标签: ios audio progress-bar

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

我的代码。

  -(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,但是在计算文件大小之后它还没有工作我的设备它仍然显示更多数字并且它没有播放音频文件请告诉我在上面哪里做错了代码。

感谢。

0 个答案:

没有答案