如何在iOS中的歌曲加载时间中显示活动指示器

时间:2014-06-09 05:38:48

标签: ios objective-c

我正在使用Audioplayer功能。我正在使用AVAudioplayer framework.i有一些问题。我想在歌曲加载时间中放置一个活动指示器视图。所以当你触摸播放时它会启动活动指示器视图和音频时开始,活动指示器停止。这是我的要求。我正在写一些代码。但它对我不起作用。帮助我任何身体。

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

1 个答案:

答案 0 :(得分:1)

你可以装箱加载自定义装载视图,如下图..

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

将所有三种方法都放在你的AppDelegate中并在你加载时调用showLoadingView并在你不想要的时候调用hideLoadingView。

我希望它对你有帮助..