在歌曲加载时间如果用户使用AVAudioplayer点击iPhone中的播放按钮,如何显示活动指示器

时间:2014-04-05 06:03:14

标签: ios iphone objective-c avaudioplayer

我在iOS的AVAudioplayer工作。如果用户点击播放按钮,我正在显示加载时间的活动指示器。我的问题是,当我点击播放按钮时,不会显示加载时间活动指示器。在播放时间活动指示器显示这是我的问题。

-(void)viewDidLoad
{
    // loading View
    loadingView=[[UILabel alloc]initWithFrame:CGRectMake(135, 200, 40, 40)];
    loadingView.backgroundColor=[UIColor whiteColor];
    loadingView.clipsToBounds=YES;
    loadingView.layer.cornerRadius=10.0;
    activityView = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhite];
    activityView.frame = CGRectMake(10, 11, activityView.bounds.size.width, activityView.bounds.size.height);
    [loadingView addSubview:activityView];
}

-(void)playOrPauseButtonPressed:(id)sender
{
    if(playing==NO)
    {
        [UIApplication sharedApplication].networkActivityIndicatorVisible=YES;
        [self.view addSubview:loadingView];
        [activityView startAnimating];
        [playButton setBackgroundImage:[UIImage imageNamed:@"Pause.png"] forState:UIControlStateNormal];

        // Here Pause.png is a image showing Pause Button.
        NSError *err=nil;
        AVAudioSession *audioSession=[AVAudioSession sharedInstance];
        [audioSession setCategory:AVAudioSessionCategoryPlayback error:nil];

        NSLog(@"%@ %d",urlsArray,selectedIndex);

        NSString *sourcePath=[urlsArray objectAtIndex:selectedIndex];
        NSData *objectData=[NSData dataWithContentsOfURL:[NSURL URLWithString:sourcePath]];
        audioPlayer = [[AVAudioPlayer alloc] initWithData:objectData error:&err];

        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.delegate=self;

        if(!audioPlayer.playing)
        {
            [audioPlayer play];
        }

        playing=YES;
    }
    else if (playing==YES)
    {
        [playButton setBackgroundImage:[UIImage imageNamed:@"play.png"] forState:UIControlStateNormal];
        [audioPlayer pause];
        playing=NO;
    }

    if (self.audioPlayer)
    {
        [self updateViewForPlayerInfo];
        [self updateViewForPlayerState];
        [self.audioPlayer setDelegate:self];
    }
}

2 个答案:

答案 0 :(得分:1)

使用活动指示器显示加载过程将活动指示器连接到IBOutlet并合成h文件和代码如下

在m文件中声明如下

   @interface updatepoliticalViewController : UIViewController 
  {
      UIActivityIndicatorView *spinner;
    }

并将微调器合成到你的文件中这个不同的工作

    -(void)temp
 {
       spinner = [[UIActivityIndicatorView alloc]initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];

       spinner.frame = CGRectMake(0.0, 0.0, 40.0, 40.0);
       [spinner setCenter:CGPointMake(160, 240)];
       [self.view addSubview:spinner];
       spinner.color = [UIColor blueColor];
       spinner.backgroundColor = [UIColor colorWithWhite:0.2 alpha:0.4];
       [spinner startAnimating];
       [spinner release];
  }

使用此方法停止活动

-(void) myMethod{
   [spinner stopAnimating];
   spinner.hidden = YES;

 }

在视图didload中调用此函数并指定您想要显示加载过程的时间

- (void)viewDidLoad
 {
     [super viewDidLoad];
     [self temp];
     [self performSelector:@selector(myMethod) withObject:nil afterDelay:5.0f];
 }

希望对你有所帮助

答案 1 :(得分:0)

在开始实施之前首先要了解活动指标的概念。那对我们态度很好。

http://www.ioscreator.com/tutorials/display-an-activity-indicator