app freezes by using avplayer - ios

I have used AVPlayer for streaming audio. But it makes the app freeze after some seconds.
AVPlayer *player = [[AVPlayer alloc]initWithURL:[NSURL URLWithString:[songsarray objectAtIndex:songnumber]]];
self.songPlayer = player;
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(playerItemDidReachEnd:)
name:AVPlayerItemDidPlayToEndTimeNotification
object:[songPlayer currentItem]];
[self.songPlayer addObserver:self forKeyPath:#"status" options:NSKeyValueObservingOptionNew context:nil];
[self.songPlayer play];
I am npt getting proper way for streaming audio. If anyone has some idea please help me out.
Thanks to all.

Related

iOS Streaming : timedmetadata not returning the entire value

Trying to play an live audio stream in which I am observing the "timedMetadata"
The normal data items are getting retrieved such as the content title and url..But there is some extra data coming as part of the ICEcast which is basically for playback of Ads.This data however is visible in Charles is not getting populated in AVMetadataItem
for (AVMetadataItem *data in item.timedMetadata) {
NSLog(#"Identifier : %#",data.identifier);
NSLog(#"Value : %#",data.value);
NSLog(#"Datatype : %#",data.dataType);
}
Am I missing something here?The same data is retrievable in Matt's AudioStreamer classes.Can it be done via AVPlayer?Is it coming under a different observer?
1. How can I get this data?
AVPlayerItem *item = [AVPlayerItem playerItemWithURL:[NSURL URLWithString:urlString]];
self.songPlayer = [AVPlayer playerWithPlayerItem:item];
CALayer *superlayer = self.playerView.layer;
AVPlayerLayer *playerLayer = [AVPlayerLayer playerLayerWithPlayer:self.songPlayer];
[playerLayer setFrame:self.playerView.bounds];
playerLayer.videoGravity = AVLayerVideoGravityResizeAspectFill;
[superlayer addSublayer:playerLayer];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(playerItemDidReachEnd:)
name:AVPlayerItemDidPlayToEndTimeNotification
object:[self.songPlayer currentItem]];
[item addObserver:self forKeyPath:#"status" options:0 context:nil];
[item addObserver:self forKeyPath:#"currentItem" options:0 context:nil];
[item addObserver:self forKeyPath:#"timedMetadata" options:0 context:nil];
what are the different observers available for the AVPlayer?

In objective-c how to play multiple video url one after another with out inbetween buffering

In my project I have to play multiple video one after another from server and I have done the following code. My video is playing well but the problem is that while one video is finished I am playing the next video in the -(void)aMoviePlaybackComplete method a black screen is appearing for 1 or 2 seconds. I dont want that and also want to play the video without buffering. Is there any better way to do this? If possible please kindly share valuable code or related links.
Thanks in advance.
-(void)viewDidAppear:(BOOL)animated
{
NSURL * myUrl = [NSURL URLWithString:[arrVideoLink objectAtIndex:0]];
moviePlayer = [[MPMoviePlayerController alloc]initWithContentURL:myUrl];
moviePlayer.controlStyle=MPMovieControlStyleNone;
[vwVideoview addSubview:moviePlayer.view];
moviePlayer.view.frame = CGRectMake(0, 0, vwVideoview.frame.size.width, vwVideoview.frame.size.height);
moviePlayer.view.layer.zPosition = 1;
[moviePlayer prepareToPlay];
[moviePlayer play];
[[NSNotificationCenter defaultCenter] removeObserver:self
name:MPMoviePlayerPlaybackDidFinishNotification
object:moviePlayer];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(aMoviePlaybackComplete:)
name:MPMoviePlayerPlaybackDidFinishNotification
object:moviePlayer];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(loadStateChanged:)
name:MPMoviePlayerPlaybackStateDidChangeNotification
object:moviePlayer];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(nowPlayingMovie:)
name:MPMoviePlayerNowPlayingMovieDidChangeNotification
object:moviePlayer];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(doneButtonClick:)
name:MPMoviePlayerWillExitFullscreenNotification
object:moviePlayer];
}
- (void)aMoviePlaybackComplete:(NSNotification*)notification
{
if (urlIndex+1<=arrVideoLink.count-1)
{
NSURL * myUrl = [NSURL URLWithString:[arrVideoLink objectAtIndex:urlIndex+1]];
moviePlayer = [[MPMoviePlayerController alloc]initWithContentURL:myUrl];
moviePlayer.controlStyle = MPMovieControlStyleNone;
[vwVideoview addSubview:moviePlayer.view];
moviePlayer.view.frame = CGRectMake(0, 0, vwVideoview.frame.size.width, vwVideoview.frame.size.height);
moviePlayer.view.layer.zPosition = 1;
[moviePlayer prepareToPlay];
[moviePlayer play];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(aMoviePlaybackComplete:)
name:MPMoviePlayerPlaybackDidFinishNotification
object:moviePlayer];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(loadStateChanged:) name:MPMoviePlayerPlaybackStateDidChangeNotification object:moviePlayer];
urlIndex++;
}
}else
{
[[NSNotificationCenter defaultCenter]removeObserver:self name:MPMoviePlayerPlaybackDidFinishNotification object:moviePlayer];
[[NSNotificationCenter defaultCenter]removeObserver:self name:MPMoviePlayerPlaybackStateDidChangeNotification object:moviePlayer];
[player.view removeFromSuperview];
}
}

How to reduce delay in playing m3u8 video through mpmovieplayercontroller

Iam playing m3u8 url(i.e from Wowza Streaming Engine) through mpmovieplayercontroller in ipad. But initially I can observe some delay in playing video. Please help me how can I reduce this delay time in playing the video.
Here is my code:
urlStr=#"http://192.168.1.14:1935/live/teststream/playlist.m3u8";
NSURL *url = [NSURL URLWithString:urlStr];
videoPlayer = [[MPMoviePlayerController alloc] initWithContentURL:url];
[videoPlayer prepareToPlay];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(doneButtonClicked:) name:MPMoviePlayerPlaybackDidFinishNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(MPMoviePlayerDidExitFullscreen:) name:MPMoviePlayerDidExitFullscreenNotification object:nil];
videoPlayer.movieSourceType = MPMovieSourceTypeStreaming;
[videoPlayer setShouldAutoplay:YES];
videoPlayer.controlStyle=MPMovieScalingModeAspectFit;
//[videoPlayer prepareToPlay];
[_session.previewView addSubview:videoPlayer.view];
[_session.previewView bringSubviewToFront:videoPlayer.view];
videoPlayer.view.frame = CGRectMake(0, 0,768, 512);
[videoPlayer play];
Please help me to resolve this issue.

MPMoviePlayerController Wont Disappear

I have been trying all day but for some reason on IOS6 im having so many issues with MPMoviePlayerController, it seemed people have had issues but not similar enough to mine:
The movie loads up and plays fine, when it completes the movie. I can loop the movie. The issue arises when I try to close and remove the movie like so:
If I add the controls to the Movie itself and hit the "Done" button, the movie sort of just pauses & never closes!
Any tips/ideas?
_moviePlayerController = [[MPMoviePlayerController alloc] initWithContentURL:fileURL];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(moviePlaybackComplete:)
name:MPMoviePlayerPlaybackDidFinishNotification
object:_moviePlayerController];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(moviePlaybackLoaded:)
name:MPMoviePlayerLoadStateDidChangeNotification
object:_moviePlayerController];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(moviePlaybackComplete:)
name:kMovieOverlayViewTapped
object:nil];
[self.view addSubview:_moviePlayerController.view];
_moviePlayerController.fullscreen = YES;
-
Now on Playback loaded:
- (void)moviePlaybackLoaded:(NSNotification *)notification
{
[[NSNotificationCenter defaultCenter] removeObserver:self
name:MPMoviePlayerLoadStateDidChangeNotification
object:_moviePlayerController];
_moviePlayerController.movieSourceType = MPMovieSourceTypeFile;
_moviePlayerController.controlStyle = MPMovieControlStyleDefault;
[_moviePlayerController prepareToPlay];
[_moviePlayerController.view setFrame:CGRectMake(38, 100, 250, 163)];
[_moviePlayerController play];
}
now to remove the video:
- (void)moviePlaybackComplete:(NSNotification *)notification
{
[[NSNotificationCenter defaultCenter] removeObserver:self
name:MPMoviePlayerPlaybackDidFinishNotification
object:_moviePlayerController];
[[NSNotificationCenter defaultCenter] removeObserver:self
name:MPMoviePlayerDidExitFullscreenNotification
object:_moviePlayerController];
[[NSNotificationCenter defaultCenter] removeObserver:self
name:kMovieOverlayViewTapped
object:nil];
[_moviePlayerController stop];
[_moviePlayerController.view removeFromSuperview];
_moviePlayerController = nil;

iOS - Playing more than one video

I am using this code with the MediaPlayer framework to play a video:
-(void)GrommeVideoExcerpt1
{
NSURL *url1 = [NSURL fileURLWithPath:[[NSBundle mainBundle]
pathForResource:#"GrommeVideoExcerpt1" ofType:#"mp4"]];
grommePlayer1 = [[MPMoviePlayerController alloc]
initWithContentURL:url1];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(moviePlayBackDidFinish:)
name:MPMoviePlayerPlaybackDidFinishNotification
object:grommePlayer1];
grommePlayer1.controlStyle = MPMovieControlStyleDefault;
grommePlayer1.shouldAutoplay = YES;
[self.view addSubview:grommePlayer1.view];
[grommePlayer1 setFullscreen:YES animated:YES];
}
But when I play one video, then navigate to another video and try to play it, the app crashes. It gives me this error:
http://pastebin.com/nUGLXEAi
The problem here is you are adding a notification for when the video finishes playing, and that notification triggers a method you do not have (moviePlayBackDidFinish:) causing the crash.
You could implement this method or remove the notification, depends what you want to do after the video is done playing i.e. remove a viewController that plays the video, etc.
//Register notification
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(moviePlayBackDidFinish:)
name:MPMoviePlayerPlaybackDidFinishNotification
object:nil];
//Then the method ...
-(void) moviePlaybackDidFinish:(NSNotification *)aNotification{
[grommePlayer1.view removeFromSuperview];
grommePlayer1 = nil;
}

Resources