在iOS应用程序中播放视频:有音频但没有图像

发布于 2024-10-24 03:28:39 字数 664 浏览 1 评论 0原文

我想要在我的 iPhone 应用程序中播放一段短视频。当我使用下面的代码时,我只能听到音频并看到 应用程序的常规视图。我希望视频在此视图之上播放。 对此我能做什么?

    NSBundle *bundle = [NSBundle mainBundle];
    NSString *moviePath = [bundle pathForResource:@"LEADER" ofType:@"mov"];
    NSURL  *movieURL = [[NSURL fileURLWithPath:moviePath] retain];
    MPMoviePlayerController *theMovie = [[MPMoviePlayerController alloc] initWithContentURL:movieURL];
    theMovie.scalingMode = MPMovieScalingModeAspectFill;
    [theMovie play];
    MPMoviePlayerViewController *moviePlayer = [[MPMoviePlayerViewController alloc] initWithContentURL:movieURL];
    [self presentMoviePlayerViewControllerAnimated:moviePlayer];

i want a short video to play in my iphone app. When i use the code below, i only hear the audio and see
the regular view of the app. I want the video to play on top of this view.
What can i do about this?

    NSBundle *bundle = [NSBundle mainBundle];
    NSString *moviePath = [bundle pathForResource:@"LEADER" ofType:@"mov"];
    NSURL  *movieURL = [[NSURL fileURLWithPath:moviePath] retain];
    MPMoviePlayerController *theMovie = [[MPMoviePlayerController alloc] initWithContentURL:movieURL];
    theMovie.scalingMode = MPMovieScalingModeAspectFill;
    [theMovie play];
    MPMoviePlayerViewController *moviePlayer = [[MPMoviePlayerViewController alloc] initWithContentURL:movieURL];
    [self presentMoviePlayerViewControllerAnimated:moviePlayer];

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

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

发布评论

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

评论(3

清醇 2024-10-31 03:28:39

不要混淆 MPMoviePlayerControllerMPMoviePlayerViewController。当您使用 MPMoviePlayerController 时,请像这样使用它(通常用于 iPad 上的嵌入式视频):

MPMoviePlayerController *player =
        [[MPMoviePlayerController alloc] initWithContentURL: myURL];
[player.view setFrame: myView.bounds];  // player's frame must match parent's
[myView addSubview: player.view];
// ...
[player play];

当您使用 MPMoviePlayerViewController 时,然后使用 呈现视频>presentMoviePlayerViewControllerAnimated:通常用于全屏视频)。

Don't mix up MPMoviePlayerController and MPMoviePlayerViewController. When you use MPMoviePlayerController use it like this (typically for embedded videos on the iPad):

MPMoviePlayerController *player =
        [[MPMoviePlayerController alloc] initWithContentURL: myURL];
[player.view setFrame: myView.bounds];  // player's frame must match parent's
[myView addSubview: player.view];
// ...
[player play];

When you use MPMoviePlayerViewController then present the video with presentMoviePlayerViewControllerAnimated: (typically for fullscreen videos).

去了角落 2024-10-31 03:28:39
MPMoviePlayerController *player =
        [[MPMoviePlayerController alloc] initWithContentURL: myURL];
[player.view.frame = self.view.frame];
[self.view addSubview: player.view];
// ...
[player play];
MPMoviePlayerController *player =
        [[MPMoviePlayerController alloc] initWithContentURL: myURL];
[player.view.frame = self.view.frame];
[self.view addSubview: player.view];
// ...
[player play];
绿萝 2024-10-31 03:28:39

对我有用的唯一魔法是

 - (void) playMovie {
    NSURL *url = [NSURL URLWithString: 
        @"http://www.example.com/video.mp4"];
    MPMoviePlayerController *controller = [[MPMoviePlayerController alloc] 
        initWithContentURL:url];

    self.mc = controller; //Super important
    controller.view.frame = self.view.bounds; //Set the size

    [self.view addSubview:controller.view]; //Show the view
    [controller play]; //Start playing
}

在头文件

@property (nonatomic,strong) MPMoviePlayerController* mc;

更多详情

The only magic that worked for me was

 - (void) playMovie {
    NSURL *url = [NSURL URLWithString: 
        @"http://www.example.com/video.mp4"];
    MPMoviePlayerController *controller = [[MPMoviePlayerController alloc] 
        initWithContentURL:url];

    self.mc = controller; //Super important
    controller.view.frame = self.view.bounds; //Set the size

    [self.view addSubview:controller.view]; //Show the view
    [controller play]; //Start playing
}

In Header file

@property (nonatomic,strong) MPMoviePlayerController* mc;

More Details

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