如何在ios中显示或隐藏歌曲加载时间中的活动指示器

时间:2015-02-18 09:15:13

标签: 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];
  }
}

2 个答案:

答案 0 :(得分:0)

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

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

答案 1 :(得分:0)

删除行

[bg_Image addSubview:activityIndicator];

停止活动指示后,您不会添加到bg_Image。 你仍然可以使用相同的问题。

[NSThread detachNewThreadSelector:@selector(stopIndicator:) toTarget:self withObject:nil];

-(void)stopIndicator
{
    [self.activityIndicator stopAnimating];
}