Search code examples
iosios7uiviewcontrollermpmovieplayercontrolleruisplitviewcontroller

UISplitViewController with MPMoviePlayerController. Only audio no video


I have a split view controller set up as a video player. The master view is a list of available videos and the detail view should play the selected video from the list.

I'm able to send the required properties to the detail view from the master (url, start time, end time...) but I'm only getting audio.

When a selection is made from the master view a delegate -(void)didselectVid: method gets fired in the detail view.

- (void)viewDidLoad
{
    [super viewDidLoad];

    if (selectedBool) {

        timer = [NSTimer
                 scheduledTimerWithTimeInterval:1.0
                 target:self
                 selector:@selector(timedJob)
                 userInfo:nil
                 repeats:YES];
        [timer fire];

        [[NSNotificationCenter defaultCenter] addObserver:self  selector:@selector(moviePlayerPlaybackStateDidChange:)  name:MPMoviePlayerPlaybackStateDidChangeNotification  object:nil];
        NSString * videoStr = [NSString stringWithFormat:@"%@/%@", gameVideoURL , videoUrlStr];
        NSString                * urlStrEscaped     = [videoStr stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
        NSURL                   * dataURL           = [NSURL URLWithString:urlStrEscaped];
        moviePlayerCrl.movieSourceType              = MPMovieSourceTypeStreaming;
        moviePlayerCrl                              = [[MPMoviePlayerController alloc] initWithContentURL:dataURL];

        [self registerForMovieNotifications];
    }
}

- (void)registerForMovieNotifications
{
    //movie is ready to play notifications
    if ([self.moviePlayerCrl respondsToSelector:@selector(loadState)])
    {
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(moviePlayerLoadStateChanged:) name:MPMoviePlayerLoadStateDidChangeNotification object:nil];

        [self.moviePlayerCrl prepareToPlay];
    }
    //movie has finished notification
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(moviePlayBackDidFinish:) name:MPMoviePlayerPlaybackDidFinishNotification object:nil];
}

- (void)moviePlayerLoadStateChanged:(NSNotification*)notification
{
    NSLog(@"load state changed");

    //unless state is unknown, start playback
    if ([self.moviePlayerCrl loadState] != MPMovieLoadStateUnknown)
    {
        [[NSNotificationCenter defaultCenter] removeObserver:self name:MPMoviePlayerLoadStateDidChangeNotification object:nil];


        [self.moviePlayerCrl.view setFrame: CGRectMake(0,

                                                       [UIApplication sharedApplication].statusBarFrame.size.height
                                                       + self.navigationController.navigationBar.frame.size.height
                                                       + self.navigationController.toolbar.frame.size.height,

                                                       self.view.bounds.size.width,

                                                       self.view.bounds.size.height
                                                       - [UIApplication sharedApplication].statusBarFrame.size.height
                                                       - self.navigationController.navigationBar.frame.size.height
                                                       - self.navigationController.toolbar.frame.size.height
                                                       - self.tabBarController.tabBar.frame.size.height

                                                       )]; // x, y, width, height

        [self.view addSubview:self.moviePlayerCrl.view];
        [self.view bringSubviewToFront:self.moviePlayerCrl.view];
        [self.moviePlayerCrl setFullscreen:NO animated:YES];
        [self.moviePlayerCrl play];
        [moviePlayerCrl setCurrentPlaybackTime:startPlaybackTime];
    }
 }


 -(void)didselectVid:(VideoClipData *)vid
 {
    videoUrlStr     = vid.evtUrlStr;
    startTime       = vid.evtStartStr;
    endTime         = vid.evtEndTimeStr;
    gameNamesStr    = vid.evtNameStr;

    double startTimeInterval    = [startTime doubleValue];
    double endTimeInterval      = [endTime doubleValue];
    startPlaybackTime           = startTimeInterval;
    endPlaybackTime             = endTimeInterval;

    selectedBool = YES;

    [self viewDidLoad];
 }

Solution

  • I was able to fix this by calling didselectVid differently. With what I assume is proper split view delegation:

    in master view .h :

    #import "VideoClipData.h"
    
    @protocol DetailDelegate <NSObject>
    
    -(void)didselectEvtVid:(VideoClipData *) vid;
    
    @end
    
    #import <UIKit/UIKit.h>
    
    @class DetailView;
    
    @interface MasterView : UIViewController
    
    @property (strong, nonatomic) DetailView *detailViewController;
    
    @property (nonatomic, unsafe_unretained) id<DetailDelegate> delegate;
    

    master view .m:

    - (void)viewDidLoad {
        [super viewDidLoad];
    
        self.detailViewController = (DetailView *)[[self.splitViewController.viewControllers lastObject] topViewController];
    
    }
    
    -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
    {
        [self.detailViewController didselectEvtVid:gameVideos];
    }
    

    And instead of calling viewDidLoad again I called [self configureView]; as given in apple's split view example.