I am trying to play videos from url which my web service returns using MPMoviePlayerViewController. A sample url returned is:
http://view.vzaar.com/1128188/player
Which i pass to MPMoviePlayerViewController. Do you think this url is compatible to be played?
I looked at the source of that page, and it's an html page. I did find a html5 source url. Try:
http://view.vzaar.com/1128188/video
Yes, with above url. I just tried it in xcode and I got it to work.
NSURL *url = [[NSURL alloc] initWithString:#"http://view.vzaar.com/1128188/video"];
self.moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:url];
[self.moviePlayer prepareToPlay];
[self.moviePlayer.view setFrame:self.view.bounds]; // player's frame must match parent's
[self.view addSubview: self.moviePlayer.view];
[self.moviePlayer play];
No. It can't be played because it's an HTML page with a flash player embedded. Change your web service to return the video directly, possibly in a different URL.
Related
This question already has answers here:
How to make a custom view resize with the window with Cocoa Auto Layout?
(4 answers)
Closed 2 years ago.
I am very new to MacOS Objective-C Coding. I know there are a lot of solutions for iOS on displaying a YouTube video, but I am attempting with MacOS and Objective C.
So far, I am using WKWebView to display a url. This work fine, except I more wanted to display a youtube video full screen in the window (and just the video, not the whole youtube webpage along with the video).
- (void)windowDidLoad {
WKWebViewConfiguration *theConfiguration = [[WKWebViewConfiguration alloc] init];
WKWebView *webView = [[WKWebView alloc] initWithFrame:self.webNSView.frame configuration:theConfiguration];
webView.navigationDelegate = self;
NSURL *nsurl=[NSURL URLWithString:#"https://www.youtube.com/watch?v=t168mpf0SxU"];
NSURLRequest *nsrequest=[NSURLRequest requestWithURL:nsurl];
[webView loadRequest:nsrequest];
[self.webNSView addSubview:webView];
My solution was to use embed instead of a direct url.
NSURL *nsurl=[NSURL URLWithString:#"https://www.youtube.com/embed/t168mpf0SxU"];
However, I resize the window the video doesn't resize the webview embed with the window. Is there a piece of code I'm missing for that?
My solution was to first set Auto Resize Mask Constraints:
[webView setTranslatesAutoresizingMaskIntoConstraints:NO];
Then to add layout constraints:
[self.webNSView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:#"H:|-[webView]-|" options:0 metrics:nil views:NSDictionaryOfVariableBindings(webView)]];
[self.webNSView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:#"V:|-[webView]-|" options:0 metrics:nil views:NSDictionaryOfVariableBindings(webView)]];
This seems to resize the WKWebView inside a NSView
I had tried to play video using vlckit for mac os x,
using below code but , i am unable to see video, after compiling i am having black screen but no video is playing.
here a.mp4 is my video inside main bundle , i had cheacked that videos path is correct.
let me know what is the issue why i am not able to see video on screen.
// Set up a videoView by hand. You can also do that in the nib file
videoView = [[VLCVideoView alloc] initWithFrame:NSMakeRect(0, 0, 400, 400)];
[self.view addSubview:videoView];
[videoView setAutoresizingMask: NSViewHeightSizable|NSViewWidthSizable];
// Init the player object
player = [[VLCMediaPlayer alloc] initWithVideoView:videoView];
[player setMedia:[VLCMedia mediaWithPath:#"/Users/gurpalrajput/Desktop/demoVlc Player/demoVlc Player/a.mp4"]];
[player play];
That path looks wrong; for a resource inside a macOS application I would expect something like /path/to/Application.app/Contents/Resources/a.mp4. You're not on iOS anymore... ;)
Generally you want to resolve paths to resources inside the application bundle programmatically; for the movie that would be:
NSString* path = [NSBundle.mainBundle pathForResource:#"a" ofType:#"mp4"];
I am displaying a video to my UIWebView from an online Database as:
//-------- Creating webview --------//
UIWebView *webview=[[UIWebView alloc]initWithFrame:CGRectMake(10, 145, 300,190)];
webview.scalesPageToFit = YES;
webview.backgroundColor = [UIColor redColor];
webview.opaque = NO;
[self.view addSubview:webview];
//-------- Loading Video --------//
NSURL *fileURL = [NSURL URLWithString:
[NSString stringWithFormat:#"http://mysite.org/myProject/sample.php?name=123746"]];
NSURL *nsurl=fileURL;
[webview loadRequest:nsrequest];
It is working great. But the video looks like below:
So, Is there is any way to align the video to center of the web view? Anyone please help. thanks.
3 Options.
If the video will always be that size, make the UIWebView just slightly bigger and center that.
Use [webView stringByEvaluatingJavaScriptFromString:#""] to pass in javascript to adjust the css to center it.
If its a webpage you control, add css to the HTML to center the video div in the window
as I was not able to find any good inbuilt solution using Xcode, I have solved the issue with the help of backend and HTML iframes. I've saved the exact url in a data base and using HTML <iframe>, I loaded the video in the webview with tag to the iframes.
I am trying to get a video playing in an iOS applocation, using MPMoviePlayerController to append a movie-view to an existing view when a user pushes a button.
The view appears as a black box on the right position and everything, but nothing happens.
I am expecting the movie to start playing, but nothing happens and no hints from the application on why.
Can anyone see where this goes wrong?
-(IBAction) playButtonClicked:(id)sender
{
NSString* path = [[NSBundle mainBundle] pathForResource:#"sample_mpeg4" ofType:#"mp4"];
NSLog(#"Using videoPath %#", path);
NSURL* url = [NSURL fileURLWithPath:path];
MPMoviePlayerController *player = [[MPMoviePlayerController alloc] initWithContentURL:url];
[self.view addSubview:player.view];
player.view.frame = CGRectMake(10, 10, 300, 220);
[player play];
}
The movie exists and logs the correct path, but still no movie playing.
2012-07-06 11:51:13.492 experiments[84702:12203] Using videoPath /Users/marius/Library/Application Support/iPhone Simulator/5.1/Applications/9799C851-8D2B-4CFE-8CFA-A4C4C954787F/experiments.app/sample_mpeg4.mp4
From what I have read and understood this should be working.
Any hints or suggestions on what to try?
I moved the movie to a HTTP-server (fg.mp4) and are tailing the access-logs.
When clicking the play-button the usual black window appears.
Serverside i find two new entries in the log:
168.122.x.x - - [06/Jul/2012:22:42:33 +0200] "GET /fg.mp4 HTTP/1.1" 304 192 "-" "AppleCoreMedia/1.0.0.9B176 (iPhone Simulator; U; CPU OS 5_1 like Mac OS X; en_us)"
168.122.x.x - - [06/Jul/2012:22:42:33 +0200] "GET /fg.mp4 HTTP/1.1" 206 33304 "-" "AppleCoreMedia/1.0.0.9B176 (iPhone Simulator; U; CPU OS 5_1 like Mac OS X; en_us)"
So the phone gets parts of the movie content, and -apparently- prepares to get the rest when needed.
Is there some obvious parameter for the MPMoviePlayerController I'm missing?
UPDATE: Solution and working code
The problem is caused by ARC and the fact that no reference to the MPMoviePlayerController were kept after calling the method setting up the controller.
The solution: Adding a class-variable to the class and use this to keep a reference to the controller for the lifetime of the movie.
So early in the class declaration (outside any message/function-definitions):
MPMoviePlayerController* mpController;
And the abeformentioned playButtonClicked uses this variable to keep track of the controller:
NSString* path = [[NSBundle mainBundle] pathForResource:#"sample_mpeg4" ofType:#"mp4"];
NSURL* url = [NSURL fileURLWithPath:path];
mpController = [[MPMoviePlayerController alloc] initWithContentURL:url];
mpController.view.frame = CGRectMake(10, 10, 300, 220);
[self.view addSubview:mpController.view];
[mpController play];
As with my answer to MPMoviePlayerController playback terminates before video finishes, this looks to be a memory management (instance lifetime) issue. Assuming you are compiling with ARC, the player variable is released when your playButtonClicked: method returns. At that point the MPMoviePlayerController instance takes its bat and ball, and goes home.
Try assigning the MPMoviePlayerController instance to a strong instance variable or property.
Nothing seems obviously out of place to me. Here are some debugging tips to help narrow down the problem:
try using a URL to an mp4 file served over http---that should tell you whether it's a UI problem or an asset problem.
try inspecting the url for properties such as the file length and confirm that it exactly matches what you expect.
I'm trying to embed a Youtube video using a mixture of this technique, and this Youtube SDK blog post in a universal app. The iPhone version, using the same code, works fine.
On the iPad the video does embed, and it plays fine in it's embedded form, but as soon as you tap the full screen button the app crashes (buttons do not respond, the device does not rotate). The music from the Youtube video keeps playing.
There is no error message logged but the app does register as 'Paused' or hung in xCode. Every time it crashes com.apple.libdispatch-manager is on thread 2. Ask me questions and I'll give you more information about the error, but I'm not sure where to start.
I have tried:
changing the size of the UIWebView frame
the UIWebView is in a UIScrollView, but if I take it out of the scrollview and add it to the view the problem is identical.
changing the video
changing the html that I use in the UIWebView from this to this, with no result
changing the format of the youtube link from ?v=uniqueID to /v/uniqueID
checking the presenting view is the rootviewcontroller (it is, but the video is embedded in a modal, which is not the rootviewcontroller).
I am building for iOS 5.1, this doesn't happen if running on iOS6.
The View that the video is embedded in is modal, both on the phone and the iPad. There's no hackery or unusual things happening in the app.
There seems to be talk of Evernote's app having a similar problem, but I don't know if it is related or not.
For your reference, here is the YouTubeView subclass (which subclasses UIWebView):
- (YouTubeView *)initWithStringAsURL:(NSString *)urlString frame:(CGRect)frame;
{
if (self = [super init])
{
// Create webview with requested frame size
self = [[YouTubeView alloc] initWithFrame:frame];
// HTML to embed YouTube video
// NSString *youTubeVideoHTML = #"<html><head>
// <body style=\"margin:0\">
// <embed id=\"yt\" src=\"%#\"
// type=\"application/x-shockwave-flash\"
// width=\"%0.0f\" height=\"%0.0f\">
// </embed>
// </body>
// </html>";
NSString *youTubeVideoHTML = #"<html><head><meta name = \"viewport\" content = \"initial-scale = 1.0, user-scalable = no, width = %0.0f\"/></head><body style=\"background:#FFF;margin-top:0px;margin-left:0px\"><div><object width=\"%0.0f\" height=\"%0.0f\"><param name=\"movie\" value=\"%#\"></param><param name=\"wmode\" value=\"transparent\"></param><embed src=\"%#\"type=\"application/x-shockwave-flash\" wmode=\"transparent\" width=\"%0.0f\" height=\"%0.0f\"></embed></object></div></body></html>";
// Populate HTML with the URL and requested frame size
// NSString *html = [NSString stringWithFormat:youTubeVideoHTML, urlString, frame.size.width, frame.size.height];
NSLog(#"html:\n %#", youTubeVideoHTML);
NSString *html = [NSString stringWithFormat:youTubeVideoHTML, frame.size.width, frame.size.width, frame.size.height, urlString, urlString, frame.size.width, frame.size.height];
NSLog(#"html:\n %#", html);
// Load the html into the webview
[self loadHTMLString:html baseURL:nil];
}
return self;
}
Modal view on iOS 5.0 and iOS 5.1 is the problem that causes crash on full screen video, AFAIK. They just changed hierarchy of views in that version of iOS (parentViewController and presentingViewController) and that is the aftermath. I asked about it long time ago here and one more same question is here and still no one knows what to do.
First of all, they fixed it in 6.0, I guess, that's good.
For 5.1 we changed design a little and avoided modal view. Do it, if it is possible in your situation.