ios - 如何在 ios 的歌曲加载时间中显示或隐藏事件指示器

标签 ios objective-c iphone avaudioplayer

我在 iOS 的 AVAudioplayer 工作。如果用户单击播放按钮,我将显示加载时间的事件指示器。我的问题是,当我单击播放按钮时,会显示加载时间事件指示器。在播放时间也显示事件指示器,这是我的问题。我需要在歌曲播放时间隐藏事件指示器

- (void)playOrPauseButtonPressed:(id)sender

{

  [self.view addSubview:today_slokaText];

  if (playing == NO)

  {

    [UIApplication sharedApplication].networkActivityIndicatorVisible = YES;

    [playButton setBackgroundImage:[UIImage imageNamed:@"Pause.png"]
                          forState:UIControlStateNormal];

    [self.view addSubview:today_slokaText];

    // Here Pause.png is a image showing Pause Buttomn

    NSError *err = nil;

    if (!audioPlayer)

    {

      [self loadSongAtIndex:selectedIndex];

      playing = YES;
    }

    if (err)

    {

      NSLog(@"Error %ld,%@", (long)err.code, err.localizedDescription);
    }

    NSTimeInterval bufferDuration = 0.005;

    [audioSession setPreferredIOBufferDuration:bufferDuration error:&err];

    if (err)

    {

      NSLog(@"Error %ld, %@", (long)err.code, err.localizedDescription);
    }

    double sampleRate = 44100.0;

    [audioSession setPreferredSampleRate:sampleRate error:&err];

    if (err)

    {

      NSLog(@"Error %ld, %@", (long)err.code, err.localizedDescription);
    }

    [audioSession setActive:YES error:&err];

    if (err)

    {

      NSLog(@"Error %ld,%@", (long)err.code, err.localizedDescription);
    }

    sampRate = audioSession.sampleRate;

    bufferDuration = audioSession.IOBufferDuration;

    NSLog(@"SampeRate:%0.0fHZI/OBufferDuration:%f", sampleRate, bufferDuration);

    audioPlayer.numberOfLoops = 0;

    [audioPlayer prepareToPlay];

    [audioPlayer play];

    audioPlayer.delegate = self;

    if (!audioPlayer.playing)

    {

      [audioPlayer play];

      activityIndicator = [[UIActivityIndicatorView alloc]
          initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhite];

      [self.view addSubview:self.activityIndicator];

      self.activityIndicator.center = CGPointMake(
          self.view.frame.size.width / 2, self.view.frame.size.height / 2);

      [self.activityIndicator startAnimating];

      [playButton setBackgroundImage:[UIImage imageNamed:@"Pause.png"]
                            forState:UIControlStateNormal];

      [bg_Image addSubview:activityIndicator];

    }

    else if (audioPlayer.playing == YES)

      [audioPlayer play];

    [self.activityIndicator stopAnimating];

    [playButton setBackgroundImage:[UIImage imageNamed:@"Pause.png"]
                          forState:UIControlStateNormal];

    playing = YES;

  }

  else if (playing == YES)

  {

    [self.activityIndicator stopAnimating];

    [bg_Image addSubview:activityIndicator];

    [playButton setBackgroundImage:[UIImage imageNamed:@"play12.png"]
                          forState:UIControlStateNormal];

    [self.view addSubview:today_slokaText];

    [audioPlayer pause];

    playing = NO;

    timer = [NSTimer
        scheduledTimerWithTimeInterval:1.0
                                target:self
                              selector:@selector(updateViewForPlayerState)
                              userInfo:nil
                               repeats:YES];

    [self.view addSubview:today_slokaText];
  }
}

最佳答案

为什么停止后将其添加为 subview ?
您可以将其 alpha 设置为 0.0 或从 superview 中删除:

[self.activityIndicator stopAnimating];
self.activityIndicator.alpha = 0.0;

关于ios - 如何在 ios 的歌曲加载时间中显示或隐藏事件指示器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28579779/

相关文章:

ios - 通过更新约束在底部的 MGLMapView scaleBar 位置

ios - 安装 ios-buildkit 时出错

iphone - 指向 Objective-C 中的指针的指针

ios - Swift - 在 AppDelegate 中添加 subview

ios - 使用 Realm 保存数据数组或自定义类

ios - SwiftUI:如何更改列表所选项目的颜色

ios - NSURL 类似于 CFURLCreateCopyAppendingPathComponent?

iphone - 清除 dequeueReusableCellWithIdentifier 单元格

iphone - 加快 iPhone 3G 上的全屏图像绘制

javascript - 使用 Javascript 中的 SauceLabs 在 iPhone 上进行 Selenium 测试