4
votes

I need to create a Audio Player for streamed URL (m3u8 format). I have created music player using AVPlayer. But I need to show visualizer for streamed song. I have tried different solution but not found any working example of it.

I have created visualizer using AVAudioPlayer(averagePower) but it won't support streamed URL.

Any help to show visualizer for AVPlayer? Thanks in advance.

I have also tried using MYAudioTapProcessor which most of the people suggested, but for streamed URL, tracks always returns null.

Added the MYAudioTapProcessor.h and MYAudioTapProcessor.m in project

//Initialization of player 
let playerItem = AVPlayerItem( url:NSURL( string:"https://bitdash-a.akamaihd.net/content/sintel/hls/playlist.m3u8" ) as! URL )
let audioPlayer: AVPlayer = AVPlayer(playerItem:playerItem)


//Added periodic time observer
    audioPlayer!.addPeriodicTimeObserver(forInterval: CMTimeMakeWithSeconds(1, 1), queue: DispatchQueue.main) { (CMTime) -> Void in
                if audioPlayer!.currentItem?.status == .readyToPlay
                {

                    if let playerItem: AVPlayerItem = audioPlayer!.currentItem {
                        print(playerItem.asset.tracks.count)
                        if (playerItem.asset.tracks) != nil {
                        self.tapProcessor = MYAudioTapProcessor(avPlayerItem: playerItem)
                        playerItem.audioMix = self.tapProcessor.audioMix
                        self.tapProcessor.delegate = self
                        }
                    }

                }
            }


//Delegate callback method for MYAudioTapProcessor
func audioTabProcessor(_ audioTabProcessor: MYAudioTapProcessor!, hasNewLeftChannelValue leftChannelValue: Float, rightChannelValue: Float) {
        print("volume: \(leftChannelValue) : \(rightChannelValue)")
        volumeSlider.value = leftChannelValue
    }

Also tried by adding the "Track" observer.

playerItem.addObserver(self, forKeyPath: "tracks", options: NSKeyValueObservingOptions.new, context:  nil);

Now if play mp3 file, the callback method calls but for m3u8 callback method doesn't call. The main reason for failing m3u8 URL is it always show tracks array count zero whereas for mp3 files tracks array has one item.

1
This sounds like a pretty big task. Can you share what you've completed so far, and maybe someone can offer some guidance from there?Dave Weston
@DaveWeston I have added some code. Please let me know if there is any workaround for fixing the issue. Thanks.miOS
@GirishM, got any way?Chanchal Raj
@GirishM, Did you find any solution?adev
@adev not found any solution yet. For time being, we have skipped the visualization functionality.miOS

1 Answers

0
votes

You cannot get tracks for HLS via AVPLayer. You should use progressive download or local file for getting audio tracks while playing media.