Playing Quicktime videos using AVPlayer

Greetings everyone.


A while back I posted a message here asking about playing two quicktime movies at the same time using the MPMoviePlayerController. jlilest002 pointed out that I couldn't, and he/she also pointed out that the MPMoviePlayerController is depricated. And was kind enough to point me to some links.


Not sure how to do the code, I looked around for some helpful guides on the net. Finally I found a simple, straightforward Youtube video:


https://www.youtube.com/watch?v=cT9vPwOcmng


. . . and followed its directions. And it WORKS!! Sort of....


I can hear it playing but I can't see it.


Here is what I did. First I made an AVPlayerClass.

// AVPlayerClass.h

#import <UIKit/UIKit.h>
#import <AVFoundation/AVFoundation.h> /* supplies the AVPlayer class */

@class AVPlayer; / implement the class for AVPlayer */

@interface AVPlayerClass : UIView

@property (nonatomic, retain) AVPlayer *player;

- (void) setMovieToPlayer: (AVPlayer *) player;

@end


and . . .


// AVPlayerClass.m

#import "AVPlayerClass.h"

@implementation AVPlayerClas

+ (Class)layerClass
  {
    return [AVPlayerLayer class];
  }

- (AVPlayer *) player
  {
    return [(AVPlayerLayer *) [self layer] player];
  }

- (void) setMovieToPlayer:(AVPlayer *) player /* takes an AVPlayer, then creates an AVPlayer layer and sets that AVPlayer to that layer */
  {
    [(AVPlayerLayer *) [self layer] setPlayer: player];
  }

@end


And then in the class which displays the video . . .


//  ViewController1birth.h

#import <UIKit/UIKit.h>
#import <AVFoundation/AVFoundation.h> /* imports the SystemSoundsID that represents the sounds needed in the application & supplies the AVPlayer class  */
#import "AVPlayerClass.h" /* imports the AVPlayer class I made that enables QuickTime movies to be played */

#import "ViewController2.h"

@class AVPlayer;
@class AVPlayerClass;

@interface ViewController1birth: UIViewController
{
   // code that has nothing to do with AVPlayer
}

@property (nonatomic, retain) AVPlayer *player; /* creates an instance (player) of AVPlayer that plays the birth video */
@property (nonatomic, retain) AVPlayerClass *playerView; /* creates an instance (playerView) of the AVPlayerClass that I created */

// more code that has nothing to do with AVPlayer

@end


and finally . . .


//  ViewController1birth.m

#import "ViewController1birth.h"

@interface ViewController1birth ()
@end

@implementation ViewController1birth

@synthesize player; /* here we synthesize the player property in ViewController1birth.h file */
@synthesize playerView; /* and we synthesize the playerView property in the ViewController1birth.h file */

- (void) viewDidLoad
{
  [self setUpMovie]; /* setUpMovie method (line 20) is called so the birth.mp4 video is played when the view is loaded */
}


- (void) setUpMovie /* This method is set up so we don't call all the code below in the viewDidLoad */
{
  NSURL *url = [[NSBundle mainBundle] URLForResource: @"birth" withExtension: @"mp4"]; /* this line calls the birth.mp4 video file */
  self.player = [AVPlayer playerWithURL: url]; /* sets the url to the AVPlayer property (player) property we synthesized (line 15) */
  [self.playerView setMovieToPlayer: player]; /* setting the AVPlayer to the playerView. The playerView is the storyboard object that will display the birth.mp4 video */
  [self.player play];
}

// code that has nothing to do with AVPlayer

@end


Now following the Youtube video's directions, in the story board, in the ViewController1birthScene, I drag a UIView onto the view controller and position it.


In the identity inspector, in the Class field, I keyed in 'AVPlayerClass'.


So far, I was able to follow his directions without any problems. Except with the last instruction . . .


What he did is right click on the yellow view controller icon on the viewcontroller and a dark grey popup menu appears and under Outlets there is the lable 'playerView' which you find in my ViewController1birth class (and his as well). He drags from playerView to the UIView in his storyboard. Finished! He plays it. It runs!!


Now what I did is . . . I right clicked on the yellow view controller in the storyboard, and then suddenly in the document outline bar on the left, the highlite switches from the UIView 'Player Class' to the yellow icon 'ViewController1birth' at the top and the pop up menus is completely different from what I expected and doesn't have the lable 'Outlets' in it or does it have 'playerView' either. I tired doing it from the Connections Inspector on the right of the storyboard but it doesn't show 'Outlets', it shows 'Referencing Outlets' and so I draged from Referencing Outlets to the UIView called Player Class and I just get 'view' in a popup menu. I settled for that seeing I didn't know what else to do. And when I run my app, like I said at the top, I see a blank white space where the animation should be and I hear it playing, so I am almost there. But I'm NOT!!


Can anyone see what I am doing wrong?


JR

Answered by JackRabbit in 197263022

I have found out the solution to my problem and I am posting it here in case someone else needs the code that works when it comes to playing QuickTime videos in their ViewController.


In the code that I posted above, look at line 16 in ViewController1birth.h. It should be:


@property (nonatomic, retain) IBOutlet AVPlayerClass *playerView; / creates an instance (playerView) of the AVPlayerClass that I created */


I had to put IBOutlet before AVPlayerClass


In the Youtube video called "Playing Movies With AVPlayerLayer In iOS 6" in the comments below the video, CHRISTIAN MCKENZIE posted this correction and now it works just fine!!


JR

Accepted Answer

I have found out the solution to my problem and I am posting it here in case someone else needs the code that works when it comes to playing QuickTime videos in their ViewController.


In the code that I posted above, look at line 16 in ViewController1birth.h. It should be:


@property (nonatomic, retain) IBOutlet AVPlayerClass *playerView; / creates an instance (playerView) of the AVPlayerClass that I created */


I had to put IBOutlet before AVPlayerClass


In the Youtube video called "Playing Movies With AVPlayerLayer In iOS 6" in the comments below the video, CHRISTIAN MCKENZIE posted this correction and now it works just fine!!


JR

Playing Quicktime videos using AVPlayer
 
 
Q