简体   繁体   中英

IOs App works on simulator but crashes on device (mainly using AVFoundation)

I'm working on a learning project that works with AVFoundation and deals with recording and manipulating the playback of Audio. The app runs fine on the simulator but it crashes when running on the device.

Mind you, it doesn't crash instantly. It only crashes when I try to manipulate the pitch of the sound to be played.

Also if I just try to play back the audio that was recorded, it won't work, but it also won't crash.

If you want/can help me, the whole project is on github, at https://github.com/pablomarques/Voice-FX

[Project is running on XCode 6.4 and the device is an iPhone 6 running 8.4]

Thanks a whole bunch in advance.

Edit: Adding some of the code here to save people the hassle of having to go through the repo (will keep repo there if you want to replicate it)

It works just fine on the Simulator, it is doing my head in. :(

import UIKit
import AVFoundation

class PlaySoundsViewController: UIViewController {

    var audioPlayer:AVAudioPlayer!
    var receivedAudio:AudioRecording!

    var audioEngine:AVAudioEngine!
    var audioFile:AVAudioFile!

    override func viewDidLoad() {
        super.viewDidLoad()
        if let path = receivedAudio.filePathURL {
            audioPlayer = AVAudioPlayer(contentsOfURL: path, error: nil)
            audioPlayer.enableRate = true

            audioEngine = AVAudioEngine()
            audioFile = AVAudioFile(forReading: path, error: nil)

            println(path)
        }
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    @IBAction func playSlowSound(sender: AnyObject) {

        playSound(0.35)
        println("clicking works")

    }

    @IBAction func playFastSound(sender: AnyObject) {

        playSound(3.0)

    }

    @IBAction func chipMunkSound(sender: AnyObject) {

        playAudioWithVariablePitch(1000.0)

    }

    @IBAction func vaderSound(sender: AnyObject) {

        playAudioWithVariablePitch(-1000.0)

    }

    @IBAction func stopSounds(sender: AnyObject) {

        audioPlayer.stop()
        audioPlayer.currentTime = 0

    }

    func playSound(soundSpeed: Float) {
        audioPlayer.stop()
        audioPlayer.currentTime = 0
        audioPlayer.rate = soundSpeed
        audioPlayer.prepareToPlay()
        audioPlayer.play()
        println("playing")
    }

    func playAudioWithVariablePitch(pitch: Float){

        audioPlayer.stop()
        audioEngine.stop()
        audioEngine.reset()

        var audioPlayerNode = AVAudioPlayerNode()
        audioEngine.attachNode(audioPlayerNode)

        var changePitchEffect = AVAudioUnitTimePitch()
        changePitchEffect.pitch = pitch
        audioEngine.attachNode(changePitchEffect)

        audioEngine.connect(audioPlayerNode, to: changePitchEffect, format: nil)
        audioEngine.connect(changePitchEffect, to: audioEngine.outputNode, format: nil)

        audioPlayerNode.scheduleFile(audioFile, atTime: nil, completionHandler: nil)
        audioEngine.startAndReturnError(nil)

        audioPlayerNode.play()

    }

it shows the green error breakpoint at my AppDelegate.swift file which is weird.

class AppDelegate: UIResponder, UIApplicationDelegate {

and this is the error message i get:

2015-08-20 05:32:55.506 Voice FX[2265:548015] 05:32:55.506 ERROR: [0x19bb13310] >aurioc> 806: failed: -10851 (enable 2, outf< 2 ch, 0 Hz, Float32, non-inter> inf< 2 ch, 0 Hz, Float32, non-inter>) 2015-08-20 05:32:55.509 Voice FX[2265:548015] 05:32:55.508 ERROR: [0x19bb13310] >aurioc> 806: failed: -10851 (enable 2, outf< 2 ch, 44100 Hz, Float32, non-inter> inf< 2 ch, 0 Hz, Float32, non-inter>) 2015-08-20 05:32:55.512 Voice FX[2265:548015] 05:32:55.512 ERROR: [0x19bb13310] >aurioc> 806: failed: -10851 (enable 2, outf< 2 ch, 44100 Hz, Float32, non-inter> inf< 2 ch, 0 Hz, Float32, non-inter>) 2015-08-20 05:32:55.513 Voice FX[2265:548015] 05:32:55.512 ERROR: [0x19bb13310] AVAudioEngineGraph.mm:2417: PerformCommand: error -10851 2015-08-20 05:32:55.517 Voice FX[2265:548015] * Terminating app due to uncaught exception 'com.apple.coreaudio.avfaudio', reason: 'error -10851' * First throw call stack: (0x18563022c 0x1972fc0e4 0x1856300ec 0x183f6e008 0x183fa07ac 0x183fa8a54 0x183fa99dc 0x183f9d9b4 0x183f9da94 0x183f9da94 0x183f9e9f4 0x183fa2140 0x183f9292c 0x183f91f98 0x183f91f28 0x10006c03c 0x10006b20c 0x10006b264 0x18a0e11ec 0x18a0ca2c8 0x18a0e0b88 0x18a0e0814 0x18a0d9d50 0x18a0acf74 0x18a34e124 0x18a0ab488 0x1855e7f8c 0x1855e7230 0x1855e52e0 0x185510f74 0x18ef6b6fc 0x18a112d94 0x10006f3f8 0x1979a6a08) libc++abi.dylib: terminating with uncaught exception of type NSException (lldb) bt

I just ran into this, when I was working with both the older AVAudioSession (for recording a sound file) functionalities AND the new AVAudioEngine (for applying a voice pitch shift).

The error -10851 is kAudioUnitErr_InvalidPropertyValue as seen here https://developer.apple.com/library/ios/documentation/AudioUnit/Reference/AUComponentServicesReference/

My hacky fix right now is just stopping my session when I'm about to use the AudioEngine

ie

self.avSession?.setCategory(AVAudioSessionCategoryPlayAndRecord, error: nil)
self.avSession?.setActive(false, error: nil)

I still get the following error

AVAudioSession.mm:646: -[AVAudioSession setActive:withOptions:error:]: Deactivating an audio session that has running I/O. All I/O should be stopped or paused prior to deactivating the audio session.

But the voice pitch part is working and my app no longer crashes. So I'm guessing that it's just a matter of tidying up how I'm using the two AVAudio tools.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM