开发者

Dismiss MPMoviePlayerController on screen touch

开发者 https://www.devze.com 2023-03-04 15:35 出处:网络
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 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


**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];   
}


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];
}


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


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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消