【问题标题】:How to display Activity Indicator in song loading time in iOS如何在 iOS 中的歌曲加载时间显示活动指示器
【发布时间】:2014-06-09 13:48:25
【问题描述】:

我正在使用 Audioplayer 功能。我正在使用 AVAudioplayer 框架。我有一些问题。我想在歌曲加载时间放置一个活动指示器视图。这样当你触摸播放时它会启动活动指示器视图,当音频启动,活动指示器停止。这是我的要求。我正在编写一些代码。但它对我来说不能正常工作。请帮助我。

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

【问题讨论】:

    标签: ios objective-c


    【解决方案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。

    希望对你有帮助。

    【讨论】:

    • Mits Bhadeshiya。谢谢你的回复。我有很多疑问。所以你会来堆栈溢出charing 网站和聊天室“iOS 教程”。我在“iOS 教程”聊天室。请来兄弟
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多