Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trial
Stone Preston
42,016 Pointsdisplaying MPMoviePlayer before getting the file
im trying to figure out how to display an MPMoviePlayer BEFORE actually getting the file from parse (currently I have to wait for the file to be obtained and then the moviePlayer is displayed, which creates a lag between when the user taps and the player appears)
Anyone have any suggestions? Here is the current code:
PFFile *videoFile = [self.selectedMedia objectForKey:@"file"];
NSURL *fileURL = [NSURL URLWithString:videoFile.url];
self.moviePlayer.contentURL = fileURL;
[self.moviePlayer prepareToPlay];
[self thumbnailFromVideoAtURL:self.moviePlayer.contentURL];
//add it to the view controller so we can see it
[self.view addSubview:self.moviePlayer.view];
[self.moviePlayer setFullscreen:YES animated:YES];
any ideas Ben Jakuben
Stone Preston
42,016 Pointsyep sure am. thats what my moviePlayer property is, a moviePlayerController (I guess the names a bit confusing)
Marshall Huss
3,504 PointsHave you tried adding it to self.view BEFORE setting the contentURL?
Stone Preston
42,016 Pointsyes it still lags, then shows up
Stone Preston
42,016 Pointsill give it another shot though
3 Answers
Marshall Huss
3,504 PointsTo give the appearance that a video is sitting on a black screen while it's being loaded add a black UIView. In the example below I have a UIView called moviePlayerView, I set it's frame equal to it's bounds, and set the autoresize mask to flexible width and height.
MPMoviePlayerController * movieController = [[MPMoviePlayerController alloc] init];
movieController.view.frame = moviePlayerView.bounds;
movieController.view.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
[moviePlayerView addSubview:movieController.view];
To ensure any type of loading UI is animating make sure you're not loading the video on the main thread.
Stone Preston
42,016 Pointshow would I load the video on a seperate thread?
Marshall Huss
3,504 Points-setContentURL: should already load it in the background. Use the Notifications to be updated on state changes as outlined in the docs.
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(loadStateDidChange:) name: MPMoviePlayerLoadStateDidChangeNotification object:nil];
Stone Preston
42,016 Pointsfigured it out. It was my thumbnail code causing the delay (makes sense, cant show the thumbnail for a video without getting the video first) so i just removed the thumbnail line and it works just how I want it to. cant believe it was this simple
[self.view addSubview:self.moviePlayer.view];
[self.moviePlayer setFullscreen:YES animated:YES];
[self.moviePlayer prepareToPlay];
PFFile *videoFile = [self.selectedMedia objectForKey:@"file"];
NSURL *fileURL = [NSURL URLWithString:videoFile.url];
self.moviePlayer.contentURL = fileURL;
Marshall Huss
3,504 PointsTry setting the movieSourceType = MPMovieSourceTypeStreaming. The docs mention that:
The default value of this property is MPMovieSourceTypeUnknown. This property provides a clue to the playback system as to how it should download and buffer the movie content. If you know the source type of the movie, setting the value of this property before playback begins can improve the load times for the movie content. If you do not set the source type explicitly before playback, the movie player controller must gather this information, which might delay playback.
Stone Preston
42,016 Pointssetting that to streaming actually causes a crash if I select the cell more than once. It also still lags for about 3 seconds. this is what I currently have
//add it to the view controller so we can see it
// self.moviePlayer.movieSourceType = MPMovieSourceTypeStreaming;
[self.view addSubview:self.moviePlayer.view];
[self.moviePlayer setFullscreen:YES animated:YES];
PFFile *videoFile = [self.selectedMedia objectForKey:@"file"];
NSURL *fileURL = [NSURL URLWithString:videoFile.url];
self.moviePlayer.contentURL = fileURL;
[self.moviePlayer prepareToPlay];
[self thumbnailFromVideoAtURL:self.moviePlayer.contentURL];
Marshall Huss
3,504 PointsIt looks like you're loading it from disk, perhaps use MPMovieSourceTypeFile.
Stone Preston
42,016 Pointssame lag, no crash, which is better I guess haha. I really dont understand why adding it to the view before doesnt seem to do anything.
Marshall Huss
3,504 PointsI have an old proof of concept project I made for the old Treehouse iPad App video player that used MPMoviePlayerController. I'll see if I can dig it up.
Stone Preston
42,016 Pointsalright cool thanks
Marshall Huss
3,504 PointsTo clarify, what you really want is a big black rectangle to show before the video is loaded?
Stone Preston
42,016 Pointspretty much. i just want to show the user the app is actually doing something or waiting to obtain some information istead of just freezing for a few seconds. Ive tried using an activity indicator but cant get it to show up (must be because im getting the file url on the main thread)
Marshall Huss
3,504 PointsMarshall Huss
3,504 PointsAre you using a MPMoviePlayerController?