ios - 如何在 iOS 中的歌曲加载时间显示事件指示器

标签 ios objective-c

我正在使用音频播放器功能。我正在使用 AVAudioplayer 框架。我有一些问题。我想在歌曲加载时间放置一个事件指示器 View 。这样,当您触摸播放时,它会启动事件指示器 View ,并且当音频播放时,它会启动事件指示器 View 。启动,事件指示器停止。这是我的要求。我正在编写一些代码。但它对我来说效果不佳。请帮助我。

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

最佳答案

你可以像下面这样创建加载自定义加载 View ..

-(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。

希望对你有帮助

关于ios - 如何在 iOS 中的歌曲加载时间显示事件指示器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24114271/

相关文章:

ios - 了解 UILocalNotification 时区

ios - Objective-C非弧形物体

ios - 将 Sprite Kit 与空应用程序混合

ios - 有没有办法将 Touch ID 与 iOS Keychain 一起使用,但不提示输入用户密码?

ios - NSURLConnection 返回错误的缓存数据

ios - Touch 开始无法在 xcode 中使用 sprite kit 正常运行

将文本从一个 ViewController 发送到另一个时,iOS 应用程序崩溃

objective-c - 从数组中检索单独的值

ios - 当 UItableViewcell 高亮显示时,UITableViewcell 的 subview 消失,无论如何要修复它?

objective-c - 使用 objective-c 计算xml中的元素