在屏幕触摸上关闭MPMoviePlayerController

时间:2011-05-10 08:14:34

标签: iphone objective-c mpmovieplayercontroller


我按照tutorial进行了操作,并在我的应用中添加了一个视频播放器。
但问题是我想隐藏控件,并能够在屏幕触摸时关闭视频。
我尝试在视频前面放一个大透明按钮,触发解散功能,但没有运气。视频将始终在按钮上,并且永远不会调用该功能 还有另一种方法吗?
TY

5 个答案:

答案 0 :(得分:1)

**嗨我认为AVPlayer变得更适合你,你可以使用如下,然后添加子视图,如果你想玩,如果你想停止从超级视图中删除它

** #import“AVFoundation / AVFoundation.h”

**用于创建玩家列表:

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

**你必须在其中制作关于playerview的uiview:

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

答案 1 :(得分: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];
}

答案 2 :(得分:0)

您应该使用UIGestureRecognizer类。有关详细信息,请参阅manual。或者阅读this tutorial

答案 3 :(得分:0)

使用此代码。

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


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

在视图中执行;

并在

- (void) stopPlayer

停止播放器并从视图中释放此播放器。

希望它对你有所帮助。

答案 4 :(得分:0)

我刚刚在另一个问题中找到问题solution。经过一些小的修改,代码按预期工作。谢谢大家。