How to Open Youtube video in MPMoviePlayerController and Play it - objective-c

i use this method before 1 time. but Now i can not get video from this URL that direct-play YouTube video in MPMoviePlayerController.
URL = http://www.youtube.com/watch?v=JPUWNcGDyvM&feature=player_embedded
- (void)viewDidLoad
{
player = [[MPMoviePlayerController alloc] initWithContentURL:[NSURL URLWithString:[NSString stringWithFormat:#"http://www.youtube.com/watch?v=JPUWNcGDyvM&feature=player_embedded"]]];
[[NSNotificationCenter defaultCenter]
addObserver:self
selector:#selector(movieFinishedCallback:)
name:MPMoviePlayerPlaybackDidFinishNotification
object:player];
//---play movie---
[player prepareToPlay];
[player pause];
player.view.frame = CGRectMake(0, 0, 320, 367);
[self.view addSubview:player.view];
[super viewDidLoad];
}
and My movieFinishedCallback is as Under ...
- (void) movieFinishedCallback:(NSNotification*) aNotification
{
player = [aNotification object];
[[NSNotificationCenter defaultCenter]
removeObserver:self
name:MPMoviePlayerPlaybackDidFinishNotification
object:player];
}

Create a custom uiwebview: please follow instruction
1) Create a interface .h file named "YouTubeView.h"
#import <UIKit/UIKit.h>
#interface YouTubeView : UIWebView
{
}
- (YouTubeView *)initWithStringAsURL:(NSString *)urlString frame:(CGRect)frame mimeSubType:(NSString *)mimeType;
#end
2) in to .m file
#import "YouTubeView.h"
#implementation YouTubeView
- (YouTubeView *)initWithStringAsURL:(NSString *)urlString frame:(CGRect)frame mimeSubType:(NSString *)mimeType
{
NSString *strMimeType;
if([mimeType length]>0)
{
strMimeType = mimeType;
}
else
{
strMimeType =#"x-shockwave-flash"; //#"x-shockwave-mp4";
}
if (self = [super init])
{
// Create webview with requested frame size
self = [[UIWebView alloc] initWithFrame:frame];
// HTML to embed YouTube video
NSString *youTubeVideoHTML = #"<html><head>\
<body style=\"margin:0\">\
<embed id=\"yt\" src=\"%#\" type=\"application/%#\" \
width=\"%0.0f\" height=\"%0.0f\"></embed>\
</body></html>";
// Populate HTML with the URL and requested frame size
NSString *html = [NSString stringWithFormat:youTubeVideoHTML, urlString,strMimeType, frame.size.width, frame.size.height];
// Load the html into the webview
[self loadHTMLString:html baseURL:nil];
}
return self;
}
- (void)dealloc
{
[super dealloc];
}
#end
3) Import the youtubeview file in the controller where you want to play you tube video and create uiview like the code below
YouTubeView *videoVw = [[YouTubeView alloc] initWithStringAsURL:[NSString
stringWithFormat:#"%#",strNormalVdoURL] frame:CGRectMake(0,0,320,480) mimeSubType:#"x-shockwave-
flash"];
[self.view addSubview:videoVw];
[videoVw release];
videoVw = nil;
Note: Video will play in device only so check in device

if possible to open your URL into UIWebview then take a button in your class and redirect it on to the WebsiteViewController class may be it helps u
WebsiteViewController.h ///----
#interface WebsiteViewController : UIViewController <UIWebViewDelegate>
{
IBOutlet UIWebView *webView;
}
#property(nonatomic,retain) IBOutlet UIWebView *webView;
WebsiteViewController.m ///---
-(void)viewDidLoad {
NSURLRequest *request = [[NSURLRequest alloc] initWithURL:#"url"];
[self.webView setScalesPageToFit:YES];
[self.webView loadRequest:request];
[request release];
}

Related

playerItemDidReachEnd moves to wrong ViewController

My app has a start page and a second page with buttons that play videos. When a video is finished the AVPlayer is dismissed with playerItemDidReachEnd. But for some reason it shows the start page and not the second page when the player is dismissed. How can I get it to show the second page instead?
#import "ViewController.h"
#import AVKit;
#import AVFoundation;
#interface ViewController ()
#property(nonatomic, readonly) AVPlayerItem *currentItem;
#end
#implementation ViewController
AVPlayerViewController *playerViewController;
- (void)viewDidLoad {
[super viewDidLoad];
playerViewController = [[AVPlayerViewController alloc] init];
}
-(void)playerItemDidReachEnd:(NSNotification *) notification {
//remove the player
[self dismissViewControllerAnimated:NO completion:nil];
[[NSNotificationCenter defaultCenter] removeObserver:self];
}
- (IBAction)playVideo:(id)sender {
NSURL *url = [NSURL URLWithString:#"https://clips.vorwaerts-gmbh.de/big_buck_bunny.mp4"];
AVURLAsset *asset = [AVURLAsset assetWithURL: url];
AVPlayerItem *item = [AVPlayerItem playerItemWithAsset: asset];
AVPlayer * player = [[AVPlayer alloc] initWithPlayerItem: item];
playerViewController.player = player;
UIScreen *mainScreen = [UIScreen mainScreen];
CGRect viewRect = mainScreen.bounds;
playerViewController.view.frame = viewRect;
playerViewController.showsPlaybackControls = YES;
[self.view addSubview:playerViewController.view];
[player play];
/* When the player item has played to its end time we'll dismiss the controller */
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(playerItemDidReachEnd:)
name:AVPlayerItemDidPlayToEndTimeNotification
object:playerViewController.player.currentItem];
}
#end
If I understand your code correctly, your "second page" is ViewController.
You are adding AVPlayerViewController by [self.view addSubview:playerViewController.view];, so you need to remove this view by calling
[playerViewController.view removeFromSuperview].
After [self dismissViewControllerAnimated:NO completion:nil];, you dismiss whole ViewController, not only AVPlayerViewController.

mp4 video uploaded from android not play on ios

We have an app that allows user to upload videos and images to server.
if i upload a mp4 video from android , it is not playing in ios devices.
this is my code. player status shows up readytoplay but it doesn't load up and no error comes up. vieo is playing fine with web and android.
here is video url : https://justgolive.net/assets/uploads/newrecordings/20919/19777983355a28e15326df6.mp4
NSURL *streamURL = [NSURL URLWithString:_videoUrl];
player = [AVPlayer playerWithURL:streamURL];
AVPlayerLayer *playerLayer = [AVPlayerLayer playerLayerWithPlayer:player];
playerLayer.frame =self.previewView.frame;
playerLayer.videoGravity = AVLayerVideoGravityResizeAspectFill;
//CGRectMake(0, 50, self.view.frame.size.width, self.view.frame.size.height/1.2);
[self.previewView.layer addSublayer:playerLayer];
// [self.playerContainer setUserInteractionEnabled:NO]; //to avoid touch events when activity is on
[self.player addObserver:self forKeyPath:#"status" options:NSKeyValueObservingOptionInitial | NSKeyValueObservingOptionNew context:NULL];
player.actionAtItemEnd = AVPlayerActionAtItemEndNone;
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(playerItemDidReachEnd:)
name:AVPlayerItemDidPlayToEndTimeNotification
object:[player currentItem]];
I have tried with your url of Video and working with below code:
header file like this:
#import <UIKit/UIKit.h>
#interface ViewController : UIViewController
#property (weak, nonatomic) IBOutlet UIView *playerView;
#end
Implementation file like this:
#import "ViewController.h"
#import <AVFoundation/AVFoundation.h>
#interface ViewController ()
#property (nonatomic) AVPlayer *player;
#end
#implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
NSURL *url = [NSURL URLWithString:#"https://justgolive.net/assets/uploads/newrecordings/20919/19777983355a28e15326df6.mp4"];
AVPlayerItem *playerItem = [AVPlayerItem playerItemWithURL:url];
self.player = [AVPlayer playerWithPlayerItem:playerItem];
CALayer *superlayer = self.playerView.layer;
self.player.volume = 20.0;
AVPlayerLayer *playerLayer = [AVPlayerLayer playerLayerWithPlayer:self.player];
[playerLayer setFrame:self.playerView.bounds];
playerLayer.videoGravity = AVLayerVideoGravityResizeAspect;
playerLayer.frame = self.playerView.layer.bounds;
[superlayer addSublayer:playerLayer];
[self.player seekToTime:kCMTimeZero];
[self.player play];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#end

Background Video Objective-c

I want to play a loop background video in my view. I wrote this code :
-(AVPlayerLayer*)playerLayer{
if(!_playerLayer){
// find movie file
NSString *moviePath = [[NSBundle mainBundle] pathForResource:#"fond_login" ofType:#"mp4"];
NSURL *movieURL = [NSURL fileURLWithPath:moviePath];
_playerLayer = [AVPlayerLayer playerLayerWithPlayer:[[AVPlayer alloc]initWithURL:movieURL]];
_playerLayer.frame = CGRectMake(0,0,self.view.frame.size.width, self.view.frame.size.height);
[_playerLayer.player play];
return _playerLayer;
}
return nil; }
-(void)replayMovie:(NSNotification *)notification {
NSLog(#"Finis");
[self.playerLayer.player play];}
And in my viewdidload, I have :
[self.view.layer addSublayer:self.playerLayer];
[[NSNotificationCenter defaultCenter] addObserver: self
selector: #selector(replayMovie:)
name: AVPlayerItemDidPlayToEndTimeNotification
object:nil];
My problem is that the video don't play in loop. I see the video one time and it's finish. I don't know why ..
Thank's
EDIT :
I see another tutorial.
#import MediaPlayer;
#property (nonatomic, strong) MPMoviePlayerController *moviePlayer;
My code in viewDidLoad :
NSURL *videoURL = [[NSBundle mainBundle] URLForResource:#"my_video" withExtension:#"format"];
self.moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:videoURL];
self.moviePlayer.controlStyle = MPMovieControlStyleNone;
self.moviePlayer.scalingMode = MPMovieScalingModeAspectFill;
self.moviePlayer.view.frame = self.view.frame;[self.view insertSubview:self.moviePlayer.view atIndex:0];
[self.moviePlayer play];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(loopVideo) name:MPMoviePlayerPlaybackDidFinishNotification object:self.moviePlayer];
And :
- (void)loopVideo {
[self.moviePlayer play]; }
It's work perfectly.
In your replayMovie: method you need to seek back to the beginning:
- (void)replayMovie:(NSNotification *)notification {
[(AVPlayerItem *)notification.object seekToTime:kCMTimeZero];
}
You're also going to want to set the actionAtItemEnd of the AVPlayer so it doesn't pause:
_playerLayer.player.actionAtItemEnd = AVPlayerActionAtItemEndNone;

MPMoviePlayerController endless loading (filepicker)

I am trying to stream a video uploaded on filepicker, but it keeps loading without playing the video.
The link is: https://www.filepicker.io/api/file/fLayj5bYTWuso1TVNknv
The page is a video/mp4 page, but as you can see there is no extension for the page.
The Code:
#interface RTVideo()
#property (nonatomic,strong) MPMoviePlayerController *moviePlayer;
#end
#implementation RTVideo
UIView *frame;
-(void)play
{
NSNumber *adId = 5 ;
NSString *session = [[NSUserDefaults standardUserDefaults]stringForKey:#"sessionID"];
NSString *type = #"video";
[NetworkManagerCenter retrievePageWithtype:type
Id:adId
session:session
success:^(RT *ad) {
[self playAD];
} failure:^(NSError *error) {
[self removeFromSuperview];
}];
}
-(void)playAD
{
frame = self;
NSURL *movieURL = [NSURL URLWithString: #"https://www.filepicker.io/api/file/fLayj5bYTWuso1TVNknv"];
self.moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:movieURL];
[frame addSubview:self.moviePlayer.view];
self.moviePlayer.fullscreen = YES;
self.moviePlayer.movieSourceType = MPMovieSourceTypeFile;
self.moviePlayer.initialPlaybackTime = -1.0;
[self.moviePlayer prepareToPlay];
[self.moviePlayer play];
}
#end
Is this not working because there is no extension and how can I solve this?

NSNotification touch overlay

4 files for 2 view controllers, firstviewcontroller and MyOverlayView. MyOverlayView just receives touches and sends notification which should be received by firstviewcontroller. Im getting touch event on the overlayview but not receiving the notification at firstviewcontroller. Any ideas?
FVC-Header
#import <UIKit/UIKit.h>
#import <MediaPlayer/MediaPlayer.h>
#import "MyOverlayView.h"
extern NSString *const OverlayViewTouchNotification;
#interface FirstViewController : UIViewController {
MPMoviePlayerViewController *mvc;
MyOverlayView *overlayView;
NSArray *keyArray;
NSMutableDictionary *urlDic;
}
#property (nonatomic, retain) MPMoviePlayerViewController *mvc;
#property (nonatomic, retain) IBOutlet MyOverlayView *overlayView;
#property (nonatomic, retain) NSArray *keyArray;
#property (nonatomic, retain) NSMutableDictionary *urlDic;
-(void)playMovieAtURL:(NSDictionary *)dic:(NSArray *)array:(int) rand;
-(void)overlayViewTouches:(NSNotification *)notification;
-(void)loadArray;
-(void)reset;
#end
FVC-Implementation
#import "FirstViewController.h"
#import "SMWebRequest.h"
NSString * const OverlayViewTouchNotification = #"overlayViewTouch";
#implementation FirstViewController
#synthesize mvc;
#synthesize overlayView;
#synthesize keyArray, urlDic;
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad
{
[super viewDidLoad];
}
-(void)viewDidAppear:(BOOL)animated
{
[self loadArray];
if (!overlayView) {
overlayView = [[MyOverlayView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame]];
}
NSArray *windows = [[UIApplication sharedApplication] windows];
if ([windows count] >= 1)
{
// Locate the movie player window
UIWindow *moviePlayerWindow = [[UIApplication sharedApplication] keyWindow];
// Add our overlay view to the movie player's subviews so it is
// displayed above it.
[moviePlayerWindow addSubview:self.overlayView];
}
//[self.parentViewController.view addSubview:overlayView];
}
-(void)loadArray{
if (!urlDic) {
NSMutableDictionary *tempDic = [NSMutableDictionary new];
[tempDic setObject:#"mp4" forKey:#"euromount_high_res"];
urlDic = [[NSDictionary alloc] initWithDictionary:tempDic];
}
if (!keyArray) {
NSArray *tempArray = [urlDic allKeys];
keyArray = [[NSArray alloc] initWithArray:tempArray];
}
//Random choice
int point = rand() % ([keyArray count]);
//Call play movie
[self playMovieAtURL :urlDic :keyArray :point];
}
-(void)playMovieAtURL:(NSDictionary *)dic:(NSArray *)array:(int)rand
{
NSString *key = [array objectAtIndex:rand];
NSString *path = [[NSBundle mainBundle] pathForResource:key ofType:[dic valueForKey:key]];
if (mvc == nil) { mvc = [[MPMoviePlayerViewController alloc] initWithContentURL:[NSURL fileURLWithPath:path]]; }
mvc.moviePlayer.scalingMode = MPMovieScalingModeAspectFit;
mvc.moviePlayer.shouldAutoplay = TRUE;
mvc.moviePlayer.controlStyle = MPMovieControlStyleNone;
// Register for the playback finished notification.
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(myMovieFinishedCallback:)
name:MPMoviePlayerPlaybackDidFinishNotification
object:nil];
[self presentModalViewController:mvc animated:YES];
// Movie playback is asynchronous, so this method returns immediately.
[mvc.moviePlayer play];
}
// When the movie is done,release the controller.
-(void)myMovieFinishedCallback:(NSNotification*)aNotification
{
[[NSNotificationCenter defaultCenter] removeObserver:self
name:MPMoviePlayerPlaybackDidFinishNotification
object:nil];
//for (UIView *view in self.view.subviews) {
// [view removeFromSuperview];
//}
mvc = nil;
[self loadArray];
}
// Touches in the overlay view (not in the overlay button)
// post the "overlayViewTouch" notification and will send
// the overlayViewTouches: message
- (void)overlayViewTouches:(NSNotification *)notification
{
NSLog(#"screen touched");
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
// Return YES for supported orientations
return YES;
}
- (void)didReceiveMemoryWarning
{
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];
// Release any cached data, images, etc. that aren't in use.
}
- (void)viewDidUnload
{
mvc = nil;
keyArray = nil;
urlDic = nil;
[super viewDidUnload];
}
-(void)reset{
for (UIView *view in self.view.subviews) {
[view removeFromSuperview];
}
mvc = nil;
keyArray = nil;
urlDic = nil;
[self loadArray];
}
- (void)dealloc
{
[super dealloc];
}
#end
MyOverlayView-Header
#import <UIKit/UIKit.h>
#interface MyOverlayView : UIView {
}
- (void)awakeFromNib;
- (void)dealloc;
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event;
#end
MyOverlayView-Implementation
#import "MyOverlayView.h"
#import "FirstViewController.h"
#implementation MyOverlayView
// MPMoviePlayerController will play movies full-screen in
// landscape mode, so we must rotate MyOverlayView 90 degrees and
// translate it to the center of the screen so when it draws
// on top of the playing movie it will display in landscape
// mode to match the movie player orientation.
//
- (void)awakeFromNib
{
CGAffineTransform transform = self.transform;
// Rotate the view 90 degrees.
transform = CGAffineTransformRotate(transform, (M_PI / 2.0));
UIScreen *screen = [UIScreen mainScreen];
// Translate the view to the center of the screen
transform = CGAffineTransformTranslate(transform,
((screen.bounds.size.height) - (self.bounds.size.height))/2,
0);
self.transform = transform;
CGRect newFrame = self.frame;
newFrame.origin.x = 190;
self.frame = newFrame;
}
- (void)dealloc {
[super dealloc];
}
// Handle any touches to the overlay view
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch* touch = [touches anyObject];
if (touch.phase == UITouchPhaseBegan)
{
NSNotificationCenter *nc = [NSNotificationCenter defaultCenter];
[nc postNotificationName:OverlayViewTouchNotification object:nil];
}
}
#end
****EDIT ANSWER*******
I wasnt adding an observer for the notification. Got it all sorted out.
I wasn't adding an observer for the notification. Got it all Sorted out