簡體   English   中英

在歌曲加載時間中,如果用戶使用AVAudioplayer單擊iPhone中的播放按鈕,如何顯示活動指示器

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

我正在iOS的AVAudioplayer中工作。 如果用戶單擊播放按鈕,我將顯示一個加載時間的活動指示器。 我的問題是,當我單擊播放按鈕時,加載時間活動指示符未顯示。 在播放時間顯示的活動指示器是我的問題。

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

使用活動指示器顯示加載過程,將活動指示器連接到IBOutlet並在h文件和類似的代碼中進行合成

在m文件中這樣聲明

   @interface updatepoliticalViewController : UIViewController 
  {
      UIActivityIndicatorView *spinner;
    }

並將微調框合成到您的文件中

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

使用此方法停止活動

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

 }

在didload視圖中調用此函數,並指定要顯示加載過程的時間

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

希望對您有幫助

在開始實施之前,首先要了解活動指示器的概念。 那對我們是好的態度。

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM