1


I followed this tutorial, and included a video player in my app.
But the problem is that i want to hide the controls, and be able to dismiss the video on screen touch.
I tried putting a big transparent button in front of the video that triggers the dismiss function, but with no luck. the video will always be over the button and the function will never be called.
Is there another way of doing it?
ty

astazed
  • 649
  • 1
  • 10
  • 24

5 Answers5

1

**Hi I think AVPlayer is become more suitable to you,You can use it as below and then add subview if you want to play and if you want to stop remove it from super view

** #import"AVFoundation/AVFoundation.h"

**For creating Player List:

NSString *path1 = [[NSBundle mainBundle] pathForResource:@"hello" ofType:@"mp4"];
AVPlayerItem *first = [AVPlayerItem playerItemWithURL:[NSURL fileURLWithPath:path1]]; 
player= [AVPlayer playerWithPlayerItem:first];
[self.playerView setPlayer:player];
[player play];

** You have to make uiview for playerview in it :

(id)initWithFrame:(CGRect)frame {self = [super initWithFrame:frame];
if (self) {
    // Initialization code.
}
return self;
}
+ (Class)layerClass {
 return [AVPlayerLayer class];
}

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

-(void)setPlayer:(AVPlayer *)player {
[(AVPlayerLayer *)[self layer] setPlayer:player];   
}
sinh99
  • 3,909
  • 32
  • 32
  • friend..in above code you have to create one uiview and give its name player view and put above second code in it. after that in your main view where you want to display your vedio at there,in it's did load put above 1st code then [self.view addsubview playerview] for playing vedio and remove it from superview for stop your vedio. – sinh99 May 10 '11 at 10:57
  • AVPlayer use less memory concuption in compare of mpplayer so it is more efficient. – sinh99 May 10 '11 at 11:01
  • ok, read apple's documentation about avfoundation, rewrote the code, and get rid of some errors. but i'm still stuck with one, in [self.playerView setPlayer:player];. this is what the comiler is showing: "accessing unknown 'playerView' getter method". any thoughts? – astazed May 11 '11 at 09:02
  • Look i am expalin you in detail...Take 1 Player view Controller in its .m file you have to put that second code.and then add new uiview controller and give its name mainview controller.in it you have to take that 1st code in didload and then take uiview in its xib file and give it's class name as playerview..then this view you have to put on mainview via [self.view addsubview:PlayerView]..so your player view make continue and for remove it [remove from super view]..and make sure vou have to import #import frame work. – sinh99 May 11 '11 at 09:59
  • man i already did what you suggested. still no luck! anyway thank you for your help, i just found the solution to the problem. i will post the answer in a few minutes. – astazed May 11 '11 at 10:04
1

PlayerViewControll .H

#import <UIKit/UIKit.h>
#import <AVFoundation/AVFoundation.h>
@interface PlayerView : UIView UILabel *pageNumberLabel;
int pageNumber;
}
@property (nonatomic, retain) AVPlayer *player;
- (id)initWithPageNumber:(int)page;

PlayerView .M

#import "PlayerView.h"


@implementation PlayerView
 - (id)initWithFrame:(CGRect)frame {
self = [super initWithFrame:frame];
if (self) {}
return self;
}
 + (Class)layerClass {
return [AVPlayerLayer class];
}
- (AVPlayer*)player {
return [(AVPlayerLayer *)[self layer] player];
}
- (void)setPlayer:(AVPlayer *)player {
[(AVPlayerLayer *)[self layer] setPlayer:player];
}

MainViewControll .H

#import <AVFoundation/AVFoundation.h>
#import "PlayerView.h"
@interface MainViewController : UIViewController {
IBOutlet PlayerView *playerView;
NSString *url;
AVPlayer *player;
NSMutableArray *arrIteam;
}
@property(nonatomic,retain) NSMutableArray *arrIteam;
@property (nonatomic, retain) AVPlayer *player;
@property(nonatomic ,retain)IBOutlet PlayerView *playerView;

PlayerView.M

#import "MainViewController.h"
#import <QuartzCore/QuartzCore.h>
@implementation MainViewController
@synthesize player,playerView,arrIteam;
- (void)viewDidLoad {
NSString *path1 = [[NSBundle mainBundle] pathForResource:@"hello" ofType:@"mp4"];
AVPlayerItem *first = [AVPlayerItem playerItemWithURL:[NSURL fileURLWithPath:path1]]; 
arrIteam = [[NSMutableArray alloc] initWithObjects:first,second, third,fourth,nil]; 
player=[AVPlayer playerWithPlayerItem:[arrIteam objectAtIndex:i]];
[self.playerView setPlayer:player];
[player play];
[super viewDidLoad];
}
sinh99
  • 3,909
  • 32
  • 32
  • thank you! this is approximatively what i was writing, and i still had that same error! there must be something I'm missing.... – astazed May 11 '11 at 10:26
  • I think you didn't take uiview(playerview) in MainView's xib that's y it may be happen. – sinh99 May 11 '11 at 10:30
0

You should use UIGestureRecognizer class. See manual for details. Or read this tutorial.

0

Use this code.

UITapGestureRecognizer *gestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(stopPlayer)];


[self.view addGestureRecognizer:gestureRecognizer];
gestureRecognizer.cancelsTouchesInView = NO;
[gestureRecognizer release];

in view didload;

And in

- (void) stopPlayer

stop the player and release this player from view.

Hope it helps you.

Sandeep Singh
  • 826
  • 2
  • 10
  • 24
0

i just found the solution to my problem in another question asked here.
after some minor modifications, the code worked as expected. thank you all.

Community
  • 1
  • 1
astazed
  • 649
  • 1
  • 10
  • 24