neekeetab / CachingPlayerItem

Play and cache media content simultaneously on your iOS device
MIT License
520 stars 89 forks source link

Is there a way to play the data received ? #36

Closed satyres closed 4 years ago

satyres commented 4 years ago

Hi i 've tried to play to data , and i found a way but the problem is that i want to append data to the player but i can't figure it out. please help regards

smhk commented 4 years ago

@satyres ,

1) are you playing audio or video?

2) what do you mean by "append data to the player" ?

cheers

satyres commented 4 years ago

Hi Thanks for your answser i'm playing an audio radio stream , so i'm using AVAudioPlayer , the problem is after getting some amount of data i call AVAudioPlayer and it play the data , but the problem is that the sound glitches because the AVAudioPlayer get new data here is the code : if audioData.length > 1024*100{ player = try AVAudioPlayer(data: audioData as Data, fileTypeHint: AVFileType.caf.rawValue) player?.play() } so is there a way to append the data to AVAudioPlayer after getting the first byte to be played? regards

smhk commented 4 years ago

@satyres just to be clear ..

are you in fact using CachingPlayerItem,

or, are you asking how to get started with it?

If you are already using CachingPlayerItem, everything is automatic and quite easy!

satyres commented 4 years ago

@smhk Yes i'm already using it ,and i'm getting the bytes and it records i want to play the data received and record at the same time but i can't figure it out regards

smhk commented 4 years ago

to set up your AVPlayer

let pi = CachingPlayerItem(url: .. string url .. )
pi.delegate = self
let avp = AVPlayer(playerItem: pi)
avp.automaticallyWaitsToMinimizeStalling = false

Then CPI will tell you when it is ready to play

func playerItemReadyToPlay(_ playerItem: CachingPlayerItem) {
    print("cpi  says READY TO PLAY")
}

then play ..

func playerItemReadyToPlay(_ playerItem: CachingPlayerItem) {
    print("cpi  says READY TO PLAY")
    avp.play()
}

everything else is automatic. That's the whole thing.

If you have any basic questions about using AVAudioPlayer, etc, should ask on stackoverflow.com, you will get fast and complete answers. Good luck!

satyres commented 4 years ago

@smhk i've tried to play using delegate . i got the message "cpi says READY TO PLAY" but there is no sound after calling avp.play() ! am i missing something ? regards

smhk commented 4 years ago

@satyres I would strongly suggest

(1) just remove CPI from your project

(2) try getting it working normally with just AVAudioPlayer

(3) once it is working perfectly w/ AVAudioPlayer, just drop in CPI

(4) you will really have to ask on stackoverflow.com for general questions about getting AVAudioPlayer working - you can post all your code there and people are very fast/helpful

Don't forget to try playing the URL simply by dropping the URL in the browser on your laptop, to test the URL.

Good luck !

satyres commented 4 years ago

Ok i got the problem. it works ,only with file with limited data (like the example) but for live stream it doesn't work. (i mean endless data) how can i play the live data stream as soon as the buffer get filled regards

smhk commented 4 years ago

investigate

func playerItemReadyToPlay(_ playerItem: CachingPlayerItem)

good luck

satyres commented 4 years ago

Hi thank you for the hint , but i didn't figure it out. i've tried to get the data and then passed to AVAudioPlayer , i can hear sound but it stops and glitches , because i update all the data and i want to append it. AVAudioPlayer doesn't have a method to append Data , so how can we do ? playerItemReadyToPlay works only when the file has been cached , for endless stream how can we do i'm sorry but i'm totally desperate. any help please regards

smhk commented 4 years ago

simply ask on Stackoverflow! your problem does not relate to CPI. good luck !

satyres commented 4 years ago

@smhk i did already , but no answer : https://stackoverflow.com/questions/60950374/how-to-append-data-to-avaudioplayer-live-audio-stream regards