运行时期间发生AVAudioPlayer错误

时间:2016-09-27 06:15:45

标签: ios swift avfoundation recording

我在开发应用时遇到问题。我的storyboard是这样的:tableviewController将转移到ShowViewControllerShowViewController嵌入pageViewController嵌入ContentViewController

实现录音功能后,录音功能正常,用户可以录音和播放。但是当我将ContentViewController解除回tableViewController时。我发现用户无法再次播放录音机。一般来说,当第一次记录时,我还再次确认音频文件在app文件中。但是用户仍然可以再次播放唱片。

P.S :我也确认了recorder.url有文件,但有播放,调试风显示

  

错误域= NSOSStatusErrorDomain代码= 1685348671"(null)"

我想知道为什么它是null?在我解雇ContentViewController之前它仍然可以播放。

以下是我的代码:请帮我解决这个问题,谢谢

===

import UIKit
import AVFoundation

class ContentViewController: UIViewController, AVAudioRecorderDelegate, AVAudioPlayerDelegate {

@IBAction func playAudioRecord(sender: UIButton) {
        delegate?.delegatePlaying(self.recordingButton, stopRecordButton: self.stopRecordingButton)
    }
}
==============================================================================
import UIKit
import AVFoundation
class ShowStoryViewController: UIViewController , UIPageViewControllerDataSource, ContentViewControllerDelegate , AVAudioRecorderDelegate{

var pageViewController : UIPageViewController!
var recorder : AVAudioRecorder?
var player : AVAudioPlayer?
var documentPath : NSURL?

//AVAudio init
        self.documentPath = NSFileManager.defaultManager().URLsForDirectory(.DocumentDirectory, inDomains: .UserDomainMask).first!

    let recordingURL = documentPath!.URLByAppendingPathComponent("Moon Story.m4a")
    //print("\(documentPath)")
    let audioSession = AVAudioSession.sharedInstance()

    do{
    try audioSession.setCategory(AVAudioSessionCategoryPlayAndRecord, withOptions: .DefaultToSpeaker)

    }catch{print("\(error)")}

    let audioSetting : [String : AnyObject] =  [AVFormatIDKey:Int(kAudioFormatMPEG4AAC) ,
                                                AVSampleRateKey:44100.0 ,
                                                AVNumberOfChannelsKey:2 ,
                                                AVEncoderAudioQualityKey : AVAudioQuality.High.rawValue]
    do{
    try self.recorder = AVAudioRecorder(URL: recordingURL, settings: audioSetting)
    }catch{print("\(error)")}

    self.recorder?.delegate = self
    self.recorder?.meteringEnabled = true
    self.recorder?.prepareToRecord()
}        

 //MARK : - ContentViewControllerDelegate

func delegateStartToRecording(){

    if let audioPlayer = self.player {
        if audioPlayer.playing {
         audioPlayer.stop()
        }
    }

    if let audioRecorder = self.recorder{
        if !audioRecorder.recording{
            let audioSession = AVAudioSession.sharedInstance()
            do{try audioSession.setActive(true)}catch{print("\(error)")}
            audioRecorder.record()
        }else{
            audioRecorder.pause()
        }
    }
}

func delegateStopRecording(){
    if let audioRecord = self.recorder{
       audioRecord.stop()
        let audioSession = AVAudioSession.sharedInstance()
        do{try audioSession.setActive(false)}catch{print("\(error)")}
    }
}

//Mark Playing function
func delegatePlaying(recordButton: UIButton,stopRecordButton : UIButton){
    if let audioRecorder = self.recorder{
        if (audioRecorder.recording == false){

            do{
                try self.player = AVAudioPlayer(contentsOfURL: recorder!.url)
                self.player?.prepareToPlay()
            }catch{
                print("\(error)")
            }

            //print(audioRecorder.url)
                       self.player?.play()
        }            
    }       
}

func delegatePausePlaying(){
self.player?.pause()
}

func delegateStopPlaying(){
self.player?.stop()
    }
}

0 个答案:

没有答案