1

私はObjective-Cを書くのが初めてです。そして、私はXcode 4.2から始めます。学習用の例を見つけるのは難しいことがわかりました。

最近、mp4 ビデオを再生する必要があるアプリを書き始めました。次に、MPMovieplayercontroller が役立つことがわかりました。

これらはコードです(さまざまな例から結論付けられています):

-(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];
}

うまくいきません。いいえ 何も表示されません。私のボタンが反応し、正しい機能(再生)を呼び出すと確信しています。

また、プロファイルを使用して実行時にアプリをチェックしました。リークが発見されたとのこと。そして今、私は自分に何ができるかわかりません。

私もスタックオーバーフローは初めてです。不適切な方法で質問する場合は、お知らせください。ありがとうございます

4

1 に答える 1

1

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"); 
    }
于 2011-07-05T09:41:22.803 に答える