简体   繁体   中英

In song loading time how to display activity indicator if user click the play button in iPhone using AVAudioplayer

I am working in AVAudioplayer in iOS. I am displaying an activity indicator for loading time if a user clicks the play button. My problem is that when I click the play button the loading time activity indicator is not displayed. In playing time activity indicator displayed that is my problem.

-(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];
    }
}

use the activity indicator to display a loading process connect the activity indicator to IBOutlet and synthesize in h file and code like this

declare like this in m file

   @interface updatepoliticalViewController : UIViewController 
  {
      UIActivityIndicatorView *spinner;
    }

and synthesize spinner into ur h file this difenlty work

    -(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];
  }

use this method to stop the activity

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

 }

call this function in view didload and specify how long u want to display the loading process

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

hope it helps for you

First know the concept on activity indicator before start to implement. Thats good attitude for us.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM