2

I am trying to build a music app in Swift in which I got a remote URL of a song and now I want to play the song on button click. I also want a slider or progress bar to display progress of the song according to playback progress.

This is a sample audio file I want to stream.

https://audio-ssl.itunes.apple.com/itunes-assets/AudioPreview113/v4/99/c4/84/99c48467-71dd-0a95-8388-3c5d4d433ee2/mzaf_6642611679343132363.std.aac.p.m4a

Sarthak
  • 33
  • 4
  • Can you tell us what you have tried so far? There are millions of tutorials on how to achieve this. Did you hit any specific problem or don't you know where to start? – fruitcoder Mar 12 '21 at 11:53
  • @fruitcoder Thanks for your reply..I dont know where to start.. – Sarthak Mar 15 '21 at 07:11

1 Answers1

0

Have a look at Apple's AVFoundation framework. There are several classes you can use like AVPlayer, AVAudioPlay, AVQueuePlayer?, it all depends on your requiements.

here's an example to get you started: (you need to enable "Allow Outgoing Connections" in xcode under signing and capabilities for the audio to play.)

for the slider, you find out the duaration of the audio and set the slider maxVaue to duration. using addPeriodicTimeObserver we update the slider in real time with slider.doubleValue = ProgressTime.seconds

you can also be notified when the audio has ended with AVPlayerItemDidPlayToEndTime.

import AVFoundation

var player: AVPlayer! = nil //(needs to be outside of class)
@IBOutlet weak var slider: NSSliderCell!

    let urlToPlay = URL(string:  "https://audio-ssl.itunes.apple.com/itunes-assets/AudioPreview113/v4/99/c4/84/99c48467-71dd-0a95-8388-3c5d4d433ee2/mzaf_6642611679343132363.std.aac.p.m4a")

    let asset = AVAsset(url: urlToPlay!)
    let playerItem = AVPlayerItem(asset: asset)
    if player == nil {
            player = AVPlayer(playerItem: playerItem)
            player.play()
            }
    
    //time observer to update slider.
    player.addPeriodicTimeObserver(forInterval: CMTime(value: 1, timescale: 2), // used to monitor the current play time and update slider
        queue: DispatchQueue.global(), using: { [weak self] (progressTime) in
            
            DispatchQueue.main.async {
                 self!.slider.maxValue = player.currentItem!.asset.duration.seconds
                 self!.slider.doubleValue = progressTime.seconds
            }
    
    })
Lorenzo707
  • 13
  • 3
  • I am using UISlider instead of NSSliderCell and it is showing Value of type 'UISlider' has no member 'maxValue' and Value of type 'UISlider' has no member 'doubleValue' What should i use......and enable "Allow Outgoing Connections" I can't find where to do this.........looking for positive reply @lorenzo707 – Sarthak Mar 15 '21 at 10:43
  • I am not familiar with IOS, but you use maximumValue instead of maxValue and either value or setValue instead of doubleValue, please take a look here for further info. https://developer.apple.com/documentation/uikit/uislider. Allow outgoing connections is in your xcode project under the sandbox options? – Lorenzo707 Mar 15 '21 at 14:08
  • here is a link ive found which shows you a pic of where to enable outgoing connections https://stackoverflow.com/questions/55346223/app-sandbox-capability-missing-in-xcode-project – Lorenzo707 Mar 15 '21 at 14:16
  • hey..it's not working can you send me full code Please – Sarthak Mar 16 '21 at 09:25