播放NSUserDefaults的视频

时间:2015-04-16 01:11:56

标签: 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.

1 个答案:

答案 0 :(得分:2)

你从未真正使用过videoObj。你从用户默认值中获取它,然后检查它是否为零但在此之后从不使用它。

你改为:

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

这与我所说的videoObj无关。如果要保存NSUserDefaults的路径,那么你不会将NSString *路径设置为videoObj吗?检查一下,看看你到了哪里。