ios开发中用视频作为背景,视频上可以覆盖文字、按钮等

本文介绍了一个自定义的iOS视频播放视图控制器,通过Objective-C实现,利用AVFoundation框架来处理视频播放功能。该控制器可以全屏播放视频,并且在播放过程中能够隐藏导航栏,同时提供了跳过按钮及播放结束后的自动关闭功能。

直接上代码:

//

//  HelpVidoViewController.h

//  Goccia

//

//  Created by JackMeng on 14-7-16.

//  Copyright (c) 2014年 g-wearables.com. All rights reserved.

//


#import <UIKit/UIKit.h>


@interface HelpVidoViewController : UIViewController


@end

-----------------------------我是分割线--------------------------------------------------------------------------------

//

//  HelpVidoViewController.m

//  Goccia

//

//  Created by JackMeng on 14-7-16.

//  Copyright (c) 2014年 g-wearables.com. All rights reserved.

//


#import "HelpVidoViewController.h"

#import <AVFoundation/AVFoundation.h>

@interface HelpVidoViewController ()

{

    UIApplication *app;

}

@property (nonatomic, strong) AVPlayer *avplayer;

@property (strong, nonatomic) UIView *movieView;

@property (strong, nonatomic) UIView *gradientView;

@property (strong, nonatomic) UIView *contentView;

@end


@implementation HelpVidoViewController


- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil

{

    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];

    if (self) {

        // Custom initialization

    }

    return self;

}

- (void) viewWillAppear:(BOOL)animated

{

    [super viewWillAppear:animated];

    [self.navigationController setNavigationBarHidden:YES animated:YES];

}

- (void)viewDidAppear:(BOOL)animated

{

    [super viewDidAppear:animated];

    [self.avplayer seekToTime:kCMTimeZero];

    //    [self.avplayer setVolume:0.50f];

    [self.avplayer play];

    [self.avplayer setActionAtItemEnd:AVPlayerActionAtItemEndNone];

    [[NSNotificationCenter defaultCenter] addObserver:self

                                             selector:@selector(helpPlayerItemDidReachEnd:)

                                                 name:AVPlayerItemDidPlayToEndTimeNotification

                                               object:[self.avplayer currentItem]];

    

}


- (void)viewDidLoad

{

    [super viewDidLoad];

    // Do any additional setup after loading the view from its nib.

    self.movieView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];

    self.movieView.backgroundColor = [UIColor clearColor];

    [self.view addSubview:self.movieView];

    

    NSBundle *bundle = [NSBundle mainBundle];

    NSString *moviePath = [bundle pathForResource:@"xxxx" ofType:@"mov"];//xxxx代表你要播放的视频

    NSURL *movieURL = [NSURL fileURLWithPath:moviePath];

    

    AVAsset *avAsset = [AVAsset assetWithURL:movieURL];

    AVPlayerItem *avPlayerItem =[[AVPlayerItem alloc]initWithAsset:avAsset];

    self.avplayer = [[AVPlayer alloc]initWithPlayerItem:avPlayerItem];

    AVPlayerLayer *avPlayerLayer =[AVPlayerLayer playerLayerWithPlayer:self.avplayer];

    [avPlayerLayer setVideoGravity:AVLayerVideoGravityResizeAspect];

    [avPlayerLayer setFrame:self.view.frame];

    [self.movieView.layer addSublayer:avPlayerLayer];

    

    self.gradientView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];

    self.gradientView.backgroundColor = [UIColor clearColor];

    [self.view addSubview:self.gradientView];

    

    CAGradientLayer *gradient = [CAGradientLayer layer];

    gradient.frame = self.gradientView.bounds;

    gradient.colors = [NSArray arrayWithObjects:(id)[[UIColor clearColor] CGColor], (id)[[UIColor clearColor] CGColor], (id)[[UIColor clearColor] CGColor],nil];

    [self.gradientView.layer insertSublayer:gradient atIndex:0];

    

    self.contentView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];

    self.contentView.backgroundColor = [UIColor clearColor];

    [self.view addSubview:self.contentView];

    

    UILabel * helpLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 60, 300, 50)];

    helpLabel.text = NSLocalizedString(@"Tutorial", @"使用教程");

    helpLabel.textAlignment = NSTextAlignmentCenter;

    helpLabel.font = [UIFont systemFontOfSize:40.0];

    helpLabel.textColor = [UIColor whiteColor];

    [self.contentView addSubview:helpLabel];

    

    UIButton * dismissBut = [UIButton buttonWithType:UIButtonTypeCustom];

    

    if (IS_IPHONE5)

    {

        dismissBut.frame = CGRectMake(240, 528, 80, 40);

    }

    else

    {

        dismissBut.frame = CGRectMake(240, 440, 80, 40);

    }

    [dismissBut setTitle:NSLocalizedString(@"Skip", @"跳过") forState:UIControlStateNormal];

    

    dismissBut.titleLabel.font = [UIFont systemFontOfSize:14.0];

    [dismissBut addTarget:self action:@selector(dismissButtonClick) forControlEvents:UIControlEventTouchUpInside];

    [self.contentView addSubview:dismissBut];

    

    app = [UIApplication sharedApplication];

    

    [[NSNotificationCenter defaultCenter]

     

     addObserver:self

     

     selector:@selector(helpApplicationDidBecomeActive:)

     

     name:UIApplicationDidBecomeActiveNotification

     

     object:app];

    

    UITapGestureRecognizer * playTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(playTap_help)];

    [self.contentView addGestureRecognizer:playTap];

}

- (void)helpApplicationDidBecomeActive:(NSNotification *)notification

{

    [self.avplayer play];

    

}

- (void) playTap_help

{

    if (self.avplayer.status == 1)

    {

        [self.avplayer play];

    }

}

- (void) dismissButtonClick

{

    [[NSNotificationCenter defaultCenter] removeObserver:self name:UIApplicationDidBecomeActiveNotification object:app];

    [self dismissViewControllerAnimated:YES completion:^{}];

}

- (void)helpPlayerItemDidReachEnd:(NSNotification *)notification

{

    [self dismissViewControllerAnimated:YES completion:^{}];

}

- (void)dealloc

{

    [[NSNotificationCenter defaultCenter] removeObserver:self name:AVPlayerItemDidPlayToEndTimeNotification object:nil];

}

- (void)viewWillDisappear:(BOOL)animated

{

    [super viewWillDisappear:animated];

    [self.avplayer pause];

}


- (void)didReceiveMemoryWarning

{

    [super didReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}


@end


转载于:https://my.oschina.net/LangZiAiFer/blog/294900

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值