zoukankan      html  css  js  c++  java
  • 音频视频播放

    文件结构

    application

    //

    #import "ViewController.h"

    #import "VideoViewController.h"

    #import "MovieViewController.h"

    @interface ViewController ()

    {

        //将类视图控制器设为全局变量

        VideoViewController *_videoVC;

        MovieViewController *_movieVC;

    }

    - (IBAction)doPlayAudioButton:(id)sender;

    - (IBAction)doPlayMovieButton:(id)sender;

    @end

    @implementation ViewController

    - (void)viewDidLoad {

        [super viewDidLoad];

        // Do any additional setup after loading the view, typically from a nib.

    }

    - (void)didReceiveMemoryWarning {

        [super didReceiveMemoryWarning];

        // Dispose of any resources that can be recreated.

    }

    //点击进入音频页面

    - (IBAction)doPlayAudioButton:(id)sender {

        //判断视图控制器是否存在,如果不存在,初始化。

        if (!_videoVC) {

            _videoVC=[[VideoViewController alloc]init];

        }

        //将_videoVC控制的视图 设为将要进入的页面

        [self.navigationController  pushViewController:_videoVC animated:YES];//yes带有动画效果

        

        

    }

    //点击进入视频页面

    - (IBAction)doPlayMovieButton:(id)sender {

        if (!_movieVC) {

            _movieVC=[[MovieViewController alloc]init];

        }

        

        [self.navigationController pushViewController:_movieVC animated:YES];

        

    }

    @end

    //

    #import "ViewController.h"

    #import "VideoViewController.h"

    #import "MovieViewController.h"

    @interface ViewController ()

    {

        //将类视图控制器设为全局变量

        VideoViewController *_videoVC;

        MovieViewController *_movieVC;

    }

    - (IBAction)doPlayAudioButton:(id)sender;

    - (IBAction)doPlayMovieButton:(id)sender;

    @end

    @implementation ViewController

    - (void)viewDidLoad {

        [super viewDidLoad];

        // Do any additional setup after loading the view, typically from a nib.

    }

    - (void)didReceiveMemoryWarning {

        [super didReceiveMemoryWarning];

        // Dispose of any resources that can be recreated.

    }

    //点击进入音频页面

    - (IBAction)doPlayAudioButton:(id)sender {

        //判断视图控制器是否存在,如果不存在,初始化。

        if (!_videoVC) {

            _videoVC=[[VideoViewController alloc]init];

        }

        //将_videoVC控制的视图 设为将要进入的页面

        [self.navigationController  pushViewController:_videoVC animated:YES];//yes带有动画效果

        

        

    }

    //点击进入视频页面

    - (IBAction)doPlayMovieButton:(id)sender {

        if (!_movieVC) {

            _movieVC=[[MovieViewController alloc]init];

        }

        

        [self.navigationController pushViewController:_movieVC animated:YES];

        

    }

    @end//

    #import "ViewController.h"

    #import "VideoViewController.h"

    #import "MovieViewController.h"

    @interface ViewController ()

    {

        //将类视图控制器设为全局变量

        VideoViewController *_videoVC;

        MovieViewController *_movieVC;

    }

    - (IBAction)doPlayAudioButton:(id)sender;

    - (IBAction)doPlayMovieButton:(id)sender;

    @end

    @implementation ViewController

    - (void)viewDidLoad {

        [super viewDidLoad];

        // Do any additional setup after loading the view, typically from a nib.

    }

    - (void)didReceiveMemoryWarning {

        [super didReceiveMemoryWarning];

        // Dispose of any resources that can be recreated.

    }

    //点击进入音频页面

    - (IBAction)doPlayAudioButton:(id)sender {

        //判断视图控制器是否存在,如果不存在,初始化。

        if (!_videoVC) {

            _videoVC=[[VideoViewController alloc]init];

        }

        //将_videoVC控制的视图 设为将要进入的页面

        [self.navigationController  pushViewController:_videoVC animated:YES];//yes带有动画效果

        

        

    }

    //点击进入视频页面

    - (IBAction)doPlayMovieButton:(id)sender {

        if (!_movieVC) {

            _movieVC=[[MovieViewController alloc]init];

        }

        

        [self.navigationController pushViewController:_movieVC animated:YES];

        

    }

    @end

    音频

    //

    #import "VideoViewController.h"

    #import <AVFoundation/AVFoundation.h>

    @interface VideoViewController ()

    {

        AVAudioPlayer *_player;//播放文件的类

    }

    /*

     IOS支持的音频格式:mp3,aac,alac,ima4,linear(wma?)

     */

    @end

    @implementation VideoViewController

    - (void)viewDidLoad {

        [super viewDidLoad];

    self.navigationItem.title=@"音频";

        //获取音频路径

        NSString *musicPath=[[NSBundle mainBundle]pathForResource:@"简单爱" ofType:@"mp3"];

    //建立连接对象(NSURL  做链接使用的类)

        NSURL *url=[[NSURL alloc]initFileURLWithPath:musicPath];

        NSError *error=nil;

        _player=[[AVAudioPlayer alloc]

                 initWithContentsOfURL:url error:&error];//还有initWithData

        [_player prepareToPlay];//准备播放(开始加载音频文件,确保player尽可能快地开始播放)

        if (error) {

            NSLog(@"错误信息:%@",[error localizedDescription]);

            

        }

        NSLog(@"1");

    }

    -(void)viewWillAppear:(BOOL)animated{

        NSLog(@"2");

    }

    - (void)didReceiveMemoryWarning {

        [super didReceiveMemoryWarning];

        // Dispose of any resources that can be recreated.

    }

    - (IBAction)doPlayButton:(id)sender {

        [_player play];

        

    }

    - (IBAction)doPauseButton:(id)sender {

        [_player pause];//暂停

        //[_player  stop];//停止

        /* stops playback. no longer ready to play. */

    //    _player.currentTime=0;//从头开始

        /* returns the current time associated with the output device */

    }

    - (IBAction)doGo5Button:(id)sender {

        if (_player.currentTime<=_player.duration-5) {

            _player.currentTime+=5;//当前时间变为五秒后时间

        }

        

    }

    - (IBAction)doBack5Button:(id)sender {

        if (_player.currentTime>=5) {

            _player.currentTime-=5;

        }

    }

    //音量

    - (IBAction)doChangeVoice:(UISlider *)sender {

        _player.volume=sender.value;

        

    }

    @end

    视频

    #import "MovieViewController.h"

    #import <MediaPlayer/MediaPlayer.h>

    @interface MovieViewController ()

    {

        MPMoviePlayerViewController *_player;

    }

    @end

    @implementation MovieViewController

    - (void)viewDidLoad {

        [super viewDidLoad];

    self.navigationItem.title=@"视频";

        self.navigationController.navigationBarHidden=YES;//隐藏导航栏

        

        

        NSString *moviePath=[[NSBundle mainBundle]pathForResource:@"hulala" ofType:@"mp4"];

        //建立连接对象(NSURL  做链接使用的类)

        NSURL *url=[[NSURL alloc]initFileURLWithPath:moviePath];

        

        

        NSError *error=nil;

        _player=[[MPMoviePlayerViewController alloc]initWithContentURL:url];

           if (error) {

            NSLog(@"错误信息:%@",[error localizedDescription]);

            

        }

        [_player.moviePlayer setScalingMode:MPMovieScalingModeAspectFit];

       /*

         

        MPMovieScalingModeNone //不对视频进行缩放

        MPMovieScalingModeAspectFit,  // 视频缩放4:3

        MPMovieScalingModeAspectFill, //  缩放到外框,4:3尺寸会隐藏一部分

        MPMovieScalingModeFill        // 视频拉伸失去原本比例

         */

        [_player.moviePlayer    setControlStyle:MPMovieControlStyleFullscreen];//充满全屏

       /* MPMovieControlStyleNone,       // No controls

        MPMovieControlStyleEmbedded,   // Controls for an embedded view

        MPMovieControlStyleFullscreen, // Controls for fullscreen playback

        

        MPMovieControlStyleDefault = MPMovieControlStyleEmbedded

        */

        [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(finshMoviePlay) name:MPMoviePlayerPlaybackDidFinishNotification  object:nil];

         NSLog(@"1");

     }

    -(void)viewWillAppear:(BOOL)animated{

        [self.view addSubview:_player.view];

    }

    -(void)finshMoviePlay

    {

        self.navigationController.navigationBarHidden=NO;

        [self.navigationController  popToRootViewControllerAnimated:YES];

    }

    - (void)didReceiveMemoryWarning {

        [super didReceiveMemoryWarning];

        // Dispose of any resources that can be recreated.

    }

  • 相关阅读:
    二叉树逻辑结构重点
    循环链表
    数据结构 单链表
    《深入理解计算机系统》第7章:重定位PC相对引用的理解
    一个关于空指针的思考
    简单解决python安装中的Unable to find vcvarsall.bat问题
    解决python本地离线安装requests问题
    使用共享内存和信号量模拟实现多进程会话
    使用openssl演练数字签名
    简单了解C语言内嵌汇编
  • 原文地址:https://www.cnblogs.com/OIMM/p/4728159.html
Copyright © 2011-2022 走看看