如果我单击播放按钮,请慢慢启动我的AVAudioplayer

时间:2014-04-03 05:39:20

标签: ios iphone objective-c xcode avaudioplayer

我使用 AVAudioplayerDelegate 制作 Audioplayer 。音频播放器功能成功完成。但是我有小问题。我点击播放按钮音频延迟至少触及1到2分钟。这是我的问题。我正在使用此代码。但我的问题不应该解决。所以请帮助我任何人。

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

}

1 个答案:

答案 0 :(得分:0)

此序列可能与此链接重复: - what is the best way to play sound quickly upon fast button presses xcode?

我希望这有帮助:)