IOS YouTube Helper Library - ios

I am trying to use this Library to play youtube video in my ios app, it works for when I load the page for the first time, when I go back to the previous view controller and go into the viewcontroller again which have the youtube video, the video player doesn't show up any more, unless I kill the app and restart again. Not sure Why it is like.
I have a view controller, in the view controller I am using this library to play youtube videos. when I get into this view controller for the first time it all works fine, but when i get to this view controller for the second time, the player not showing up unless I kill the app.
- (void)viewDidLoad
{
[super viewDidLoad];
[self.player loadPlayerWithVideoId:#"M7lc1UVf-VE"];
}
-(YTPlayerView*)player
{
if(!_player)
{
_player = [[YTPlayerView alloc] initWithFrame:CGRectMake(0, 50, self.view.bounds.size.width, 220)];
_player.delegate = self;
_player.autoplay = NO;
_player.modestbranding = YES;
_player.allowLandscapeMode = YES;
_player.forceBackToPortraitMode = YES;
_player.allowAutoResizingPlayerFrame = YES;
_player.playsinline = NO;
_player.fullscreen = YES;
_player.playsinline = YES;
}
return _player;
}

I have used this library to load youtube video and it's working fine for me in the situation you described....
You should declare YTPlayerView as property in .h file like this.
#property(nonatomic, strong) IBOutlet YTPlayerView *playerView;
It is working well for me.

Related

Xcode Objective-C IOS - How to play two videos next to each other?

I want to play two videos next to each other.
When building Mac apps I did this by using a AVPlayer object.
It is a view that you could place inside of your ViewController.
But when I tried to find the AVPlayer object in the object library in an IOS project it didn't show up. Only the AVKit Player View Controller did.
But the AVKit Player View Controller always plays videos full screen in a separate ViewController. I have searched the internet to find a way to display two videos next to each other but so far, I have not found anything useful.
So my question is: Is there any way that I can display two videos next to each other in an IOS application?
// ViewController.h
#import AVFoundation;
#import AVKit;
#property(nonatomic, readonly) AVPlayerItem *currentItem;
// ViewController.m
- (void)viewDidLoad {
[super viewDidLoad];
i=0;
[self videoPlay:[_passdataArr valueForKey:#"videoUrl"]];
}
-(void)videoPlay:(NSArray*)links
{
NSLog(#"1");
//create an AVPlayer
_detailView.hidden=YES;
// NSMutableArray *videolinksArr=[[NSMutableArray alloc] init];
// videolinksArr=_videoLinkArr;
NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:#"%#",links]];
// create a player view controller
player = [AVPlayer playerWithURL:url];
AVPlayerViewController *controller = [[AVPlayerViewController alloc] init];
[self addChildViewController:controller];
[self.view addSubview:controller.view];
controller.view.frame = self.bgimg.frame;
controller.player = player;
controller.showsPlaybackControls = YES;
player.closedCaptionDisplayEnabled = NO;
[player pause];
[player play];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(AllVideoPlay:) name:AVPlayerItemDidPlayToEndTimeNotification object:_currentItem];
}
-(void)AllVideoPlay:(NSNotification*)aNotification
{
NSLog(#"2");
NSArray *temp = _videoLinkArr[i];
[self videoPlay:temp];
i++;
}
But the AVKit Player View Controller always plays videos full screen in a separate ViewController
That's false. If you want the movie view to appear as a subview, make the AVPlayerViewController a child view controller. Start with the Container View Controller in the storyboard and make an Embed segue to an AVPlayerViewController.
Or, you could use AVPlayer without an AVPlayerViewController (in code).
AVPlayer has been available on iOS since iOS 4. AVPlayer is a controller object that manages the playback of media assets with no UI elements directly tied to it, which is why it is not located in the IB object library. Rather you can build your own UI using AVPlayerLayer our use Apple's standard AVPlayerViewController, which includes default controls and standard playback behavior. The fastest way to get two videos playing side-by-side is to embed two instances of AVPlayerViewController inside another view controller. If you are using storyboards your code could be as short as:
#implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
}
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([segue.destinationViewController isKindOfClass:[AVPlayerViewController class]] && [segue.identifier isEqualToString:#"First Player"])
{
AVPlayerViewController *avPlayerViewController = (AVPlayerViewController *)segue.destinationViewController;
avPlayerViewController.player = [AVPlayer playerWithURL: [NSURL URLWithString:#"http://devstreaming.apple.com/videos/wwdc/2016/703rx8zlfedjfom6l93/703/hls_vod_mvp.m3u8"]];
[avPlayerViewController.player play];
} else if ([segue.destinationViewController isKindOfClass:[AVPlayerViewController class]] && [segue.identifier isEqualToString:#"Second Player"])
{
AVPlayerViewController *avPlayerViewController = (AVPlayerViewController *)segue.destinationViewController;
avPlayerViewController.player = [AVPlayer playerWithURL: [NSURL URLWithString:#"http://devstreaming.apple.com/videos/wwdc/2016/703rx8zlfedjfom6l93/704/hls_vod_mvp.m3u8"]];
[avPlayerViewController.player play];
}
}
#end
Or something similar. Where most of the work is done by two container views and embed segues to two AVPlayerViewControllers If you are looking to have custom video UI, you can look at Apple's PIP sample code where they show how you can use key value observing to a video player with custom UI.

UIImagePickerController takePicture Dismisses Controller

I have a custom UIImagePickerController that works nicely, only I am facing one issue that I feel should be fairly simple - I just have yet to figure out the solution.
Upon touching my custom added "photo" button, I have it targeted to the build in takePicture method of the UIIPC. Here is my code
#interface CustomCameraController ()
#end
#implementation CustomCameraController {
CGFloat width, height;
}
- (instancetype)init {
if (self = [super init]) {
width = self.view.frame.size.width, height = self.view.frame.size.height;
self.allowsEditing = YES;
self.sourceType = UIImagePickerControllerSourceTypeCamera;
self.showsCameraControls = NO;
self.toolbarHidden = YES;
[self buildCameraOverlay];
}
return self;
}
- (void)buildCameraOverlay {
UIView *customOverlay = [UIView alloc] ...
// ... Custom overlay setup done here
_takePhoto = [[CustomButton alloc] initWithFrame:CGRectMake(0, 0, heightBottomBar*.5, heightBottomBar*.5)];
_takePhoto.center = CGPointMake(bottomBar.frame.size.width/2, bottomBar.frame.size.height/2);
[_takePhoto setImage:[UIImage imageNamed:#"camera button icon"] forState:UIControlStateNormal];
[_takePhoto addTarget:self action:#selector(takePicture) forControlEvents:UIControlEventTouchUpInside];
[bottomBar addSubview:_takePhoto];
// ...
self.cameraOverlayView = customOverlay;
}
This is done in my custom controller CustomCameraController init call.
The problem is, upon taking the picture via takePicture, the camera shutter goes off, everything works just fine, but the controller dismisses itself. I'm trying to figure out how to stop it from closing immediately after taking the picture, so I can A)present the taken picture, and B) give the user the option to choose the image or cancel and retake another one (returning to the camera)
If anyone knows why this happens or something that I am missing / doing incorrectly please let me know. I'm sure it's a simple answer - just can't seem to figure it out. Thanks!
The most common reason for such a weird behaviour is usually lack of delegate methods (for UIImagePickerController in this case) or their wrong implementation.

iOS7 viewController and MPMoviePlayerViewController Rotation

I try a lot of things, but I still can't rotate only 1 viewControllor in all my app.
I would like to show (or to rotate) in landscape only a vc with a MPMoviePlayerViewController.
Like the videos in Facebook app. The app is only in portrait but video could rotate.
After played the video, application return in portrait mode.
I'm able to ratate but after "done" videoplayer button clicked the view return in landscape mode.
How can I fix this?
Thank you very much.
Create new View controller for Playing videos
Click on Project then click on Target. In General category under Deployment Info enable All the rotations
Now open your Root view controller and Put following lines.give what orientation of your app
code:
-(BOOL)shouldAutorotate
{
return TRUE;
}
-(NSUInteger)supportedInterfaceOrientations
{
if(UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone)
{
return UIInterfaceOrientationMaskPortrait;
}
else
{
return UIInterfaceOrientationMaskLandscapeLeft | UIInterfaceOrientationMaskLandscapeRight;
}
}
4.Now if you used addsubview method to represent your other ViewController's views then no need to apply orintation methods to other controller. But If any vc you have used PresentController method then add orientation methods to that conroller
Create a new UIViewController that you will use for showing a video.
Create a MPMoviePlayerController property
#property (nonatomic, strong) MPMoviePlayerController* moviePlayerController;
Then in viewDidLoad, try this:
- (void)viewDidLoad
{
[super viewDidLoad];
_moviePlayerController = [[MPMoviePlayerController alloc] init];
_moviePlayerController.controlStyle = MPMovieControlStyleFullscreen;
_moviePlayerController.contentURL = [NSURL URLWithString:#"example.com"];
// Rotating the player to landscape position
_moviePlayerController.view.frame = CGRectMake(0.0f,
0.0f,
[UIScreen mainScreen].bounds.size.height,
[UIScreen mainScreen].bounds.size.width);
_moviePlayerController.view.transform = CGAffineTransformMakeRotation(M_PI_2);
_moviePlayerController.view.center = self.view.center;
UIView *playerView = _moviePlayerController.view;
[self.view insertSubview:playerView atIndex:0];
[_moviePlayerController prepareToPlay];
}
Hope it helps.

How do I stop a video from playing when switching to another ViewController?

I have this could which allows me to play a youtube video within my ipad app. However when I switch views, the video does not stop playing in the background. Do I have to add anything to make it stop playing when the views switch?
- (void)viewDidLoad
{
[super viewDidLoad];
NSURL *videoURL = [NSURL URLWithString:#"http://www.youtube.com/embed/123456"];
NSURLRequest *requestObj = [NSURLRequest requestWithURL:videoURL];
UIWebView * YoutubeVideo = [[UIWebView alloc] init];
YoutubeVideo.frame = CGRectMake(176,243,556,335);
[self.view addSubview:YoutubeVideo];
[YoutubeVideo loadRequest:requestObj];
}
-(void)viewWillDisappear:(BOOL)animated
{
[self.YoutubeVideo loadHTMLString:nil baseURL:nil];
}
This should work.When the view disappears, webview have nothing and when view comes back in viewWillAppear instead of viewDidLoad, load the video again, keep the youtubeVideo webview as a property so that you can access it in every method.
Youtube has provided its player called YTPlayer. You can import its library, and use as follow:
In your podfile, you should have this line:
pod "youtube-ios-player-helper", "~> 0.1.4"
In the ViewController.h file you have to:
a. Import the YTPlayer.h file:
#import "YTPlayer.h"
b. Define this within the interface:
#property(nonatomic, strong) IBOutlet YTPlayerView *playerView;
In the ViewController.m file, you have this function:
(void)viewWillDisappear: (BOOL)animated {
[self.playerView stopVideo];
}
It works for me.

YouTube Autoplay Possibilities In IOS

I have created a webView:
videoView.delegate = self;
videoView.userInteractionEnabled = true;
[videoView loadRequest:[[NSURLRequest alloc]initWithURL:[[NSURL alloc] initWithString:#"website"]]];
I have html/javascript to handle the video's loading as well as the autoplay and next video. This all works, but my problem is that IOS autoloads the quicktime - like player, which is also a good thing, but I do not know how to control that, nor do I know how to get the video to play once that has happened. I would like the button that shows up in the player to autoplay. I would like that to happen anytime a new video is loaded.
JS code works like this. I take in a list and that is my playlist. I do not load a playlist from youTube I just make my own list and when end of video is called I load a new video by id. I also autoplay the video which is what gives me the button that IOS supplies anytime a video loads. Also, I am using an iframe to do all of this, with the youTube api's that is how I have control on the js side of things.
Question: Is there a way to control the play via code? If so how, also when the player is already loaded and I load another video how can I get that to autoplay as well?
I found the answer:
If you put webView.mediaPlaybackRequiresUserAction=FALSE; in the webViewDidFinishLoad then it will autoplay and still let you use the other buttons as well. This worked like a charm and suggest using it.
I found the answer: If you put
webView.mediaPlaybackRequiresUserAction=FALSE;
In the webViewDidFinishLoad: then it will autoplay and still let you use the other buttons as well. This worked like a charm and suggest using it.
To check if the playback is finished I used the following method:
First, set the delegate of your webview to self and implement this:
- (void)webViewDidFinishLoad:(UIWebView *)_webView {
UIButton *b = [self findButtonInView:_webView];
[b sendActionsForControlEvents:UIControlEventTouchUpInside]; //autoplay :)
[[[[[UIApplication sharedApplication] keyWindow] subviews] lastObject] setTag:1234]; //tag it
}
then start a timer:
NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:#selector(onTimer:) userInfo:nil repeats:YES];
and check:
BOOL playing;
- (void)onTimer:(NSTimer *)t {
id _topView = [[[[UIApplication sharedApplication] keyWindow] subviews] lastObject];
if ([_topView tag]==1234)
{
playing=YES;
}
else {
if (playing==YES) { //was playing, but now not anymore
//dismiss your view, or do other things.
[t invalidate];
playing=NO;
}
}
}

Resources