//
// playSoundsViewController.swift
// recordVoice
//
// Created by david on 11/08/15.
// Copyright (c) 2015 Tomcorp. All rights reserved.
//
import UIKit
import AVFoundation
class playSoundsViewController: UIViewController {
var audioPlayer:AVAudioPlayer!
var receivedAudio:RecordedAudio!
override func viewDidLoad() {
super.viewDidLoad()
audioPlayer = AVAudioPlayer(contentsOfURL: receivedAudio.filePathUrl, error: nil)
audioPlayer.enableRate = true
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
@IBAction func slowVoice(sender: UIButton) {
audioPlayer.stop()
audioPlayer.rate = 0.5
audioPlayer.currentTime = 0.0
audioPlayer.play()
}
@IBAction func fastVoice(sender: UIButton) {
audioPlayer.stop()
audioPlayer.rate = 2.0
audioPlayer.currentTime = 0.0
audioPlayer.play()
}
@IBAction func stopButtonSounds(sender: UIButton) {
audioPlayer.stop()
}
}
/*
// MARK: - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
// Get the new view controller using segue.destinationViewController.
// Pass the selected object to the new view controller.
}
*/
//
// recordSoundsViewController.swift
// recordVoice
//
// Created by david on 11/08/15.
// Copyright (c) 2015 Tomcorp. All rights reserved.
//
import UIKit
import AVFoundation
class recordSoundsViewController: UIViewController, AVAudioRecorderDelegate {
@IBOutlet weak var recordButton: UIButton!
@IBOutlet weak var recordingLabel: UILabel!
@IBOutlet weak var stopButton: UIButton!
var audioRecorder:AVAudioRecorder!
var recordedAudio:RecordedAudio!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
override func viewWillAppear(animated: Bool) {
stopButton.hidden = true
recordButton.enabled = true
}
@IBAction func recordButton(sender: UIButton) {
recordButton.enabled = false
stopButton.hidden = false
recordingLabel.hidden = false
//record voice
//Inside func recordAudio(sender: UIButton)
let dirPath = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true)[0] as! String
let recordingName = "my_audio.wav"
let pathArray = [dirPath, recordingName]
let filePath = NSURL.fileURLWithPathComponents(pathArray)
println(filePath)
var session = AVAudioSession.sharedInstance()
session.setCategory(AVAudioSessionCategoryPlayAndRecord, error: nil)
audioRecorder = AVAudioRecorder(URL: filePath, settings: nil, error: nil)
audioRecorder.delegate = self
audioRecorder.meteringEnabled = true
audioRecorder.record()
}
func audioRecorderDidFinishRecording(recorder: AVAudioRecorder!, successfully flag: Bool) {
if(flag){
recordedAudio = RecordedAudio()
recordedAudio.filePathUrl = recorder.url
recordedAudio.title = recorder.url.lastPathComponent
self.performSegueWithIdentifier("stopRecording", sender: recordedAudio)
}else {
println("Recording was not successfully")
recordButton.enabled = true
stopButton.hidden = true
}
}
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if (segue.identifier == "StopRecording"){
let playSoundsVC:playSoundsViewController = segue.destinationViewController as! playSoundsViewController
let data = sender as! RecordedAudio
playSoundsVC.receivedAudio = data
}
}
@IBAction func stopButton(sender: UIButton) {
recordButton.enabled = true
recordingLabel.hidden = true
//Inside func stopAudio(sender: UIButton)
audioRecorder.stop()
var audioSession = AVAudioSession.sharedInstance()
audioSession.setActive(false, error: nil)
}
}
//
// RecordedAudio.swift
// recordVoice
//
// Created by david on 13/08/15.
// Copyright (c) 2015 Tomcorp. All rights reserved.
//
import Foundation
class RecordedAudio: NSObject{
var filePathUrl: NSURL!
var title: String!
}
//
// AppDelegate.swift
// recordVoice
//
// Created by david on 11/08/15.
// Copyright (c) 2015 Tomcorp. All rights reserved.
//
import UIKit
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
// Override point for customization after application launch.
return true
}
func applicationWillResignActive(application: UIApplication) {
// Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
// Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
}
func applicationDidEnterBackground(application: UIApplication) {
// Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
// If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}
func applicationWillEnterForeground(application: UIApplication) {
// Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
}
func applicationDidBecomeActive(application: UIApplication) {
// Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}
func applicationWillTerminate(application: UIApplication) {
// Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}
}
這就是它說,在控制檯上後, -5FFA-4964-9AEE-F395D962264F /數據/容器/數據/應用/ A5B7545C-2800-495C-B016-07C3DF010D97 /文檔/ my_audio.wav) 致命錯誤:意外地發現零而展開的可選值崩潰與展開的零值試圖初始化AVAudioPlayer
這些公司的部分其錯誤 – TOMCORP
這些公司的部分其錯誤:audioPlayer = AVAudioPlayer(contentsOfURL:receivedAudio.filePathUrl,錯誤:無) – TOMCORP