2016-07-28 148 views
0

我試圖記錄正在動態生成本地音頻文件的音頻輸出。iOS:記錄動態音頻播放

所以我們可以說我有一個音頻文件「drums.mp3」,和我目前玩它在這樣一個循環:

var drumSoundEffect: AVAudioPlayer! 
var repeatTimer: NSTimer! 
var url: NSURL! 
let soundInterval: NSTimeInterval = 1 

@IBAction func playSoundTapped(sender: AnyObject) { 
    initSound() 
    initAndStartTimer() 
}  

func initSound() { 
    let path = NSBundle.mainBundle().pathForResource("drums.mp3", ofType: nil)! 
    url = NSURL(fileURLWithPath: path); 
} 

func initAndStartTimer() { 
    repeatTimer = NSTimer.scheduledTimerWithTimeInterval(soundInterval, target: self, selector: #selector(playSound), userInfo: nil, repeats: true) 
    repeatTimer.fire() 
} 

func playSound() { 
    do { 
     let sound = try AVAudioPlayer(contentsOfURL: url) 
     drumSoundEffect = sound 
     sound.play() 
    } catch { 
     //an error occurred... 
    } 
} 

當我嘗試添加AVAudioRecorder功能,現在看來,這只是從麥克風錄音,而不是從我生成的音頻錄音...從我在互聯網上發現的看來,AVFoundation只能從麥克風錄音,所以這不是我的場景的正確選擇。 This示例似乎相當有前途,但我完全不知道如何創建一個工作示例應用程序。我知道這個問題很籠統,但也許有人可以給我一個提示嗎?

非常感謝。

回答

1

我使用AmazingAudioEngine解決了這個問題。像挖掘示例應用程序一樣的魅力

+0

很高興你做到了。這是一個高質量的開源音頻API的例子,值得借鑑。 – user3078414