简体   繁体   中英

Playing recorded audio with swift

I am currently working through the "Intro to iOS App Development with Swift" course through Udacity. I have copied the code exactly up to the point where we finish recording the audio. This is what they tell you to type:

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {         if (segue.identifier == "stopRecording"){             
let playVC:PlayViewController = segue.destinationViewController as PlayViewController             
let data = sender as RecordedAudio             
playVC.receivedAudio = data         }     } 

However, it returns a compiler error and asks me to add the exclamation point after both as . When I run the program, it says "I found nil while unwrapping an optional". I'm relatively new to programming, so any advice would be helpful.

在此输入图像描述

I just completed this course. So basicly what you are tring to do is passing data between differnet screens. Try to understand what are you tring to achive and it help you to understand the code better.

The main task for the first screen is to record audio, after the task completed, you store all the information about the completed task into an object called RecordedAudio . The information included are var title: String! and var filePathURL: NSURL! . After we store the recording info., we are ready to pass it to the next screen's controller, namely PlayScreenController . First, we have to get access to the conroller and than passing the data.

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?)
{
    // Checking segue if there are more than one
    if (segue.identifier == "stopRecording") {
        // Get the PlayScreenController access
        let playSoundVC = segue.destinationViewController
        as playScreenViewController

        // Cast sender into a RecordedAudio Object
        let data = sender as RecordedAudio

        // Passing the data to PlayScreen
        playSoundVC.audioData = data
    }
}

Now everything is prepared, we can perform the segue . During perform the segue we have to provide the identity of segue and who are the sender.

var recordedAduio = RecordedAduio()
recordedAduio.filePathURL = recorder.url
recordedAudio.title = recorder.url.lastPathComponent    

self.performSegueWithIdentifier("stopRecording", sender: recordedAudio)

Pay attention to the sender object in performSegueWithIdentifier that the reason why we can cast the send in prepareSegue into a RecordedAudio object.

NOTE : Remeber to define var audioData: RecordedAudio! in the PlayScreenViewController otherwise you cannot pass the data to the second screen, becuase there is no variable can hold the data which you are trying to pass.

Leave comments below if you are still not understand.

Happy coding and don't be discouraged by the down vote.

One more thing, typing code is better than coping and pasting.

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