Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to play a video embedded in a UIView?

I tried to play video in a seperate UIView, so that I can leave some room for control utils such as start/stop buttons etc, so I write code like this:
First, I create an iOS project of Single View Application, then I add a View to the storyboard, at last, I append a new UIView based class named VideoView, then I associated the View to the class VideoView, and I wrote some code below:

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

@synthesize moviePlayer;

- (IBAction)playMovie:(id)sender
{
    VideoView *videoView = [[VideoView alloc] initWithFrame:CGRectMake(0, 0, 320, 100)];

    NSString *videoPath = [[NSBundle mainBundle] pathForResource:@"Sanda" ofType:@"mp4"];
    NSURL *videoURL = [NSURL fileURLWithPath:videoPath];
    moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:videoURL];
    //[moviePlayer.view setFrame:CGRectMake(0, 0, 320, 476)];
    [moviePlayer.view setFrame:videoView.bounds];
    [videoView addSubview:moviePlayer.view];

    [moviePlayer prepareToPlay];
    [moviePlayer play];

}

Well, I build and run it, the result is that I can just hear the sound but without seeing the video, I'm really a new comer to iOS,so any tips?

like image 688
liunx Avatar asked Feb 16 '14 16:02

liunx


2 Answers

It seems that you forget to add videoView as subview. Try

[self.view addSubview: videoView];
like image 76
Avt Avatar answered Oct 19 '22 20:10

Avt


[self.view addSubview:videoView];

like image 1
santhu Avatar answered Oct 19 '22 20:10

santhu