小编Bic*_*ick的帖子

斯威夫特,按下按钮时如何播放声音

我是编程的初学者,并开始学习Swift来制作钢琴应用程序以获得乐趣.

按下按钮时播放声音很麻烦.我搜索了一些网站,但我太初学了解......

http://www.tmroyal.com/playing-sounds-in-swift-avaudioplayer.html http://www.rockhoppertech.com/blog/swift-avfoundation/

你可以告诉我如何在按下"PainoC"按钮时播放我的"C.m4a"声音吗?

这是我的"view controller.swift".

import UIKit

class ViewController: UIViewController {
    override func viewDidLoad() {
        super.viewDidLoad()   
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    }

    @IBAction func PianoC(sender: AnyObject) {
    }
}
Run Code Online (Sandbox Code Playgroud)

swift

14
推荐指数
3
解决办法
3万
查看次数

如何用AVAudioPCMBuffer播放声音

我无法用AVAudioPCMBuffer播放声音(尽管我可以使用AVAudioFile).我收到了这个错误.

错误:AVAudioBuffer.mm:169: - [AVAudioPCMBuffer initWithPCMFormat:frameCapacity:]:必需条件为false:isCommonFormat

这是我下面的代码,我非常感谢你的帮助.

import UIKit
import AVFoundation

class ViewController: UIViewController {

let audioEngine: AVAudioEngine = AVAudioEngine()
let audioFilePlayer: AVAudioPlayerNode = AVAudioPlayerNode()

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.

    audioEngine.attachNode(audioFilePlayer)

    let filePath: String = NSBundle.mainBundle().pathForResource("test", ofType: "mp3")!
    let fileURL: NSURL = NSURL(fileURLWithPath: filePath)!
    let audioFile = AVAudioFile(forReading: fileURL, error: nil)
    let audioFormat = audioFile.fileFormat
    let audioFrameCount = UInt32(audioFile.length)
    let audioFileBuffer = AVAudioPCMBuffer(PCMFormat: audioFormat, frameCapacity: audioFrameCount)

    var mainMixer …
Run Code Online (Sandbox Code Playgroud)

swift ios8 avaudiopcmbuffer avaudiofile avaudioplayernode

7
推荐指数
1
解决办法
5975
查看次数

如何使用AVAudioPlayerNode显示歌曲的当前时间和持续时间

我正在制作一个使用AVAudioPlayerNode的音频播放器并播放一些有效(延迟,音高变化,速率变化等)的歌曲,但我仍然坚持显示歌曲的当前时间和持续时间.AVAudioPlayerNode似乎没有AVAudioPlayer.currentTime或.duration.

如果您可以通过使用AVAudioPlayerNode帮助我显示当前时间和持续时间,我真的很感激.

avfoundation swift avaudioplayernode

6
推荐指数
5
解决办法
4017
查看次数

插入或插入耳机时,AVAudioEngine崩溃

我正在iPhone6 PlusaudioApp上进行测试,插入或拔出耳机后会崩溃。 ,,在我的应用程序使用。AVAudioEngineAVAudioMixerAVAudioPlayerNode

不带耳机的启动应用程序可以播放iPhone扬声器的声音。

带有耳机的启动应用程序可以播放耳机的声音。

在以下情况下会发生错误。启动不带耳机的应用,并播放扬声器的声音。然后,如果我连接耳机,则耳机没有声音。之后拔下耳机的插头,即使扬声器也没有声音。当我触摸屏幕时,audioApp崩溃。

与使用耳机启动应用程序相同,然后将我的耳机拔出。

如何解决此错误?非常感谢您的建议。

这是我收到的错误消息。

Terminating app due to uncaught exception 'com.apple.coreaudio.avfaudio', reason: 'required condition is false: _engine->IsRunning()'
*** First throw call stack:
(0x184d61e48 0x1954a80e4 0x184d61d08 0x1836d26c0 0x1836fa328 0x1836f975c 0x1000d03c0 0x1000d0728 0x1895450f8 0x18952e22c 0x189544a94 0x189544720 0x18953dc74 0x18951138c 0x1897b01b4 0x18950f8f4 0x184d1a0e8 0x184d1938c 0x184d1743c 0x184c451f4 0x18ddd35a4 0x189576784 0x1000d5614 0x1000d5654 0x195b16a08)
libc++abi.dylib: terminating with uncaught exception of type NSException
Run Code Online (Sandbox Code Playgroud)

swift

4
推荐指数
1
解决办法
3031
查看次数