2013-08-02 28 views
0

每當我下面播放音頻文件像以前一樣,它正在幾秒鐘就可以開始播放音頻。我從一些論壇上看到,並且明白,如果我們在那裏分配對象,AVAudioPlayer將需要幾秒鐘才能開始播放。我想早些時候分配這個對象(可能是Appdelegate本身),之前我想玩,所以當我想玩它時,它會立即播放。創建AVAudioPlayer對象太多的上場動​​態音頻

NSURL *audioPathURL = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:audioName ofType:@"wav"]]; 

audioP = [[AVAudioPlayer alloc]initWithContentsOfURL:audioPathURL error:NULL]; 
[appDelegate.audioP play]; 

但是,我傳遞的音頻鏈接在運行時動態的,所以我想知道我可以在更早的對象分配和再以後能夠通過動態的音頻URL路徑?

請注意,我的問題與此問題有所不同Slow start for AVAudioPlayer the first time a sound is played 在現有問題中,他們講述了一個音頻文件並在需要時播放它。但是,我有很多不同的音頻文件和URL路徑隨機生成,並在運行時播放,所以我不知道什麼是實際的路徑設置和播放。所以,這裏提到的答案 - >Slow start for AVAudioPlayer the first time a sound is played 不會幫助我的問題。 我不能使用prepareToPlay,因爲音頻路徑URL被設置在運行時間,不只是一個音頻被用於所有的時間玩,將有超過20個音頻文件,隨機選擇並設置爲播放一次一個。所以,我需要正確的答案,這不是一個重複的問題。

謝謝!

+0

見本:HTTP: //stackoverflow.com/questions/9710679/avaudioplayer-changing-current-url –

+0

如果你知道你要玩的東西你可以用prepareToPlay:儘量減少滯後。這將預加載緩衝區並獲取聲音硬件。 – Krumelur

+0

嗨,我怎麼會用prepareToPlay我的情況(音頻URL路徑將在運行時間僅過)? – Getsy

回答

0

對於這裏的單個文件的解決方案:在您的file.h

#import <AVFoundation/AVFoundation.h> 

@interface AudioPlayer : UIViewController <AVAudioPlayerDelegate> { 


} 

要使用按鈕使用添加- (IBAction)Sound:(id)sender;file.h

現在,在您file.m

//這是一個動作,但你可以在虛空內使用開始自動運行

@implementation AudioPlayer { 

    SystemSoundID soundID; 
} 

- (IBAction)Sound:(id)sender { 

    AudioServicesDisposeSystemSoundID(soundID); 
    CFBundleRef mainBundle = CFBundleGetMainBundle(); 
    CFURLRef soundFileURLRef; 

//you can use here extension .mp3/.wav/.aif/.ogg/.caf/and more 

    soundFileURLRef = CFBundleCopyResourceURL(mainBundle, (CFStringRef) @"sound" ,CFSTR ("mp3") , NULL); 
    AudioServicesCreateSystemSoundID(soundFileURLRef, &soundID); 
    AudioServicesPlaySystemSound(soundID); 

} 

如果你想用你的代碼,以播放從URL音頻您可以使用此:

#import <AVFoundation/AVFoundation.h> 
#import <MediaPlayer/MediaPlayer.h> 

@interface AudioPlayer : UIViewController <AVAudioPlayerDelegate> { 

AVPlayer *mySound; 

} 

@property (strong, nonatomic) AVPlayer *mySound; 
@end 

而且file.m

- (IBAction)playPause:(id)sender { 

if (mySound.rate == 0){ 

    NSURL *url = [NSURL URLWithString:@"http://link.mp3"]; 
    mySound = [[AVPlayer alloc] initWithURL:url]; 
    [mySound setAllowsExternalPlayback:YES]; 
    [mySound setUsesExternalPlaybackWhileExternalScreenIsActive: YES]; 
    [mySound play]; 

} else { 

[mySound pause]; 

} 

} 

希望這能幫助你!

0

有一兩件事可以做,以降低第一負載+遊戲的等待時間是你需要通過加載和播放一個虛擬文件之前,「旋轉起來」的基本音頻系統。例如,在application:didFinishLaunchingWithOptions:添加一些這樣的代碼:

// this won't effect the problem you're seeing, but it's good practice to explicitly 
// set your app's audio session category 
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error:nil]; 
NSURL *audioURL = [[NSBundle mainBundle] URLForResource:@"dummy.wav" withExtension:nil]; 
AVAudioPlayer *dplayer = [[AVAudioPlayer alloc] initWithContentsOfURL:audioURL error:nil]; 
// we don't want to hear it (alternatively, the audiofile you use for this purpose can be silence.) 
dplayer.volume = 0.f; 
[dplayer prepareToPlay]; 
[dplayer play]; 
[dplayer stop]; 

一旦該代碼完成,隨後的實例和AVAudioPlayer實例的播放應該具有相當低的等待時間(當然以及下1秒)

+0

嗨,謝謝你的建議。但是,我的問題是如何在運行時設置不同的音頻文件路徑,因爲我們需要在運行時再次使用'[[AVAudioPlayer alloc] initWithContentsOfURL'部分,這只是我們正在創建的新對象,而不是造成延遲? – Getsy

+0

我試過這個解決方案,但是我觀察到了同樣的延遲問題。 – Getsy

+0

在初始播放之後的後續實例在我的測試中快得多*我可以在iPhone 5上播放具有可接受延遲的鼓樣本。我懷疑您的問題在其他地方(例如,音頻文件是否正確修剪?)但是,您也可以看看'AVPlayer',而不是'AVAudioPlayer' - ['replaceCurrentItemWithPlayerItem:'(http://developer.apple.com/library/ios/documentation/AVFoundation/Reference/AVPlayer_Class/Reference/Reference.html#//apple_ref/occ/instm/AVPlayer/replaceCurrentItemWithPlayerItem :)可讓您更改項目而無需重新實例化。 –