ios - 从 NSUserDefaults 播放视频

标签 ios objective-c mpmovieplayercontroller

我已将路径保存到 NSUserDefaults,但当我尝试将其拉出并在电影播放器​​中显示视频时,我只是看到一个黑屏,顶部正在加载。

这是我尝试过的:

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {

    self.videoFilePath = (__bridge NSString *)([[info objectForKey:UIImagePickerControllerMediaURL] path]);

    NSString *mediaType = [info objectForKey:UIImagePickerControllerMediaType];

    if ([mediaType isEqualToString:(NSString *)kUTTypeMovie]) {
        [[NSUserDefaults standardUserDefaults] setObject:self.videoFilePath forKey:@"video"];
        [[NSUserDefaults standardUserDefaults] synchronize];
    }

    [self dismissViewControllerAnimated:YES completion:nil];
}

- (IBAction)playButtonPressed:(id)sender {
    NSObject *videoObj = [[NSUserDefaults standardUserDefaults] stringForKey:@"video"];

    if (videoObj != nil) {
        NSString* documentPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
        NSString* path = [documentPath stringByAppendingPathComponent:@"/capturedvideo.MOV"];
        NSURL* movieURL = [NSURL fileURLWithPath: path];

        self.moviePlayer =  [[MPMoviePlayerController alloc]
                             initWithContentURL:movieURL];

        self.moviePlayer.movieSourceType = MPMovieSourceTypeFile;
        self.moviePlayer.controlStyle = MPMovieControlStyleDefault;
        self.moviePlayer.shouldAutoplay = YES;

        [self.moviePlayer prepareToPlay];

        [self.view addSubview:self.moviePlayer.view];

        [self.moviePlayer setFullscreen:YES animated:YES];

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

        [self.moviePlayer play];

    } else {
        // UIAlertView is here
    }
}

我遗漏或忽略了什么?

编辑:

切换到这个,但场景相同:

    NSString *pathString = [NSString stringWithFormat:@"%@", videoObj];
    NSURL *streamURL = [NSURL fileURLWithPath:pathString];

编辑二:

我从我的设备上删除了该应用程序,录制了一个新视频,现在可以播放视频了!

关闭视频后,我收到了很多 NSAutoLayout 消息。对这些有帮助吗? :

Unable to simultaneously satisfy constraints.
    Probably at least one of the constraints in the following list is one you don't want. Try this: (1) look at each constraint and try to figure out which you don't expect; (2) find the code that added the unwanted constraint or constraints and fix it. (Note: If you're seeing NSAutoresizingMaskLayoutConstraints that you don't understand, refer to the documentation for the UIView property translatesAutoresizingMaskIntoConstraints) 
(
    "<NSLayoutConstraint:0x17408d700 H:|-(34)-[MPKnockoutButton:0x157677e00](LTR)   (Names: '|':_UIBackdropContentView:0x157550aa0 )>",
    "<NSLayoutConstraint:0x17408d660 H:[MPKnockoutButton:0x157677e00]-(34)-[MPDetailSlider:0x157550ee0](LTR)>",
    "<NSLayoutConstraint:0x1740903b0 H:[MPDetailSlider:0x157550ee0]-(34)-[UIView:0x157550dd0](LTR)>",
    "<NSLayoutConstraint:0x1740847e0 UIView:0x157550dd0.right == _UIBackdropView:0x157550560.right - 34>",
    "<NSLayoutConstraint:0x174084420 H:|-(0)-[_UIBackdropView:0x157550560]   (Names: '|':MPVideoPlaybackOverlayView:0x1575502f0 )>",
    "<NSLayoutConstraint:0x1740843d0 H:[_UIBackdropView:0x157550560]-(0)-|   (Names: '|':MPVideoPlaybackOverlayView:0x1575502f0 )>",
    "<NSAutoresizingMaskLayoutConstraint:0x170099be0 h=-&- v=-&- _UIBackdropContentView:0x157550aa0.midX == _UIBackdropView:0x157550560.midX>",
    "<NSAutoresizingMaskLayoutConstraint:0x1700825d0 h=-&- v=-&- _UIBackdropContentView:0x157550aa0.width == _UIBackdropView:0x157550560.width>",
    "<NSAutoresizingMaskLayoutConstraint:0x170096b70 h=-&- v=-&- MPVideoPlaybackOverlayView:0x1575502f0.width == MPVideoContainerView:0x157613560.width>",
    "<NSAutoresizingMaskLayoutConstraint:0x1700807d0 h=-&- v=-&- UIView:0x15754da30.width == UIView:0x15754dde0.width>",
    "<NSLayoutConstraint:0x17009d650 'UIView-Encapsulated-Layout-Width' H:[UIView:0x15754dde0(414)]>",
    "<NSAutoresizingMaskLayoutConstraint:0x17408f410 h=-&- v=-&- MPVideoContainerView:0x157613560.width == UIView:0x15754da30.width - 414>"
)

Will attempt to recover by breaking constraint 
<NSLayoutConstraint:0x17408d660 H:[MPKnockoutButton:0x157677e00]-(34)-[MPDetailSlider:0x157550ee0](LTR)>

Make a symbolic breakpoint at UIViewAlertForUnsatisfiableConstraints to catch this in the debugger.
The methods in the UIConstraintBasedLayoutDebugging category on UIView listed in <UIKit/UIView.h> may also be helpful.
2015-04-16 14:46:40.516 FollowUp[2385:657270] Unable to simultaneously satisfy constraints.
    Probably at least one of the constraints in the following list is one you don't want. Try this: (1) look at each constraint and try to figure out which you don't expect; (2) find the code that added the unwanted constraint or constraints and fix it. (Note: If you're seeing NSAutoresizingMaskLayoutConstraints that you don't understand, refer to the documentation for the UIView property translatesAutoresizingMaskIntoConstraints) 
(
    "<NSLayoutConstraint:0x17408d700 H:|-(34)-[MPKnockoutButton:0x157677e00](LTR)   (Names: '|':_UIBackdropContentView:0x157550aa0 )>",
    "<NSLayoutConstraint:0x17408d660 H:[MPKnockoutButton:0x157677e00]-(34)-[MPDetailSlider:0x157550ee0](LTR)>",
    "<NSLayoutConstraint:0x1740903b0 H:[MPDetailSlider:0x157550ee0]-(34)-[UIView:0x157550dd0](LTR)>",
    "<NSLayoutConstraint:0x1740847e0 UIView:0x157550dd0.right == _UIBackdropView:0x157550560.right - 34>",
    "<NSLayoutConstraint:0x174084420 H:|-(0)-[_UIBackdropView:0x157550560]   (Names: '|':MPVideoPlaybackOverlayView:0x1575502f0 )>",
    "<NSLayoutConstraint:0x1740843d0 H:[_UIBackdropView:0x157550560]-(0)-|   (Names: '|':MPVideoPlaybackOverlayView:0x1575502f0 )>",
    "<NSAutoresizingMaskLayoutConstraint:0x170099be0 h=-&- v=-&- _UIBackdropContentView:0x157550aa0.midX == _UIBackdropView:0x157550560.midX>",
    "<NSAutoresizingMaskLayoutConstraint:0x1700825d0 h=-&- v=-&- _UIBackdropContentView:0x157550aa0.width == _UIBackdropView:0x157550560.width>",
    "<NSAutoresizingMaskLayoutConstraint:0x170096b70 h=-&- v=-&- MPVideoPlaybackOverlayView:0x1575502f0.width == MPVideoContainerView:0x157613560.width>",
    "<NSAutoresizingMaskLayoutConstraint:0x17408dd40 h=-&- v=-&- MPVideoContainerView:0x157613560.width == MPSwipableView:0x157619cc0.width>",
    "<NSAutoresizingMaskLayoutConstraint:0x17408e4c0 h=-&- v=-&- MPSwipableView:0x157619cc0.width == MPMovieView:0x1576104a0.width>",
    "<NSAutoresizingMaskLayoutConstraint:0x174082490 h=--& v=--& H:[MPMovieView:0x1576104a0(0)]>"
)

Will attempt to recover by breaking constraint 
<NSLayoutConstraint:0x17408d660 H:[MPKnockoutButton:0x157677e00]-(34)-[MPDetailSlider:0x157550ee0](LTR)>

Make a symbolic breakpoint at UIViewAlertForUnsatisfiableConstraints to catch this in the debugger.
The methods in the UIConstraintBasedLayoutDebugging category on UIView listed in <UIKit/UIView.h> may also be helpful.

最佳答案

您实际上从未使用过 videoObj。您从用户默认值中获取它,然后检查它是否为零,但之后永远不会使用它。

您改为这样做:

NSString* documentPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
    NSString* path = [documentPath stringByAppendingPathComponent:@"/capturedvideo.MOV"];
    NSURL* movieURL = [NSURL fileURLWithPath: path];

据我所知,这与 videoObj 无关。如果您正在保存 NSUserDefaults 的路径,您会不会将 NSString *path 设置为 videoObj?检查一下,看看你得到了什么。

关于ios - 从 NSUserDefaults 播放视频,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29663665/

相关文章:

objective-c - UIWebView 作为 TableView 部分中页脚的 View 。奇怪的崩溃 - 附上崩溃日志

ios - 带有 DataTask Delegate 的 NSURLSession 返回 expectedContentLength negative(-1),无法计算文件大小

php - 如何将值从pHp服务器脚本返回到NSMutableData( objective-c ,iOS)

ios - 按位命令产生错误访问错误

objective-c - MPMoviePlayerController mp4 文件流

ios - MPMoviePlayer - 黑屏 - iOS 5 - 没有视频播放

ios - 当用户在 uipageviewcontroller 的最后一页时设置按钮的可见性

ios - 为核心数据 swift ios 中的子子项添加值(value)

iphone - 如何在iOS上播放给定字符串的音频?

ios - 单击后退或前进时 MPMovieplayerController 进入黑屏