6.19 Media 音频&视频

1,Audio

// 引入相应的框架
@import AVFoundation;

@interface MusicViewController () <AVAudioPlayerDelegate>

@property(strong,nonatomic)AVAudioPlayer        *player;
- (void)initlializeDataSource
{
    //1,创建URL
    NSURL *url = [[NSBundle mainBundle] URLForResource:@"Release My Soul" withExtension:@"mp3"];
    //2,创建player
    self.player = [[AVAudioPlayer 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 - AVAudioPlayerDelegate
//AVAudioPlayerDelegate
- (void)audioPlayerDidFinishPlaying:(AVAudioPlayer *)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
{

}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值