[英]AVPlayer takes lot of time to start playing song from url
我是快速開發的新手。我正在開發音樂播放器應用。
我正在使用AVPlayer從URL流音樂。 播放音樂很好,但是開始播放需要很多時間。 我進行了很多搜索,但沒有一種解決方案適合我。 我在下面添加了代碼im。
func playMusic(){
player?.replaceCurrentItem(with: nil)
let url = NSURL(string: musicURL )
playerItem = AVPlayerItem(url: url! as URL)
player = AVPlayer(playerItem: playerItem!)
playerLayer = AVPlayerLayer(player: player)
if player?.rate != nil{
player?.play()
// player?.playImmediately(atRate: 1.0)
player?.volume = 1.0
isPlayingDownloadedSong = false
didAVStreamingPlayerPlayedOnce = true
isSongLoading = true
//to get duration only once
didGetDurationOfCurrentSong = false
didGetDuration = false
self.startLoader()
musicObserver.playerTimeObserver()
self.didLoaderTurnedOff = false
// this is for player page
NotificationCenter.default.post(name: NSNotification.Name(rawValue: "startLoader"), object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(playerDidFinishPlaying(note:)), name: NSNotification.Name.AVPlayerItemDidPlayToEndTime, object: playerItem)
let audioSession = AVAudioSession.sharedInstance()
do {
try audioSession.setCategory(AVAudioSessionCategoryPlayback)
}
catch{
print("some error happened")
}
self.playBtnInMusicPlayer.setImage( imageLiteral(resourceName: "pause"), for: .normal)
self.musicNameLabelInMusicPlayer.text = songName
}
}
我已經添加了這兩個代碼。
automaticallyWaitsToMinimizeStalling = false
automaticallyWaitsToMinimizeStalling = true
player?.playImmediately(atRate: 1.0)
嘗試了以上兩行
但是他們都不適合我。 請在這件事上給予我幫助
請檢查您是否在啟用了掃描的網絡中。 如果是,請嘗試將播放網址列入白名單。
注意:以上應為評論,但聲譽至關重要:|
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.