iphone - 视频不显示在屏幕上

标签 iphone ios objective-c ipad ipod

我在 VideosView.h 中导入了 MPMoviePlayerController。 在我的 VideosView.m 中,我嵌入了以下代码:

    NSString *path2 = [[NSBundle mainBundle] pathForResource:@"myVideo" ofType:@"mp4" inDirectory:@"images"];
    NSLog(@"%@", path2);

    MPMoviePlayerController *myPlayer = [[MPMoviePlayerController alloc] init];
    myPlayer.shouldAutoplay = YES;
    myPlayer.repeatMode = MPMovieRepeatModeOne;
    myPlayer.fullscreen = YES;
    myPlayer.movieSourceType = MPMovieSourceTypeFile;
    myPlayer.scalingMode = MPMovieScalingModeAspectFit;
    myPlayer.contentURL =[NSURL fileURLWithPath:path2];
    myPlayer.view.frame = CGRectMake(0, 0, 500, 500);
    myPlayer.scalingMode = MPMovieScalingModeFill;
    myPlayer.controlStyle = MPMovieControlModeDefault;

    [self addSubview:myPlayer.view];
    [myPlayer play];

我在 Stackoverflow 上找到了这个示例,但无法正常运行。我的视频链接是正确的(是的,它在图像文件夹中)。我的屏幕上出现了一个 500 x 500 像素的黑色矩形(当然是框架),但没有视频在播放。

一些帮助会很棒。

最佳答案

全局定义 myPlayer 对象

在您的代码中,myPlayer 的生命周期以变量的范围结束。如果您在方法内部创建。播放器以该方法的范围结束。

@property(nonatomic, strong) MPMoviePlayerController *myPlayer;

然后从你想要的任何地方初始化,

_myPlayer = [[MPMoviePlayerController alloc] init];

关于iphone - 视频不显示在屏幕上,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16306977/

相关文章:

iphone - 如何在此方法中实现删除/撤消

ios - 有没有办法使用 iOS/Siri 语音转文本功能将语音界面添加到应用程序中?

android - Unity3D 初学者和游戏设计

ios - UIVisualEffectView 创建一个灰色框而不是模糊

objective-c - NSMenuItem 启用项

ios - Xcode 6 和约束 - 变灰的等宽选项

ios - 在保持用户位置居中的同时在 MKMapView 上安装注释

iphone - 什么是 ttstyledlayout

ios - Xcode 框架 : Umbrella header for module 'Test' does not include header 'NewTest.h'

c++ - 对 Cocos2d-x 大小类型的引用不明确