1

All , I need to create a sample audio player and recorder, its not erroring but its not playing and perhaps not recording.. any ideas ?

   @IBAction func Play(AnyObject)
   {
    println ("Play")

    let path = NSBundle.mainBundle().pathForResource("171010", ofType:"m4a")
    let fileURL = NSURL(fileURLWithPath: path)
    player = AVAudioPlayer(contentsOfURL: outputFileURL, error: nil)
    player.prepareToPlay()
    player.delegate = self
    player.play()

    }
    @IBAction func Record(AnyObject)
    {



        filename = "171010.m4a"
        let paths: NSArray = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true)
        var pathComponents = [paths.lastObject as String, filename as String]
        outputFileURL = NSURL.fileURLWithPathComponents(pathComponents)

        settings.setValue(kAudioFormatMPEG4AAC, forKey: AVFormatIDKey)
        settings.setValue(44100.0, forKey: AVSampleRateKey)
        settings.setValue(2, forKey: AVNumberOfChannelsKey)


        recorder = AVAudioRecorder(URL: outputFileURL, settings: settings, error: &error)
        recorder.delegate = self;
        recorder.prepareToRecord()
        recorder.record()

    }

    @IBAction func Stop(AnyObject)
    {
        recorder.stop()
        println("Stop")
    }

and the globals at the top are :

class MainMenu: UIViewController , AVAudioRecorderDelegate , AVAudioPlayerDelegate {

    var filename: String!
    var outputFileURL: NSURL!
    var recorder: AVAudioRecorder!
    var error: NSError?
    var settings: NSMutableDictionary = NSMutableDictionary()
    var player : AVAudioPlayer! = nil

Any ideas ? I don't know if its recording and creating the file. As I am getting URL is nill on the playing function. This is wired up to a storyboard with buttons. Any help within this simple stuff in swift would be brilliant.

I have changed it to this and this should work ? Can anyone advise ?

@IBAction func Play(AnyObject)
   {
    println ("Play")
    var audioPlayer = AVAudioPlayer()
    filename = "171010.m4a"
    let paths: NSArray = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true)
    var pathComponents = [paths.lastObject as String, filename as String]
    outputFileURL = NSURL.fileURLWithPathComponents(pathComponents)
    audioPlayer = AVAudioPlayer(contentsOfURL: outputFileURL, error: nil)
    audioPlayer.prepareToPlay()
    audioPlayer.delegate = self
    audioPlayer.play()
    }

@IBAction func Record(AnyObject)
    {



        filename = "171010.m4a"
        let paths: NSArray = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true)
        var pathComponents = [paths.lastObject as String, filename as String]
        outputFileURL = NSURL.fileURLWithPathComponents(pathComponents)

        settings.setValue(kAudioFormatMPEG4AAC, forKey: AVFormatIDKey)
        settings.setValue(44100.0, forKey: AVSampleRateKey)
        settings.setValue(2, forKey: AVNumberOfChannelsKey)


        recorder = AVAudioRecorder(URL: outputFileURL, settings: settings, error: &error)
        recorder.delegate = self;
        recorder.prepareToRecord()
        recorder.record()



    }
Jason Hough
  • 11
  • 1
  • 3
  • possible duplicate of [Playing a sound with AVAudioPlayer - swift](http://stackoverflow.com/questions/24393495/playing-a-sound-with-avaudioplayer-swift) – `player` needs to be a *property*, otherwise the player is deallocated as soon as the "Play" method returns. – Martin R Sep 05 '14 at 08:29
  • is there anyway i can check the url to see if it is correct on record and play ? – Jason Hough Sep 05 '14 at 09:00

1 Answers1

0

The player needs to be an ivar so it isn't popped off the stack before playing.

Here is a github project with a functioning recorder if you need more info.

Also, the convention is to start function names with a lowercase character.

Gene De Lisa
  • 3,628
  • 1
  • 21
  • 36