6.19 Media 音频&视频

来源:互联网 发布:实时黄金价格软件 编辑:程序博客网 时间:2024/04/29 20:08

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
原创粉丝点击