iphone - 如何将视频添加到应用程序

标签 iphone video

我正在尝试制作一个播放视频的应用程序,但我遇到了问题。我听到声音但看不到视频。我真诚地尝试四处寻找解决方案,但所有提示似乎都不起作用。这是代码:

VideoTestViewController.m

#import "VideoTestViewController.h"

@implementation VideoTestViewController

// Implement loadView to create a view hierarchy programmatically, without using a nib.
- (void)loadView {
    NSString *moviePath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"spitfiregrill_iPhone.m4v"];




    MPMoviePlayerController *theMovie = [[MPMoviePlayerController alloc] initWithContentURL:[NSURL fileURLWithPath:moviePath]];

    theMovie.controlStyle = MPMovieControlStyleDefault; 

    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(movieFinishedCallback:) name:MPMoviePlayerPlaybackDidFinishNotification object:theMovie]; 

    [theMovie play];
}



/*
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
    [super viewDidLoad];
}
*/


/*
// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
*/

- (void)didReceiveMemoryWarning {
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];

    // Release any cached data, images, etc that aren't in use.
}

- (void)viewDidUnload {
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}


- (void)dealloc {
    [super dealloc];
}

@end

VideoTestViewController.h

#import <UIKit/UIKit.h>
#import <MediaPlayer/MediaPlayer.h>

@interface VideoTestViewController : UIViewController {

}

@end

最佳答案

- (void)viewDidLoad {
    NSString *url = [[NSBundle mainBundle]
        pathForResource:@"Stock_Footage_Demobroadband"
                 ofType:@"mp4"];
    MPMoviePlayerController *player =
        [[MPMoviePlayerController alloc]
            initWithContentURL:[NSURL fileURLWithPath:url]];

    [[NSNotificationCenter defaultCenter]
        addObserver:self
           selector:@selector(movieFinishedCallback:)
               name:MPMoviePlayerPlaybackDidFinishNotification
             object:player];

    //---play partial screen---
    player.view.frame = CGRectMake(184, 200, 400, 300);
    [self.view addSubview:player.view];

    //---play movie---
    [player play];

        [super viewDidLoad];
    }

- (void) movieFinishedCallback:(NSNotification*) aNotification {
        MPMoviePlayerController *player = [aNotification object];
        [[NSNotificationCenter defaultCenter]
            removeObserver:self
                      name:MPMoviePlayerPlaybackDidFinishNotification
                    object:player];
        [player stop];
        [self.view removeFromSuperView];
        [player autorelease];
    }

全屏模式下的视频 -

- (void)viewDidLoad {
    NSString *url = [[NSBundle mainBundle]
        pathForResource:@"Stock_Footage_Demobroadband"
                 ofType:@"mp4"];

    MPMoviePlayerViewController *playerViewController =
    [[MPMoviePlayerViewController alloc]
        initWithContentURL:[NSURL fileURLWithPath:url]];

    [[NSNotificationCenter defaultCenter]
        addObserver:self
           selector:@selector(movieFinishedCallback:)
               name:MPMoviePlayerPlaybackDidFinishNotification
             object:[playerViewController moviePlayer]];

    [self.view addSubview:playerViewController.view];

    //---play movie---
    MPMoviePlayerController *player = [playerViewController moviePlayer];
    [player play];

    [super viewDidLoad];
}

- (void) movieFinishedCallback:(NSNotification*) aNotification {
    MPMoviePlayerController *player = [aNotification object];
    [[NSNotificationCenter defaultCenter]
        removeObserver:self
                  name:MPMoviePlayerPlaybackDidFinishNotification
                object:player];
    [player stop];
    [self.view removeFromSuperView];
    [player autorelease];
}

关于iphone - 如何将视频添加到应用程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5464022/

相关文章:

video - FFMPEG - avformat_open_input() 总是用 flv 文件返回错误

iphone - 在 ios 中将日历添加到文本字段单击

javascript - 用高分辨率高清视频替换低分辨率视频

javascript - 使用嵌入式 YouTube 视频,创建跳转到特定时间的链接

android - 将第二个音轨添加到 MediaMuxer

testing - JMeter : How to click on a particular element ?

ios - UINavigationBar 自定义背景图像应用于一个 View Controller ,然后影响应用程序中的每个 View Controller

iphone - 如何检查用户是否喜欢页面?

iphone - 释放 CFString

iphone - 我应该担心 NSUserDefaults 中的冲突吗?