开发者

MPMoviePlayer example does not work

开发者 https://www.devze.com 2023-03-17 02:58 出处:网络
I am new to writing Objective-C. And i start with the Xcode 4.2. I found that it is difficult to find examples for learning.

I am new to writing Objective-C. And i start with the Xcode 4.2. I found that it is difficult to find examples for learning.

Recently, I have started writing my app which need to play mp4 vi开发者_Go百科deo. Then i find MPMovieplayercontroller can help.

These are the code (concluded from different examples):

-(void)play  // a function that trigger by pressing a button
{
    [self.view addSubview:self.player.view];
    [self.player play];
}


- (void)viewDidLoad
{
    [super viewDidLoad];

    self.view.backgroundColor = [UIColor greenColor];
    screen.backgroundColor = [UIColor redColor];

    NSString *videoFilePath = [[NSBundle mainBundle] pathForResource:@"ted" ofType:@"mp4"];

    if (videoFilePath == NULL)
    {
        return;
    }

    NSURL *videoURL =[NSURL fileURLWithPath:videoFilePath];
    self.player.view.frame = CGRectMake(300,300, 400,400);
    self.player = [[MPMoviePlayerController alloc] initWithContentURL:videoURL];
}

It just doesn't work. No nothing is displayed. i am sure that my button give response and call the correct function (play).

I also checked the apps at runtime using the profile. and it said leak is discovered. And now i have no idea of what i can do.

I am new to stackoverflow too. If i ask in an improper way, please let me know.Thank you


I was facing the same problem as MPMoviPlayerController is working fine for prior or equivalent version of 4.1.x I have almost solved the problem to play video... Following is the code,

    - (void)viewDidLoad 
    {
        [super viewDidLoad];    
        NSString *strURL = @"http://iphonetv.orange.mu:1935/live/ndtvgtimes.stream/playlists.m3u8";
        NSURLRequest *urlReq = [[NSURLRequest alloc] initWithURL:[NSURL URLWithString:strMovieURL]];    
        [myWebView loadRequest:urlReq];     
    }

    - (BOOL)webView:(UIWebView *)myWebView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
    {   
        NSURL *url = [request URL]; 
        if ([[[url path] pathExtension] isEqualToString:@"mp4" ] || [[[url path] pathExtension] isEqualToString:@"m4v" ] || [[[url path] pathExtension] isEqualToString:@"m3u8" ]) 
        {
            //video files are .mp4 or m4v, stream indexes are m3u8
            //it's a movie, go play!
            [self playMovieAtURL:url];
            return YES;
        }
        else 
        {
            return [super webView:myWebView shouldStartLoadWithRequest:request navigationType:navigationType];
        }
    }

    -(void)playMovieAtURL:(NSURL*)theURL 
    {
        theMovie = [[MPMoviePlayerController alloc] initWithContentURL:theURL];    
        theMovie.scalingMode = MPMovieScalingModeAspectFit;
        [[NSNotificationCenter defaultCenter] addObserver:self selector: @selector(myMovieFinishedCallback:) name:MPMoviePlayerPlaybackDidFinishNotification object:theMovie];  
        [theMovie play];
    }

    -(void)myMovieFinishedCallback:(NSNotification*)aNotification
    {
         MPMoviePlayerController* theMovie1 = [aNotification object];
        //theMovie = [aNotification object];
        [[NSNotificationCenter defaultCenter] removeObserver:self name:MPMoviePlayerPlaybackDidFinishNotification   object:theMovie1];
        [theMovie stop];
        float version = [[[UIDevice currentDevice] systemVersion] floatValue];
        if (version >= 3.0) 
        {
            // iPhone 3.0 code here
            // theMovie.initialPlaybackTime = -1.0; //Breaks on 2.x compiling
            [theMovie setInitialPlaybackTime: -1.0]; //Only gives a warning on 2.x :)
            }

        [theMovie release];
        NSLog(@"Go STOP received"); 
    }
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号