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

发布于 2024-10-27 14:27:15 字数 1849 浏览 0 评论 0原文

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

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

I'm trying to make an app that plays a video, and I'm having problems. I hear the sound but see no video. I have sincerely tried looking around for a solution, but all the tips don't seem to work. Here's the code:

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

皇甫轩 2024-11-03 14:27:15
- (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];
}
- (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];
    }

Video in full screen mode -

- (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];
}
难理解 2024-11-03 14:27:15

有点猜测 - 但这就是我要做的:

VideoTestViewController.h

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

@interface VideoTestViewController : UIViewController
{
    MPMoviePlayerController* m_player;
}

- (void) play;

@end

VideoTestViewController.m

@implementation VideoTestViewController

- (void) viewDidAppear:(BOOL)animated
{   
    [self play];
}

- (void) play
{
    NSURL* url = [[NSBundle mainBundle] URLForResource:@"spitfiregrill_iPhone" withExtension:@"m4v"];

    m_player = [[MPMoviePlayerController alloc] initWithContentURL:url];
    [m_player.backgroundView setBackgroundColor:[UIColor blackColor]];
    [m_player.view setBackgroundColor:[UIColor blackColor]];
    [m_player setControlStyle:MPMovieControlStyleNone];
    [[m_player view] setFrame:[self.view bounds]];

    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(moviePlayBackDidFinish:) name:MPMoviePlayerPlaybackDidFinishNotification object:nil];

    [m_player play];

    [self.view addSubview:[m_player view]];
}

- (void) moviePlayBackDidFinish:(NSNotification*)_notification
{
    [[NSNotificationCenter defaultCenter] removeObserver:self
                                                    name:MPMoviePlayerPlaybackDidFinishNotification
                                                  object:nil];

    [m_player.view removeFromSuperview];
    [m_player stop];
    [m_player release];
    m_player = nil;
}

@end

A bit of guesswork - but this is what I'd do:

VideoTestViewController.h

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

@interface VideoTestViewController : UIViewController
{
    MPMoviePlayerController* m_player;
}

- (void) play;

@end

VideoTestViewController.m

@implementation VideoTestViewController

- (void) viewDidAppear:(BOOL)animated
{   
    [self play];
}

- (void) play
{
    NSURL* url = [[NSBundle mainBundle] URLForResource:@"spitfiregrill_iPhone" withExtension:@"m4v"];

    m_player = [[MPMoviePlayerController alloc] initWithContentURL:url];
    [m_player.backgroundView setBackgroundColor:[UIColor blackColor]];
    [m_player.view setBackgroundColor:[UIColor blackColor]];
    [m_player setControlStyle:MPMovieControlStyleNone];
    [[m_player view] setFrame:[self.view bounds]];

    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(moviePlayBackDidFinish:) name:MPMoviePlayerPlaybackDidFinishNotification object:nil];

    [m_player play];

    [self.view addSubview:[m_player view]];
}

- (void) moviePlayBackDidFinish:(NSNotification*)_notification
{
    [[NSNotificationCenter defaultCenter] removeObserver:self
                                                    name:MPMoviePlayerPlaybackDidFinishNotification
                                                  object:nil];

    [m_player.view removeFromSuperview];
    [m_player stop];
    [m_player release];
    m_player = nil;
}

@end
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文