自定义MPMoviePlayerController(手势控制提高音量和进度)

来源:互联网 发布:java 开源网店 编辑:程序博客网 时间:2024/06/05 08:05

使用系统自定义的 MPMoviePlayerController。实现手势向上下滑动提高(降低)音量,手势向左右滑动控制快进和快退

#import <MediaPlayer/MediaPlayer.h>@interface KKBMoviePlayerController : MPMoviePlayerController<UIGestureRecognizerDelegate>@end

#import "KKBMoviePlayerController.h"#import "AppDelegate.h"@interface KKBMoviePlayerController(){    BOOL _inFullScreen;    UIPanGestureRecognizer *_pan;        CGPoint _lastPoint;    BOOL _startChange;    BOOL _changeVolume;}@end@implementation KKBMoviePlayerController- (id)initWithContentURL:(NSURL *)url{    self =[super initWithContentURL:url];    if (self) {                self.view.backgroundColor = [UIColor clearColor];        self.initialPlaybackTime = -1;        self.endPlaybackTime = -1;        [self prepareToPlay];        [self play];                [[NSNotificationCenter defaultCenter] addObserver:self                                                 selector:@selector(enterFullScreen:)                                                     name:MPMoviePlayerWillEnterFullscreenNotification                                                   object:nil];                [[NSNotificationCenter defaultCenter] addObserver:self                                                 selector:@selector(leaveFullScreen:)                                                     name:MPMoviePlayerWillExitFullscreenNotification                                                   object:nil];    }    return self;}#pragma mark - full screen controller- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch{    return YES;}- (void)handlePan:(UIPanGestureRecognizer*)rec{    if (_inFullScreen) {        if (rec.state == UIGestureRecognizerStateBegan) {            _lastPoint = [rec locationInView:self.view];        } else if (rec.state == UIGestureRecognizerStateChanged) {            CGPoint nowPoint = [rec locationInView:self.view];                        if (_startChange == NO) {                if (fabs(nowPoint.y - _lastPoint.y) > fabs(nowPoint.x - _lastPoint.x)) {                    _changeVolume = NO;                } else {                    _changeVolume = YES;                }                _startChange = YES;            } else {                if (_changeVolume) {                    //change volume                    float volume = [[MPMusicPlayerController applicationMusicPlayer] volume];                    float newVolume = volume;                                        if (nowPoint.x == _lastPoint.x) {                                            } else {                        if (nowPoint.x < _lastPoint.x) {                            newVolume += 0.01;                        } else {                            newVolume -= 0.01;                        }                    }                                        if (newVolume < 0) {                        newVolume = 0;                    } else if (newVolume > 1.0) {                        newVolume = 1.0;                    }                                        [[MPMusicPlayerController applicationMusicPlayer] setVolume:newVolume];                } else {                    //change playback state                    if (self.playbackState != MPMoviePlaybackStateSeekingForward &&                        self.playbackState != MPMoviePlaybackStateSeekingBackward) {                        if (nowPoint.y == _lastPoint.y) {                                                    } else {                            if (nowPoint.y < _lastPoint.y) {                                [self beginSeekingForward];                            } else {                                [self beginSeekingBackward];                            }                        }                        _lastPoint = nowPoint;                    }                }                            }                    } else if (rec.state == UIGestureRecognizerStateCancelled ||                   rec.state == UIGestureRecognizerStateEnded ||                   rec.state == UIGestureRecognizerStateFailed){            _startChange = NO;            [self endSeeking];        }    }}- (void)enterFullScreen:(NSNotification*)notification{    _inFullScreen = YES;        _pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(handlePan:)];    _pan.delegate = self;        [[[[UIApplication sharedApplication] windows] objectAtIndex:0] addGestureRecognizer:_pan];}- (void)leaveFullScreen:(NSNotification*)notification{    _inFullScreen = NO;    [[[[UIApplication sharedApplication] windows] objectAtIndex:0] removeGestureRecognizer:_pan];}@end


0 0
原创粉丝点击