实现视频播放的渐变切换效果,可以修改代码在iphone上使用。
//
// FLWPlayer.h
//
// Created by scott.8an@gmail.com on 11-10-28.
// Copyright 2011 littleWorn inc. All rights reserved.
//
#import <UIKit/UIKit.h>
#import <MediaPlayer/MediaPlayer.h>
@class FLWPlayer;
@protocol FLWPlayerDelegate <NSObject>
-(void)singleTapView:(FLWPlayer*)view;
@end
//interface
@interface FLWPlayer : UIView{
@private
id<FLWPlayerDelegate>_delegate;
MPMoviePlayerController *_player;
NSURL *_url;
}
@property(assign) id<FLWPlayerDelegate> delegate;
@property(nonatomic,retain) NSURL *url;
@property(nonatomic,retain) MPMoviePlayerController *player;
-(id)initWithURL:(NSURL*)url;
-(id)initWithFrame:(CGRect)frame url:(NSURL*)url;
-(void)play;
-(void)stop;
-(void)pause;
@end
//
// FLWPlayer.m
//
// Created by scott.8an@gmail.com on 11-10-28.
// Copyright 2011 littleWorn inc. All rights reserved.
//
#import "FLWPlayer.h"
@implementation FLWPlayer
@synthesize delegate = _delegate;
@synthesize url = _url;
@synthesize player = _player;
#pragma mark life cycle
- (void)dealloc {
[_delegate release];
[_url release];
[_player release];
[super dealloc];
}
//init
-(id)init{
if (self = [super init]) {
//去掉状态栏
[[UIApplication sharedApplication] setStatusBarHidden:YES];
UITapGestureRecognizer *tg = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(singleTap)];
tg.numberOfTapsRequired = 2;
[self addGestureRecognizer:tg];
[tg release];
}
return self;
}
-(id)initWithFrame:(CGRect)frame{
if (self = [super initWithFrame:frame]) {
//去掉状态栏
[[UIApplication sharedApplication] setStatusBarHidden:YES];
UITapGestureRecognizer *tg = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(singleTap)];
tg.numberOfTapsRequired = 2;
[self addGestureRecognizer:tg];
[tg release];
}
return self;
}
-(id)initWithURL:(NSURL*)url{
if (self = [super init]) {
self.url = url;
//去掉状态栏
[[UIApplication sharedApplication] setStatusBarHidden:YES];
UITapGestureRecognizer *tg = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(singleTap)];
tg.numberOfTapsRequired = 2;
[self addGestureRecognizer:tg];
[tg release];
}
return self;
}
-(id)initWithFrame:(CGRect)frame url:(NSURL*)url{
if (self = [super initWithFrame:frame]) {
self.url = url;
//去掉状态栏
[[UIApplication sharedApplication] setStatusBarHidden:YES];
UITapGestureRecognizer *tg = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(singleTap)];
tg.numberOfTapsRequired = 2;
[self addGestureRecognizer:tg];
[tg release];
}
return self;
}
-(void)singleTap{
[self.delegate singleTapView:self];
}
#pragma mark overide
//-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{
// [self.delegate singleTapView:self];
//}
//播放视频
-(void)play{
//覆盖
UIView *cover = [[UIView alloc] initWithFrame:CGRectMake(0,0, 1024, 768)];
cover.tag = 100;
cover.backgroundColor = [UIColor blackColor];
cover.alpha = 0.0f;
//添加覆盖视图实现渐变退出效果
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.4f];
[UIView setAnimationDelay:0.2f];
[UIView setAnimationDelegate:self];
[UIView setAnimationDidStopSelector:@selector(stopPlaying)];
cover.alpha = 1.0f;
[UIView commitAnimations];
[self addSubview:cover];
[cover release];
self.backgroundColor = [UIColor blackColor];
}
//开始播放
-(void)startToPlay{
if (nil == self.url) {
return;
}
//清除覆盖
UIView *cover = [self viewWithTag:100];
if (cover) {
[cover removeFromSuperview];
}
//加载视频
if (_player) {
[_player setContentURL:self.url];
}else {
self.player = [[MPMoviePlayerController alloc] initWithContentURL:self.url];
_player.view.frame = CGRectMake(0, 0, 1024, 768);
_player.controlStyle= MPMovieControlStyleNone;
_player.repeatMode = MPMovieRepeatModeOne;
[_player setFullscreen:YES];
[self addSubview:_player.view];
[_player release];
}
//开始播放
[_player prepareToPlay];
[_player play];
//渐变进入
_player.view.alpha = 0.0f;
_player.backgroundView.backgroundColor = [UIColor blackColor];
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:1.0f];
_player.view.alpha = 1.0f;
[UIView commitAnimations];
}
//停止正在播放的视频
-(void)stopPlaying{
[self stop];
[self startToPlay];
}
//停止播放
-(void)stop{
if (_player) {
[_player stop];
}
}
//暂停播放
-(void)pause{
if (_player) {
[_player pause];
}
}
@end