简体   繁体   中英

slow start my AVAudioplayer if i click the play button

I am making Audioplayer using AVAudioplayerDelegate .The audio player functionality successfully Done.But i have small issues.I click the play button the audio lag is palpate at least 1 to 2 mins.this is my problem.I am using this code.But my problem should not solve.so please help me anybody.

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

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

}

}

- (void)setupAudio 

{

[[AVAudioSession sharedInstance] setCategory: AVAudioSessionCategoryPlayback error: nil];

UInt32 doSetProperty = 1;

AudioSessionSetProperty (kAudioSessionProperty_OverrideCategoryMixWithOthers, sizeof(doSetProperty), &doSetProperty);

[[AVAudioSession sharedInstance] setActive: YES error: nil];

}

This Quetion May be Duplicate of this link :-- what is the best way to play sound quickly upon fast button presses xcode?

I hope this help :)

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