How to open a youtube private link - mpmovieplayer

NSURL *url=[NSURL URLWithString:#"https://m.youtube.com/watch?v=E9wVJJhDOYQ&t=0s"];
NSLog(#"url is %#", url);
MPMoviePlayerController * _moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:url];
[[_moviePlayer view]setFrame:CGRectMake(50, 100, 200, 200)];
[_moviePlayer setFullscreen:NO animated:YES];
_moviePlayer.scalingMode = MPMovieScalingModeAspectFit;
_moviePlayer.shouldAutoplay= YES;
[[self view] addSubview: [_moviePlayer view]];

Related

How play a .tmp file with MPMoviePlayerController in objective-c

I download a video file .mp4 from server with NSURLSessionDownloadTask but in the delegate didFinishDownloadingToURL:(NSURL *)location, location is a .tmp file like this
file:///private/var/mobile/Applications/BEA4F9F8-D685-468F-B96C-AE7890ACFC5E/tmp/CFNetworkDownload_LQuGoo.tmp
When I try to initialize MPMoviePlayerController console show this error
_itemFailedToPlayToEnd: { kind = 1;new = 2; old = 0;} and screen show black
I researched and I can save the .tmp file and play .mp4 from my directory but I don't want to do that, I want to know if there is a way to play directly with MPMoviePlayerController. My code
NSURLSessionConfiguration *config = [NSURLSessionConfiguration defaultSessionConfiguration];
config.HTTPMaximumConnectionsPerHost = 1;
NSURLSession *downloadSession = [NSURLSession sessionWithConfiguration:config delegate:self delegateQueue:nil];
NSURLSessionDownloadTask * downloadTask =[downloadSession downloadTaskWithURL:self.userRecord.URL completionHandler:nil];
[downloadTask resume];
-(void)URLSession:(NSURLSession *)session downloadTask:(NSURLSessionDownloadTask *)downloadTask didFinishDownloadingToURL:(NSURL *)location {
self.userRecord.locationData = location;
[(NSObject *)self.delegate performSelectorOnMainThread:#selector(fileDownloaderDidFinish:) withObject:self waitUntilDone:NO];
return;
}
-(void)fileDownloaderDidFinish:(FileDownloader *)fileDownloader {
[self setVideoPlayer:[[MPMoviePlayerController alloc] initWithContentURL:fileDownloader.userRecord.locationData]]
;
[self.videoPlayer setControlStyle:MPMovieControlStyleNone];
[self.videoPlayer.view setFrame:CGRectMake(0, 0, 320, 320)];
[self.videoPlayer prepareToPlay];
[self.vwContainerVideo addSubview:self.videoPlayer.view];
[self.vwContainerVideo bringSubviewToFront:self.videoPlayer.view];
[self.vwContainerVideo bringSubviewToFront:self.btPlay];
[self.vwContainerVideo bringSubviewToFront:self.imPlay];
[self.vwContainerThumbnail setHidden:YES];
[AnimationHelper fadeIn:self.vwContainerVideo withDuration:0.5 andWait:0.5];
[self.vwContainerVideo setHidden:NO];
[self.videoPlayer play];
}
My decision was to save file .tmp in a directory and init MPMoviePlayerController with the url from directory.
NSURLSessionConfiguration *config = [NSURLSessionConfiguration defaultSessionConfiguration];
config.HTTPMaximumConnectionsPerHost = 1;
NSURLSession *downloadSession = [NSURLSession sessionWithConfiguration:config delegate:self delegateQueue:nil];
NSURLSessionDownloadTask * downloadTask =[downloadSession downloadTaskWithURL:self.userRecord.URL completionHandler:nil];
[downloadTask resume];
-(void)URLSession:(NSURLSession *)session downloadTask:(NSURLSessionDownloadTask *)downloadTask didFinishDownloadingToURL:(NSURL *)location {
self.userRecord.data = [NSData dataWithContentsOfURL:location];
[self.userRecord.data writeToFile:[rootDirectory stringByAppendingPathComponent:[NSString stringWithFormat:#"%#/%#.%#",self.userRecord.directory,self.userRecord.nameFile, self.userRecord.ext]] options:NSDataWritingWithoutOverwriting error:&error];
self.userRecord.locationData = [NSURL fileURLWithPath:[rootDirectory stringByAppendingPathComponent:[NSString stringWithFormat:#"%#/%#.%#",self.userRecord.directory,self.userRecord.nameFile, self.userRecord.ext]]];
[(NSObject *)self.delegate performSelectorOnMainThread:#selector(fileDownloaderDidFinish:) withObject:self waitUntilDone:NO];
return;
}
-(void)fileDownloaderDidFinish:(FileDownloader *)fileDownloader {
[self setVideoPlayer:[[MPMoviePlayerController alloc] initWithContentURL:fileDownloader.userRecord.locationData]]
;
[self.videoPlayer setControlStyle:MPMovieControlStyleNone];
[self.videoPlayer.view setFrame:CGRectMake(0, 0, 320, 320)];
[self.videoPlayer prepareToPlay];
[self.vwContainerVideo addSubview:self.videoPlayer.view];
[self.vwContainerVideo bringSubviewToFront:self.videoPlayer.view];
[self.vwContainerVideo bringSubviewToFront:self.btPlay];
[self.vwContainerVideo bringSubviewToFront:self.imPlay];
[self.vwContainerThumbnail setHidden:YES];
[AnimationHelper fadeIn:self.vwContainerVideo withDuration:0.5 andWait:0.5];
[self.vwContainerVideo setHidden:NO];
[self.videoPlayer play];
}

Calling UIWebView in UIScrollView

I'm trying to call the two UIWebViews which is declared in other views(AppleViewController & GoogleViewController) by calling UIScrollView in main view controller. ScrollView is displaying but the problem is the UIWebView is not loading to scroll view. What is the problem in my code?
ViewController.h file
- (void)viewDidLoad
{
[super viewDidLoad];
self.scrollView = [[[UIScrollView alloc] init] autorelease];
self.scrollView.delegate = self;
[self.view addSubview:self.scrollView];
CGSize scrollViewContentSize = CGSizeMake(640, 404);
[self.scrollView setContentSize:scrollViewContentSize];
[self.scrollView setPagingEnabled:YES];
self.scrollView.showsHorizontalScrollIndicator = YES;
pageControl = [[[UIPageControl alloc] init] autorelease];
pageControl.frame = CGRectMake(0,0,self.view.frame.size.width,self.view.frame.size.height);
pageControl.numberOfPages = 3;
pageControl.currentPage = 0;
[self.view addSubview:pageControl];
pageControl.backgroundColor = [UIColor blueColor];
AppleViewController *apple=[[AppleViewController alloc]init];
GoogleViewController *google=[[GoogleViewController alloc]init];
[self.scrollView addSubview:apple.view];
[self.scrollView addSubview:google.view];
[scrollView setPagingEnabled:YES];
}
AppleViewController.h
- (void)viewDidLoad
{
[super viewDidLoad];
webview2=[[UIWebView alloc]initWithFrame:CGRectMake(10, 500,750,350)];
[webview2 setBackgroundColor:[UIColor redColor]];
NSString *url2=#"http://www.apple.com";
NSURL *nsurl2=[NSURL URLWithString:url2];
NSURLRequest *nsrequest2=[NSURLRequest requestWithURL:nsurl2];
[webview2 loadRequest:nsrequest2];
[self.view addSubview:webview2];
[webview2 setScalesPageToFit:NO];
webview2.multipleTouchEnabled=YES;
[[webview2 layer] setCornerRadius:10];
[webview2 setClipsToBounds:YES];
[[webview2 layer] setBorderColor:
[[UIColor blackColor] CGColor]];
[[webview2 layer] setBorderWidth:2.75];
[[self view] addSubview:webview2];
[webview2 release];
}
////GoogleViewController.h
- (void)viewDidLoad
{
[super viewDidLoad];
webview=[[UIWebView alloc]initWithFrame:CGRectMake(10, 0,750,350)];
[[webview layer] setCornerRadius:10];
[webview setClipsToBounds:YES];
[[webview layer] setBorderColor:
[[UIColor blackColor] CGColor]];
[[webview layer] setBorderWidth:3];
[[self view] addSubview:webview];
[webview release];
NSString *url=#"http://www.google.com";
NSURL *nsurl=[NSURL URLWithString:url];
NSURLRequest *nsrequest=[NSURLRequest requestWithURL:nsurl];
[webview loadRequest:nsrequest];
[self.view addSubview:webview];
[webview setScalesPageToFit:YES];
webview.multipleTouchEnabled=YES;
[webview goBack];
[webview goForward];
webview.opaque = YES;
webview.backgroundColor = [UIColor clearColor];
}
Change this line's in your code and it will work:
first,create property of your webview inyour .h file
and write this code like this
[self.scrollView addSubview:apple.webview2];
[self.scrollView addSubview:google.webview];
And in your applevc put your webview code in initWithNibName method rather than viewdidload method
same in second vc.
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
webview2=[[UIWebView alloc]initWithFrame:CGRectMake(0, 0,320,640)];
[webview2 setBackgroundColor:[UIColor redColor]];
NSString *url2=#"http://www.apple.com";
NSURL *nsurl2=[NSURL URLWithString:url2];
NSURLRequest *nsrequest2=[NSURLRequest requestWithURL:nsurl2];
[webview2 loadRequest:nsrequest2];
[self.view addSubview:webview2];
[webview2 setScalesPageToFit:NO];
webview2.multipleTouchEnabled=YES;
[[webview2 layer] setCornerRadius:10];
[webview2 setClipsToBounds:YES];
[[webview2 layer] setBorderColor:
[[UIColor blackColor] CGColor]];
[[webview2 layer] setBorderWidth:2.75];
[[self view] addSubview:webview2];
}
return self;
}

UIButton inheritance doesnt show image

I am trying to make an inheritance of a UIButton. Which if clicked will play a sound , scales up. And when finishes playing sound scales back to the original state.
Everything is working except the image is not showing.
The title and background do scale up, and the sound is being played.
Anyone a hint?
soundButton.h
#import <UIKit/UIKit.h>
#import <AVFoundation/AVFoundation.h>
#import <AudioToolbox/AudioToolbox.h>
#interface soundButton : UIButton <AVAudioRecorderDelegate, AVAudioPlayerDelegate>{
BOOL isPlaying;
AVAudioPlayer *audioPlayer;
}
#property (nonatomic)BOOL isPlaying;
#property (nonatomic, strong) AVAudioPlayer *audioPlayer;
- (id)initWithFrame:(CGRect)frame;
-(void)loadSound:(NSString*)audiofile;
-(void)touchUpInside;
#end
soundButton.m
#import "soundButton.h"
#implementation soundButton
#synthesize isPlaying;
#synthesize audioPlayer;
void audioRouteChangeListenerCallback (
void *inUserData,
AudioSessionPropertyID inPropertyID,
UInt32 inPropertyValueSize,
id *inPropertyValue);
-(BOOL)isPlaying{
return audioPlayer.isPlaying;
}
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
[self addTarget:self action:#selector(touchUpInside) forControlEvents:UIControlEventTouchUpInside];
UIImage *image = [UIImage imageNamed:#"kers.png"];
[self setBackgroundImage:image forState:UIControlStateNormal];
}
return self;
}
-(void)loadSound:(NSString*)audiofile{
NSURL *url = [NSURL fileURLWithPath:[[NSBundle mainBundle]
pathForResource:audiofile
ofType:nil]];
NSError *error;
audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:&error];
if (error)
{
NSLog(#"Error in audioPlayer: %#",
[error localizedDescription]);
} else {
audioPlayer.delegate = self;
[audioPlayer setVolume:1.0];
[audioPlayer prepareToPlay];
audioPlayer.currentTime = 0;
}
}
-(void)audioPlayerDidFinishPlaying:(AVAudioPlayer *)player successfully:(BOOL)flag{
[audioPlayer setCurrentTime:0];
self.transform = CGAffineTransformMakeScale(1.5,1.5);
self.alpha = 1.0f;
[UIView beginAnimations:#"fish" context:nil];
[UIView setAnimationDuration:1];
self.transform = CGAffineTransformMakeScale(1.0,1.0);
self.alpha = 1.0f;
[UIView commitAnimations];
self.isPlaying = NO;
}
-(void)touchUpInside{
//Check if something is playing If YES=>stop it
if (self.isPlaying) {
[audioPlayer stop];
[audioPlayer setCurrentTime:0];
} else {
self.transform = CGAffineTransformMakeScale(1.0,1.0);
self.alpha = 1.0f;
[UIView beginAnimations:#"fish" context:nil];
[UIView setAnimationDuration:1];
self.transform = CGAffineTransformMakeScale(1.5,1.5);
self.alpha = 1.0f;
[UIView commitAnimations];
}
// Create an asynchronous background queue
NSOperationQueue *queue = [[NSOperationQueue alloc] init];
[queue addOperationWithBlock:
^{
[audioPlayer play];
}];
}
#end
implemented in ViewdidLoad in the viewController:
UIImage *image = [[UIImage alloc]initWithContentsOfFile:#"cherry.png"];
fishButton = [[soundButton alloc] initWithFrame:CGRectMake(323, 312, 123, 123)];
[fishButton setImage:image forState:UIControlStateNormal];
[fishButton loadSound:#"chime1.mp3"];
[fishButton setTitle:#"fish" forState:UIControlStateNormal];
[fishButton setBackgroundColor:[UIColor yellowColor]];
[fishButton setShowsTouchWhenHighlighted:YES];
[self.view addSubview:fishButton];
replace this
UIImage *image = [[UIImage alloc]initWithContentsOfFile:#"cherry.png"];
with this
UIImage *image = [UIImage imageNamed:#"cherry.png"];
or if you really need to use initWithContentsOfFile use URL link like
NSString *filePath = [[NSBundle mainBundle] pathForResource:#"cherry" ofType:#"png"];
UIImage *image = [[UIImage alloc]initWithContentsOfFile:filePath];

MPMoviePlayerController fullscreen not working

My code plays the video properly but not in full screen.
-----------
|_______ |
|XXXXXXX| |
|XXXXXXX| |
-----------
Its size is about the size of the X filed area.
I am running this on an ipad2.
NSString *urlStr = [[NSBundle mainBundle] pathForResource:#"movie.mp4" ofType:nil];
NSURL *url = [NSURL fileURLWithPath:urlStr];
videoPlayer = [[MPMoviePlayerController alloc] initWithContentURL:url];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(movieFinishedCallback:)
name:MPMoviePlayerPlaybackDidFinishNotification
object:videoPlayer];
[self.view addSubview:videoPlayer.view];
[videoPlayer setFullscreen:YES];
videoPlayer.view.frame = [[UIScreen mainScreen] bounds];
[videoPlayer play];
[super viewDidLoad];
Set the frame of your MPMoviePlayerController to 1024x768.
videoPlayer.view.frame = CGRectMake(0,0,1024,768); //748 if you have status bar.
Hope this is what you were looking for.
*edit: Try this way, it's the one I use.
UIView *tempView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 1024, 768)];
[self.view addSubview:tempView];
playerViewController = [[MPMoviePlayerViewController alloc] initWithContentURL:[NSURL URLWithString:#"www.orangebob.com/files/movie.mp4"]];
[[NSNotificationCenter defaultCenter]
addObserver:self
selector:#selector(movieFinishedCallback:)
name:MPMoviePlayerPlaybackDidFinishNotification
object:[playerViewController moviePlayer]];
playerViewController.view.frame = CGRectMake(0, 0, 1024, 768);
[tempView addSubview:playerViewController.view]; //This *fixes* it, but it's kinda dirty.
MPMoviePlayerController *player = [playerViewController moviePlayer];
[player play];

MPMoviePlayerController does not work in ios 5

i have a problem while working with MPMoviePlayerController. It doesnot work in ios 5 just a black screen to show. Not even throws any exception or any error.
Here is my code:
NSString *path = [[NSBundle mainBundle] pathForResource:#"cavity" ofType:#"mov"];
NSLog(#"%#",path);
_movieController = [[MPMoviePlayerController alloc] initWithContentURL:[NSURL URLWithString:path]];
[_movieController prepareToPlay];
[_movieController setFullscreen:NO];
[_movieController.view setFrame:CGRectMake(10, 25, 287, 213)];
[_movieController setControlStyle:MPMovieControlStyleEmbedded];
[_movieView addSubview:_movieController.view];
[_movieController play];
The _movieController object has been declared in .h file but still not in working condition.
try this
MPMoviePlayerViewController *mp = [[MPMoviePlayerViewController alloc] initWithContentURL:[NSURL URLWithString:url]];
[[mp moviePlayer] prepareToPlay];
[[mp moviePlayer] setUseApplicationAudioSession:NO];
[[mp moviePlayer] setShouldAutoplay:YES];
[[mp moviePlayer] setControlStyle:2];
[[mp moviePlayer] setRepeatMode:MPMovieRepeatModeOne];
[self presentMoviePlayerViewControllerAnimated:mp];