您的位置:首页 > 其它

6.19 Media 音频&视频

2015-06-19 18:04 337 查看
1,Audio

// 引入相应的框架
@import ***Foundation;

@interface MusicViewController () <***AudioPlayerDelegate>

@property(strong,nonatomic)***AudioPlayer        *player;


- (void)initlializeDataSource
{
    //1,创建URL
    NSURL *url = [[NSBundle mainBundle] URLForResource:@"Release My Soul" withExtension:@"mp3"];
    //2,创建player
    self.player = [[***AudioPlayer alloc] initWithContentsOfURL:url error:nil];
    //放入缓存池
    [self.player  prepareToPlay];
    //设置代理
    self.player.delegate = self;
    //设置音量(0-1)
    self.player.volume = 0.1;

}


- (void)initlializeUserInterface
{
    //按钮1:音效播放
    //1,创建BUTTON
    UIButton *Button = [UIButton buttonWithType:UIButtonTypeSystem];
    //2. 设置标题
    [Button setTitle:@"音效播放" forState:UIControlStateNormal];
    //3. 设置大小
    [Button setBounds:CGRectMake(0, 0, 80, 100)];
    //4. 设置中心点
    [Button setCenter:CGPointMake(100, 200)];

    //5. 添加到视图
    [self.view addSubview:Button];

    // 按钮2:音乐播放
    //1. 创建button
    UIButton *musicButton = [UIButton buttonWithType:UIButtonTypeCustom];
    //2. 设置标题
    [musicButton setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
    [musicButton setTitle:@"音乐播放" forState:UIControlStateNormal];
    // 3. 设置大小
    [musicButton setBounds:CGRectMake(0, 0, 80, 100)];
    // 4. 设置中心点
    [musicButton setCenter:CGPointMake(300, 200)];
    // 5. 添加到视图
    [self.view addSubview:musicButton];

    // 6. 添加事件
    [Button addTarget:self
               action:@selector(handleButtonEvent:)
     forControlEvents:UIControlEventTouchUpInside];

    [musicButton addTarget:self
                    action:@selector(handleMusicButtonEvent:)
          forControlEvents:UIControlEventTouchUpInside];
}


- (void)handleButtonEvent:(UIButton *)sender
{
    //1,获取URL
    //NSString *path = [[NSBundle mainBundle] pathForResource:@"SystemSound" ofType:@"wav"];

    NSURL *url = [[NSBundle mainBundle] URLForResource:@"SystemSound" withExtension:@"wav"];
    //2.创建音乐播放ID变量
    SystemSoundID systemID = 0;
    //3. 进行函数调用创建系统音乐ID
    AudioServicesCreateSystemSoundID((__bridge CFURLRef)(url),&systemID);
    //4.进行音效播放
    AudioServicesPlaySystemSound(systemID);

}

#pragma mark - ***AudioPlayerDelegate
//***AudioPlayerDelegate
- (void)audioPlayerDidFinishPlaying:(***AudioPlayer *)player successfully:(BOOL)flag
{
//   音乐播放完毕的时候调用该方法
}

//音乐点击按钮
- (void)handleMusicButtonEvent:(UIButton *)sender
{
    if ([self.player isPlaying]) {
        [self.player pause];
    } else {
        [self.player play];
    }

}


2,MediaPlayer

#import "MovieViewController.h"

@import MediaPlayer;

@interface MovieViewController ()

@property (nonatomic, strong) MPMoviePlayerController       *play;
@property (nonatomic, strong) MPMoviePlayerViewController   *playerVC;

- (void)initlializeUserInterface;

@end


@implementation MovieViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self initlializeUserInterface];
}

- (void)initlializeUserInterface
{

    // 按钮1:直接播放
    // 1. 创建button
    UIButton *Button = [UIButton buttonWithType:UIButtonTypeSystem];
    // 2. 设置标题
    [Button setTitle:@"直接播放" forState:UIControlStateNormal];
    // 3. 设置大小
    [Button setBounds:CGRectMake(0, 0, 80, 100)];
    // 4. 设置中心点
    [Button setCenter:CGPointMake(200, 500)];
    // 5. 添加到视图
    [self.view addSubview:Button];

    // 按钮2:全屏播放
    // 1. 创建button
    UIButton *musicButton = [UIButton buttonWithType:UIButtonTypeSystem];
    // 2. 设置标题
    [musicButton setTitle:@"全屏播放" forState:UIControlStateNormal];
    // 3. 设置大小
    [musicButton setBounds:CGRectMake(0, 0, 80, 100)];
    // 4. 设置中心点
    [musicButton setCenter:CGPointMake(100, 500)];
    // 5. 添加到视图
    [self.view addSubview:musicButton];

    // 6. 添加事件
    [Button addTarget:self
               action:@selector(handleButtonEvent:)
     forControlEvents:UIControlEventTouchUpInside];

    [musicButton addTarget:self
                    action:@selector(handleMovieButtonEvent:)
          forControlEvents:UIControlEventTouchUpInside];

}
//直接播放
- (void)handleButtonEvent:(UIButton *)sender
{
    //1,创建URL
    NSURL *url = [[NSBundle mainBundle] URLForResource:@"宣传资料" withExtension:@"mp4"];
    //2,创建player
    MPMoviePlayerController *player = [[MPMoviePlayerController alloc]initWithContentURL:url];
    //3,设置player视图
    player.view.frame = CGRectMake(0, 20, 375, 667);
    //4,设置player视图缩放
    player.scalingMode = MPMovieScalingModeAspectFit;
    //5,添加到视图
    [self.view addSubview:player.view];
    //6,播放plyer
    [player play];

    //7, 对象持有一次,否则无法播放
    self.play = player;
}

//全屏播放
- (void)handleMovieButtonEvent:(UIButton *)sender
{
    //注册通知(当视频播放完毕和用户播放完毕的时候发送通知)
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(handleMovieEvent:) name:MPMoviePlayerPlaybackDidFinishNotification object:nil];

    //1,创建URL
    NSURL *url = [[NSBundle mainBundle] URLForResource:@"宣传资料" withExtension:@"mp4"];
    //2,创建playVC
    self.playerVC = [[MPMoviePlayerViewController alloc]initWithContentURL:url];
    //3,推送
    [self presentMoviePlayerViewControllerAnimated:self.playerVC];
}

- (void)handleMovieEvent:(NSNotification *)not
{

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: