iOS AVPlayer won't play - ios

I have the following code set up to play a video in my iOS app, but it just doesn't play. The app compiles and runs, but all I get a red frame where I positioned it. When debugging, I found that the the program doesn't even step into the last line [player play]. Also, the video runs fine in a UIWebView.
NSString *streamingString = [NSString stringWithFormat:#"http://youtu.be/...."];
AVAsset *asset = [AVAsset assetWithURL:[NSURL URLWithString:streamingString]];
AVPlayerItem *playerItem = [[AVPlayerItem alloc] initWithAsset:asset];
AVPlayer *player = [AVPlayer playerWithPlayerItem:playerItem];
AVPlayerLayer *layer = [AVPlayerLayer playerLayerWithPlayer:player];
[layer setPlayer:player];
[layer setFrame:CGRectMake(50, 50, 400, 300)];
[layer setBackgroundColor:[UIColor redColor].CGColor];
[layer setVideoGravity:AVLayerVideoGravityResizeAspectFill];
[self.view.layer addSublayer:layer];
[player play];
I am a new programmer and this is my first post to Stackoverflow, so please excuse me if I have not given enough information or am missing something obvious! Thank you so much!

assetWithURL: is often, but not always, used with a URL that points to a file already on the device, often within the bundle. If you are using an external URL, such as one on the internet, the url must resolve to a video that is in a format that the AVPlayer can understand. A URL to a YouTube web page will not work. In general, you can't play a YouTube video in AVPlayer.
Check out this document from Apple and this video on YouTube.
EDIT:
Some developers have used this library to play a YouTube video in their app. Another option is to use a UIWebView.

Related

Adjust Quality while streaming video with AVPlayer

How can I able to set low or high quality video withstreaming video with AVPlayer?
My code is as follow:
self.videoPlayerItem = [AVPlayerItem playerItemWithURL:videoUrl];
videoPlayer = [AVPlayer playerWithPlayerItem:self.videoPlayerItem];
videoPlayer.actionAtItemEnd = AVPlayerActionAtItemEndNone;
CGRect frame = self.frame;
videoPlayerLayer = [AVPlayerLayer layer];
[videoPlayerLayer setPlayer:videoPlayer];
[videoPlayerLayer setVideoGravity:AVLayerVideoGravityResizeAspectFill];
Thanks for your time
You can lower the video quality/bitrate by setting AVPlayerItem's preferredPeakBitRate.
Re-set it to zero to get the usual "highest quality/bitrate supported by your connection" behaviour.
N.B. A bitrate less than or equal to preferredPeakBitRate must be available for this to work, e.g. in an HLS stream.

Play audio in background (play when using other APP)

I created an app for streaming audio (and send mail as well) but I have a problem, my app will not keep playing after I press the home button.
I use XCODE 6.4 and the mediaplayer.framework to play the stream it is in viewcontroller and I have been activating the background mode Audio & airplay.
Can someone please let me know what I am doing wrong here, I really want to learn it.
code:
NSURL *videoStreamURL = [NSURL URLWithString:#"http://streamlink.m3u"];
_player = [[MPMoviePlayerController alloc] initWithContentURL:videoStreamURL];
_player.view.frame = CGRectMake(0, 530, self.view.frame.size.width, 38);
[self.view addSubview:_player.view];
[_player play];

MPMoviePlayerViewController Not playing video by streaming?

I am using the below code to play the video, but its working fine while using the local url that mean resourse file path. Not working for server url.
player = [[MPMoviePlayerViewController alloc] initWithContentURL:urlvalue];
player.view.frame = CGRectMake(10, 10, 800, 800);
player.moviePlayer.shouldAutoplay=YES;
player.moviePlayer.movieSourceType= MPMovieSourceTypeFile;
[player.moviePlayer setControlStyle:MPMovieControlStyleDefault];
[self.view addSubview:player.view];
[player.moviePlayer prepareToPlay];
[player.moviePlayer play];
Kindly correct me if I missed out something. Thanks in advance.
Pay attention on iOS 9 App transport security. If you haven't set it up, streaming will fail with black empty screen.
Change movieSourceType from File to Streaming :
player.moviePlayer.movieSourceType= MPMovieSourceTypeStreaming;
Note : url should be a streaming url not physical file path

Using AVPlayer to view transparent video

I am trying to view a video witch has an alpha channel (the background is transparent). The only problem is that I don't seem to get how to make the background of the player transparent. I know I have to use AVplayer, but I can't access it's .view property. How can I add it to the subview and add a layer?
NSString *path = [NSString stringWithFormat:#"%#%#", [[NSBundle mainBundle] resourcePath], #"/New Project 5.m4v"];
NSURL *filePath = [NSURL fileURLWithPath:path isDirectory:NO];
moviePlayer = [[AVPlayer alloc] initWithURL:filePath];
AVPlayerLayer* playerLayer = [AVPlayerLayer playerLayerWithPlayer:moviePlayer];
self.playerLayer.frame = self.view.bounds;
moviePlayer.view.alpha = 0.3;
[moviePlayer.layer addSublayer:playerLayer];
[moviePlayer play];
The iOS SDK does not properly support alpha channel video playback. That applies for AVFramework as well as the MediaPlayer Framework. Video material that contains an alpha channel will not work as you expect it to when using Apple's API.
And as you actually show within your code, AVPlayer does not use a UIView as its surface for playing videos but a subclass of CALayer, AVLayer.
You will need to rethink your application design or chose a different playback SDK.

How to reset the content URL for MPMoviePlayerController when playing a video?

I want to reset the content URL for the MPMoviePlayerController object when playing a video.
on a button click, I am setting the content URL like below,
[videoPlayer setContentURL:url];
But i am getting the "Bad Access" error.
Is there a way to change the url when a movie player is already playing a video?
It should stop the previous video and should start the video for the new url.
It means you didn't allocate memory to videoplayer
MPMoviePlayerController * videoplayer = [MPMoviePlayerController alloc] init];
[videoplayer setcontenturl: url];
it will not give you any error.

Resources