多媒體音頻視頻與iPad基本使用

多媒體音頻視頻與iPad基本使用

1、多媒體播放

  1.配置庫  -->  AVFoundation.framework編程

    頭文件   #import <AVFoundation/AVFoundation.h>xcode

  2.播放音頻

  (1)播放本地音頻

@interface ViewController ()
{
  //  AVAudioPlayer *_player;
    UISlider *_volumeSlider;  //音量控制
//    UIProgressView *_playProgressView;  //播放進度
}
@property (nonatomic,strong) AVAudioPlayer *player;
@property (nonatomic,strong) UIProgressView *playProgressView;
#pragma mark - 播放本地音樂文件
-(void)playLocationAudio{

    NSData *data = [[NSData alloc] initWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"lalala.mp3" ofType:nil]];
    
    _player = [[AVAudioPlayer alloc] initWithData:data error:nil];   //初始化
    
    __weak typeof(self) weakSelf = self;
    [self.view addSystemButtonWithFrame:CGRectMake(100, 100, 100, 30) title:@"播放" action:^(UIButton *button) {
        //
        [weakSelf.player play];
    }];  //加按鈕 --> 播放
    
    [self.view addSystemButtonWithFrame:CGRectMake(100, 150, 100, 30) title:@"stop" action:^(UIButton *button) {
        //
        [weakSelf.player stop];   //注:stop , pause 暫停
        weakSelf.player.currentTime = 0;   //設置從頭播放
        weakSelf.playProgressView.progress = 0;
        
    }];  //加按鈕 --> stop
    
    //進度控制(UIProgressView)
    _playProgressView = [[UIProgressView alloc] initWithFrame:CGRectMake(20, 200, 280, 20)];
    [self.view addSubview:_playProgressView];
    
    [NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(dealTimer:) userInfo:nil repeats:YES];
    //添加手勢
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(dealTap:)];
    [_playProgressView addGestureRecognizer:tap];
    
    //音量控制(UISlider)
    _volumeSlider = [[UISlider alloc] initWithFrame:CGRectMake(20, 250, 280, 20)];
    [self.view addSubview:_volumeSlider];
    _volumeSlider.maximumValue = 10;
    _volumeSlider.minimumValue = 0;
    [_volumeSlider addTarget:self action:@selector(dealSlider:) forControlEvents:UIControlEventValueChanged];

}
#pragma mark - 音量控制
-(void)dealSlider:(UISlider *)silder{

    _player.volume = _volumeSlider.value;  //調節當前音量與slider對應
}
#pragma mark - 進度控制
-(void)dealTimer:(NSTimer *)timer{

    if(_player.duration != 0){
        //進度 = 當前時間/總共時間
        _playProgressView.progress = _player.currentTime/_player.duration;
    }
}
-(void)dealTap:(UITapGestureRecognizer *)tap{

    CGPoint point = [tap locationInView:_playProgressView];
    NSLog(@"point.x = %f",point.x);
    //設置點擊到的值
    double time = _player.duration * (point.x / _playProgressView.frame.size.width);
    NSLog(@"time = %f",time);
    
    _player.currentTime = time;  //
    
}

  (2)播放在線音頻

#pragma mark - 播放在線的music
-(void)audioPalyURLMusicUseDemo{

    //下載地址  http://yinyueshiting.baidu.com/data2/music/239130183/1226741191429509661128.mp3?xcode=401ee63dcc7ece3e28ee0f6f3c6ea015c3ba3c8b80064e3b
    NSString *urlStr = @"http://yinyueshiting.baidu.com/data2/music/239130183/1226741191429509661128.mp3?xcode=401ee63dcc7ece3e28ee0f6f3c6ea015c3ba3c8b80064e3b";
    _streamer = [[AudioStreamer alloc] initWithURL:[NSURL URLWithString:urlStr]];
    
    __weak typeof(self) weakSelf = self;
    [self.view addSystemButtonWithFrame:CGRectMake(100, 100, 100, 30) title:@"播放" action:^(UIButton *button) {
        //
        [weakSelf.streamer start];
    }];  //加按鈕 --> 播放
    
    [self.view addSystemButtonWithFrame:CGRectMake(100, 150, 100, 30) title:@"stop" action:^(UIButton *button) {
        //
        [weakSelf.streamer stop];
        
    }];  //加按鈕 --> stop

    //..........
}

  (3)播放視頻

//----------------3.播放視頻-----------------------------------
    //播放本地視頻文件
    //1.加庫-->MediaPlayer.framework  ,頭文件
#pragma mark - 播放視頻文件
-(void)audioMedioUseDemo{
    //播放本地視頻文件
    __weak typeof(self) weakSelf = self;
    [self.view addSystemButtonWithFrame:CGRectMake(100, 100, 100, 30) title:@"播放本地文件" action:^(UIButton *button) {
        //獲取本地視頻文件-->播放
        NSString *path = [[NSBundle mainBundle] pathForResource:@"dzs.mp4" ofType:nil];
        MPMoviePlayerViewController *moviePlayer = [[MPMoviePlayerViewController alloc] initWithContentURL:[NSURL fileURLWithPath:path]];
        [weakSelf presentViewController:moviePlayer animated:YES completion:^{
           //
        }];
        
    }];  //加按鈕 --> 播放
    
}
#pragma mark - 播放url視頻文件
-(void)audioURLMedioUseDemo{
    //播放網絡視頻文件
    __weak typeof(self) weakSelf = self;
    [self.view addSystemButtonWithFrame:CGRectMake(100, 100, 100, 30) title:@"播放URL文件" action:^(UIButton *button) {
        //
        NSString *urlString = @"http://k.youku.com/player/getFlvPath/sid/842953390607212440d37_00/st/mp4/fileid/0300080400552FC1BC6E56087AC0925DEDEBDD-A820-CF65-A99F-02EF5D8626DD?K=d576e3f832ea0e35261e4bfe&ctype=12&ev=1&oip=1931322792&token=8474&ep=eiaWE0yOVccA4yPYij8bYyjiIHQIXP4J9h%2BFidJmALshTerJ6j%2BjtJvFS%2FlCHv5oASIPEu6F3qSSGDRgYYFLq2gQrDiuPPqR9%2FmV5a1RseYFEhEwcMujwFSWRzf1";
        MPMoviePlayerViewController *moviePlayer = [[MPMoviePlayerViewController alloc] initWithContentURL:[NSURL URLWithString:urlString]];
        [weakSelf presentViewController:moviePlayer animated:YES completion:^{
            //
        }];
        
    }];  //加按鈕 --> 播放
    
    /******播放直播文件**********/
    //直播網址: http://219.232.160.141:5080/hls/c64024e7cd451ac19613345704f985fa.m3u8
    [self.view addSystemButtonWithFrame:CGRectMake(100, 200, 100, 30) title:@"播放直播文件" action:^(UIButton *button) {
        //
        NSString *urlString = @"http://219.232.160.141:5080/hls/c64024e7cd451ac19613345704f985fa.m3u8";
        MPMoviePlayerViewController *moviePlayer = [[MPMoviePlayerViewController alloc] initWithContentURL:[NSURL URLWithString:urlString]];
        [weakSelf presentViewController:moviePlayer animated:YES completion:^{
            //
        }];
        
    }];  //加按鈕 --> 播放
}

2、iPad編程

// 1.ipad ,iphone 的區別 --大小,缺乏硬件(電話,短信...),適配,多了2個控件
    //ipad -->size
    CGSize size = [UIScreen mainScreen].bounds.size;
    NSLog(@"%f, %f",size.width,size.height);   //ipad2 : 768*1024
                                            //iPad Air : 768*1024
                                            //iPad Retina : 768*1024
    //適配問題: 最好弄個iPad版本
    
    /*
     2.iPad 特有控件  (2個)
        UISplitViewController   -- 分割視圖控制器
        ---界面間的傳值-----反向傳值

UIPopoverController -- 彈出控制器
*/
#import "AppDelegate.h"
#import "MasterViewController.h"
#import "DetailViewController.h"

@interface AppDelegate ()

@end

@implementation AppDelegate


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Override point for customization after application launch.
    
    MasterViewController *master = [[MasterViewController alloc] init];  //主視圖
    DetailViewController *dvc = [[DetailViewController alloc] init];  
    UINavigationController *nvc = [[UINavigationController alloc] initWithRootViewController:dvc];
    
    UISplitViewController *svc = [[UISplitViewController alloc] init];
    svc.viewControllers = @[master,nvc];
    svc.delegate = dvc;
    
    self.window.rootViewController = svc;
    
    return YES;
}
相關文章
相關標籤/搜索