【问题标题】:Cocoa Touch - Loading AVAudioPlayerCocoa Touch - 加载 AVAudioPlayer
【发布时间】:2010-08-19 17:09:14
【问题描述】:

我有这段代码可以播放声音,但第一次播放时需要 5 秒才能加载...

如何加快速度?

-(IBAction)playSound{ //play the cricket noise
 NSString *soundPath = [[NSBundle mainBundle] pathForResource:@"Cricket_Sound" ofType:@"mp3"];
 audioPlayer =[[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:soundPath] error:NULL];
 audioPlayer.delegate = self;
 [audioPlayer prepareToPlay];
 audioPlayer.numberOfLoops = 0;
 [audioPlayer play];
}

【问题讨论】:

    标签: cocoa-touch avaudioplayer preloading


    【解决方案1】:

    您可以尝试将以下代码部分放在viewDidLoad 中,以便audioPlayer 实例在被要求实际播放音频之前准备好播放:

    NSString *soundPath = [[NSBundle mainBundle] pathForResource:@"Cricket_Sound" ofType:@"mp3"];
     audioPlayer =[[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:soundPath] error:NULL];
     audioPlayer.delegate = self;
     [audioPlayer prepareToPlay];
     audioPlayer.numberOfLoops = 0;
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-10-31
      • 1970-01-01
      相关资源
      最近更新 更多