I am developing a music player app where the tracks or sounds are played from the url and I m using AVplayer to play the tracks from the url
Basically i want to keep on playing the tracks even user navigated or goes to new other or the application goes to background !! i really don't have a idea how to achieve the same..
these is the demo code I am using to get the track from the URL
NSURL *url = [NSURL URLWithString:#"<#Live stream URL#>];
// You may find a test stream at <http://devimages.apple.com/iphone/samples/bipbop/bipbopall.m3u8>.
self.playerItem = [AVPlayerItem playerItemWithURL:url];
//(optional) [playerItem addObserver:self forKeyPath:#"status" options:0 context:&ItemStatusContext];
self.player = [AVPlayer playerWithPlayerItem:playerItem];
self.player = [AVPlayer playerWithURL:<#Live stream URL#>];
//(optional) [player addObserver:self forKeyPath:#"status" options:0 context:&PlayerStatusContext];
i even wanted to play next track if the current played track is over.
Related
I have a problem when I try to play some remoute audio file.
For this I use AvPlayer.
How you know for play something at first we start preload:
init AVURLAsset
set observer
init avplayer
example:
if (trackUrl) {
self.asset = [AVURLAsset assetWithURL:trackUrl];
NSArray *assetKeys = #[#"playable", #"hasProtectedContent"];
self.playerItem = [AVPlayerItem playerItemWithAsset:self.asset
automaticallyLoadedAssetKeys:assetKeys];
NSKeyValueObservingOptions options =
NSKeyValueObservingOptionOld | NSKeyValueObservingOptionNew;
[self.playerItem addObserver:self
forKeyPath:#"status"
options:options
context:playerItemContext];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(trackDidFinishPlaying:) name:AVPlayerItemDidPlayToEndTimeNotification object:self.playerItem];
[self removePeriodicTimeObserver];
self.player = [AVPlayer playerWithPlayerItem:self.playerItem];
return;
}
So, for audio file which does not have this field at response header "content-disposition:" everything works fine. Preload process is really fast.
example: http://podcast.cbc.ca/mp3/podcasts/asithappens_20160907_50906.mp3
But for audio file which has this field at response header "content-disposition:". Preload process is really long.
example: https://ondemand.npr.org/anon.npr-mp3/npr/fa/2017/01/20170124_fa_01.mp3?orgId=427869011&topicId=1033&d=2209&p=13&story=511387528&t=progseg&e=511402985&seg=1&siteplayer=true&dl=1
It looks like AvPlayer tries to load all file and only after that play this.
Question:
Do you know how I can omit this long preload?
How can I miss this field "content-disposition:" at response?
I am using AVPLayer to player video in UITableView. Video play properly but sometime initially when video played, sound is coming but screen in black. Video is visible after 5-6 second of video is played. I am using following code:
AVURLAsset *asset = [AVURLAsset URLAssetWithURL:url options:nil];
playerItem = [AVPlayerItem playerItemWithAsset:asset];
AVPlayer *avPlayer = [AVPlayer playerWithPlayerItem:playerItem];
self.avPlayer = avPlayer;
__weak CLBAVPlayer *weakSelf = self;
[self.avPlayer addPeriodicTimeObserverForInterval:CMTimeMakeWithSeconds(1.0 / 60.0, NSEC_PER_SEC)
queue:nil
usingBlock:^(CMTime time) {
[weakSelf progress];
}];
self.layer = [AVPlayerLayer playerLayerWithPlayer:self.avPlayer];
self.avPlayer.actionAtItemEnd = AVPlayerActionAtItemEndNone;
Please help me to figure out the issue.
I got the answer for this. Actually the issue was that when I was fetching layer and adding it as sublayer then it was not on main queue. Using dispatch_async with the main queue solved my problem.
I have a AVPlayer that plays video from remote url.
Here is my code:
AVPlayerItem* playerItem = [AVPlayerItem playerItemWithURL:videoUrl];
self.player = [AVPlayer playerWithPlayerItem:playerItem];
self.player.actionAtItemEnd = AVPlayerActionAtItemEndNone;
AVPlayerLayer *playerLayer = [AVPlayerLayer playerLayerWithPlayer:self.player];
playerLayer.frame = playerView.bounds;
[playerView.layer addSublayer:playerLayer];
[self.player play];
When I start to stream I have only less then a second video chunck and then downloading stops and nothing happens.
MPMoviePlayerController and browser plays this video as usual.
I also doubt, that it might be effect of cropping video (because videos without cropping works fine). Here is the guide I use to crod video http://www.one-dreamer.com/cropping-video-square-like-vine-instagram-xcode/
Also clean app with same setup can't play video.
Any ideas? thanks!
Use the AVPlayerItemPlaybackStalledNotification
[[NSNotificationCenter defaultCenter] addObserverForName:AVPlayerItemPlaybackStalledNotification object:nil queue:[NSOperationQueue mainQueue] usingBlock:^(NSNotification *note) {
[weakself performBlockOnMainQueueAfterDelay:1.5 block:^{
[weakself.player play];
}];
}];
I'm using two instances of AVPlayer.
First player audio is working fine but the moment the second audio starts there is a small fraction of time in which the first audio stops.
For first player I have made a singleton class.
This is the second player.
AVAsset *asset = [AVAsset assetWithURL:url];
AVPlayerItem *playerItem = [AVPlayerItem playerItemWithAsset:asset] //AVPlayerItem *playerItem=[AVPlayerItem playerItemWithURL:url];
[self.player replaceCurrentItemWithPlayerItem:playerItem]
[self.player play];
so in the init of a simple Controller i have this code:
self.playerItem = [AVPlayerItem playerItemWithURL:url];
[self.playerItem addObserver:self forKeyPath:#"status" options:0 context:nil];
which should try and load media from the url, right? i'm implemeting
observeValueForKeyPath:ofObject:change:context:
however, this method is never called. Clueless?
playerItem starts working after being assigned to a AVPlayer object (duh)
self.player = [AVPlayer playerWithPlayerItem:self.playerItem];