在ios中如何在歌曲加载时间中显示或隐藏活动指示器
我在iOS的AVAudioplayer中工作。如果用户单击播放按钮,我将显示加载时间的活动指示器。我的问题是,当我单击“播放”按钮时,会显示加载时间活动指示器。在播放时间中也会显示活动指示器,这是我的问题。我需要在歌曲播放时间中隐藏活动指示器在ios中如何在歌曲加载时间中显示或隐藏活动指示器,ios,objective-c,iphone,avaudioplayer,Ios,Objective C,Iphone,Avaudioplayer,我在iOS的AVAudioplayer中工作。如果用户单击播放按钮,我将显示加载时间的活动指示器。我的问题是,当我单击“播放”按钮时,会显示加载时间活动指示器。在播放时间中也会显示活动指示器,这是我的问题。我需要在歌曲播放时间中隐藏活动指示器 - (void)playOrPauseButtonPressed:(id)sender { [self.view addSubview:today_slokaText]; if (playing == NO) { [UIApp
- (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];
}
}
为什么在停止后将其添加为子视图? 您可以将其alpha设置为0.0或从superview中删除:
[self.activityIndicator stopAnimating];
self.activityIndicator.alpha = 0.0;
拆下线路
[bg_Image addSubview:activityIndicator];
停止活动指示器后,您不会将其添加到bg_映像中。
你仍然有同样的问题,你可以使用线程
[NSThread detachNewThreadSelector:@selector(stopIndicator:) toTarget:self withObject:nil];
-(void)stopIndicator
{
[self.activityIndicator stopAnimating];
}
注意
[bg_Image addSubview:activityIndicator]代码>执行两次。