0
votes

I am trying to stream a sample mp3 from a url. It is a very small sample file. However, when I press the play button nothing happens. Well, no sound plays.

I am rather new to Swift and very much a noob so here is the full View Controller as I am unsure what you might need.

    import UIKit
    import AVKit
    import AVFoundation
    import Foundation

    class ViewController: UIViewController {

        @IBAction func playButton(_ sender: Any) {

            print("pressed")
            let urlstring = "http://cdn.mos.musicradar.com/audio/samples/80s-heat-demos/AM_Eighties08_85-01.mp3"
            let url = NSURL(string: urlstring)
            print("the url = \(url!)")
            downloadFileFromURL(url: url!)

        }



        override func viewDidLoad() {
            // Do any additional setup after loading the view
        }



        func downloadFileFromURL(url:NSURL){
            weak var weakSelf = self
            var downloadTask:URLSessionDownloadTask
            downloadTask = URLSession.shared.downloadTask(with: url as URL, completionHandler: { (URL, response, error) -> Void in
            print("URL = \(URL)")
            weakSelf!.plays(url: URL! as URL)

            })

            downloadTask.resume()

        }



        func plays(url:URL) {

            print("playing \(url)")

            do {

                var playerItem = AVPlayerItem(url: url as URL)

                player = AVPlayer(url : url)

                player.volume = 1.0

                player.play()

            } catch let error as NSError {



                print(error.localizedDescription)

            } catch {

                print("AVAudioPlayer init failed")

            }

        }

    }

in my console I am getting this:

pressed

the url = http://cdn.mos.musicradar.com/audio/samples/80s-heat-demos/AM_Eighties08_85-01.mp3

URL = Optional(file:///private/var/mobile/Containers/Data/Application/C255E8F4-DDBA-43BB-BC33-AF71C08BBDFD/tmp/CFNetworkDownload_tm0O8v.tmp)

playing file:///private/var/mobile/Containers/Data/Application/C255E8F4-DDBA-43BB-BC33-AF71C08BBDFD/tmp/CFNetworkDownload_tm0O8v.tmp

URL = Optional(file:///private/var/mobile/Containers/Data/Application/C255E8F4-DDBA-43BB-BC33-AF71C08BBDFD/tmp/CFNetworkDownload_J1f2LF.tmp)

playing file:///private/var/mobile/Containers/Data/Application/C255E8F4-DDBA-43BB-BC33-AF71C08BBDFD/tmp/CFNetworkDownload_J1f2LF.tmp

Can anyone help me solve this? I just want to be able to play this sound on click of the button from the server not locally.

1
Try to cross check your iPhone device volumeVed Rauniyar
@Ved If you mean to check the volume? I checked on both simulator and phone. Volume was up.JamesG
Were you in silent/vibration mode?Larme
@Larme nope, it was in full volumeJamesG

1 Answers

1
votes

Please try to use this code for you player function.

func plays(url:URL) {

        print("playing \(url)")

        do {
            try AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryPlayback)
            try AVAudioSession.sharedInstance().setActive(true)

            player = try AVAudioPlayer(contentsOf: url)
            guard let player = player else { return }

            player.play()
        } catch let error as NSError {



            print(error.localizedDescription)

        } catch {

            print("AVAudioPlayer init failed")

        }

    }