简体   繁体   中英

How to display Activity Indicator in song loading time in iOS

I am working Audioplayer functionality.I am using AVAudioplayer framework.i have some problem.i want to put an activity indicator view in song loading time.so that when you touch the play it starts the activity indicator view and when the audio starts, the activity indicator stops.That is my requirement.i am writing some code.But it is not working fine for me.help me any body.

-(void)viewDidLoad {
    [self temp];
    [self loadData];       

    loadingLabel = [[UILabel alloc] initWithFrame:CGRectMake(94, 28, 130, 22)];
    loadingLabel.backgroundColor = [UIColor clearColor];
    loadingLabel.textColor = [UIColor blackColor];
    [loadingLabel setFont:[UIFont systemFontOfSize:10.0]];
    loadingLabel.adjustsFontSizeToFitWidth = YES;
    loadingLabel.textAlignment = NSTextAlignmentCenter;
    loadingLabel.text = @"loading In...";
    loadingLabel.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleBottomMargin  | UIViewAutoresizingFlexibleTopMargin;
}

-(void)temp {
    // loading View
    loadingView=[[UILabel alloc]initWithFrame:CGRectMake(135, 200, 40, 40)];
    loadingView.backgroundColor=[UIColor clearColor];
    loadingView.clipsToBounds=YES;
    loadingView.layer.cornerRadius=10.0;

    activityView = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhite];
    activityView.tag=960;
    activityView.frame = CGRectMake(10, 11, activityView.bounds.size.width, activityView.bounds.size.height);

    [loadingView addSubview:activityView];
}

-(void)playOrPauseButtonPressed:(id)sender {
    if(playing==NO)
    {
        [playButton setBackgroundImage:[UIImage imageNamed:@"Pause.png"] forState:UIControlStateNormal];

        // Here Pause.png is a image showing Pause Button.
        NSError *err=nil;

        if (!audioPlayer)
        {
            audioSession=[AVAudioSession sharedInstance];
            [audioSession setCategory:AVAudioSessionCategoryPlayback error:nil];

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

            NSString *sourcePath=[urlsArray objectAtIndex:selectedIndex];
            NSData *objectData=[NSData dataWithContentsOfURL:[NSURL URLWithString:sourcePath]];

            NSLog(@"%@",objectData);

            audioPlayer = [[AVAudioPlayer alloc] initWithData:objectData error:&err];
            [UIApplication sharedApplication].networkActivityIndicatorVisible=YES;
            [loadingView addSubview:activityView];
            [loadingView addSubview:loadingLabel];

            [self.view addSubview:loadingView];
            [activityView startAnimating];
            [self.view addSubview:loadingView];
        }

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

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

        playing=NO;

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

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

-(void)loadData
{
    [UIApplication sharedApplication].networkActivityIndicatorVisible=YES;
    [self.view addSubview:loadingView];
    [activityView startAnimating];            
    loadingConnection=[[NSURLConnection alloc]initWithRequest:request delegate:self startImmediately:YES];
}

-(void)connectionDidFinishLoading:(NSURLConnection *)connection
{
    [UIApplication sharedApplication].networkActivityIndicatorVisible=NO;
    [activityView stopAnimating];
    [loadingView removeFromSuperview];
}

you can crate loading custom loading view like bellow..

-(void) showLoadingView
{
    CGRect screenRect = [UIScreen mainScreen].bounds;

    if (loadingView == nil) 
    {
        loadingView = [[UIView alloc] initWithFrame:screenRect];
        loadingView.opaque = NO;
        loadingView.backgroundColor = [UIColor darkGrayColor];
        loadingView.alpha = 0.5;

        UIActivityIndicatorView *spinningWheel = [[UIActivityIndicatorView alloc] initWithFrame:CGRectMake(self.window.center.x-18.0,self.window.center.y-18.0, 37.0, 37.0)];
        [spinningWheel startAnimating];
        spinningWheel.activityIndicatorViewStyle = UIActivityIndicatorViewStyleWhiteLarge;
        spinningWheel.alpha = 1.0;
        [loadingView addSubview:spinningWheel];
        [spinningWheel release];        
    }    

    [window addSubview:loadingView];
}
-(void) showLoadingViewMessage:(NSString *)msg 
{
    CGRect screenRect = [UIScreen mainScreen].bounds;

    if (loadingView == nil) 
    {
        loadingView = [[UIView alloc] initWithFrame:screenRect];
        loadingView.opaque = NO;
        loadingView.backgroundColor = [UIColor darkGrayColor];
        loadingView.alpha = 0.5;

        UIActivityIndicatorView *spinningWheel = [[UIActivityIndicatorView alloc] initWithFrame:CGRectMake(self.window.center.x-18.0, self.window.center.y-18.0, 37.0, 37.0)];
        [spinningWheel startAnimating];
        spinningWheel.activityIndicatorViewStyle = UIActivityIndicatorViewStyleWhiteLarge;
        spinningWheel.alpha = 1.0;
        [loadingView addSubview:spinningWheel];
        [spinningWheel release];

    }   

    UILabel *lblTitle = [[UILabel alloc] initWithFrame:CGRectMake(0, 250.0, 320.0, 80.0)];
    lblTitle.text = msg;
    lblTitle.textAlignment = UITextAlignmentCenter;
    lblTitle.textColor = [UIColor whiteColor];
    lblTitle.alpha = 1.0;
    lblTitle.backgroundColor = [UIColor clearColor];
    lblTitle.numberOfLines = 0;
    //lblTitle.layer.borderColor = [UIColor blueColor].CGColor;
    //lblTitle.layer.borderWidth = 1.0;
    [loadingView addSubview:lblTitle];
    [lblTitle release];


    [window addSubview:loadingView];
}

-(void) hideLoadingView 
{   
    if(loadingView)
    {
        [loadingView removeFromSuperview];
        loadingView = nil;
    }
}

put that all three methods in your AppDelegate and Call the showLoadingView whenevery you whant loading and call hideLoadingView whenever You dont want .

i hope it is helpful for you..

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