标签:
一、音频
音乐播放用到一个叫做AVAudioPlayer的类,这个类可以用于播放手机本地的音乐文件。
AVAudioPlayer类依赖于AVFoundation框架,因此使用该类必须先导入AVFoundation框架,并包含其头文件(包含主头文件即可)。
//1.音乐文件的路径
NSString *urlString = [[NSBundle mainBundle] pathForResource:@"王绎龙-邻家美眉" ofType:@"mp3"];
//2.转化成本地路径
NSURL *url = [NSURL fileURLWithPath:urlString];
//3.创建播放对象
_audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:nil];
//5.记录音乐的总时长 duration声音的持续时间
@property(readonly) NSTimeInterval duration;
//6.设置音量的大小
@property float volume
设置播放次数
@property float numberOfLoops = 0;
//播放 暂停 停止
-
(BOOL)play;
-
(void)pause;
-
(void)stop;
//播放进度的改变
@property NSTimeInterval currentTime;
代码:
// 添加背景图片
imageview = [[UIImageView alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
imageview.image = [UIImage imageNamed:@"1.jpg"];
[self.view addSubview:imageview];
// 添加播放按钮
btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
btn.frame = CGRectMake(150, 180, 100, 100);
btn.tintColor = [UIColor redColor];
[btn setTitle:@"播放" forState:UIControlStateNormal];
[btn addTarget:self action:@selector(btnAction:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:btn];
// 添加暂停按钮
btn1 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
btn1.frame = CGRectMake(180, 380, 100, 100);
btn1.tintColor = [UIColor redColor];
[btn1 setTitle:@"暂停" forState:UIControlStateNormal];
[btn1 addTarget:self action:@selector(btn1Action:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:btn1];
// 添加停止按钮
btn2 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
btn2.frame = CGRectMake(130, 380, 30, 100);
btn2.tintColor = [UIColor redColor];
[btn2 setTitle:@"停止" forState:UIControlStateNormal];
[btn2 addTarget:self action:@selector(btn2Action:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:btn2];
//播放事件
-(void)btnAction:(UIButton *)bt
{
if (player == nil){
NSString *path = [[NSBundle mainBundle]pathForResource:@"王绎龙-邻家美眉" ofType:@"mp3"];
NSURL *url = [NSURL fileURLWithPath:path];
player = [[AVAudioPlayer alloc]initWithContentsOfURL:url error:nil];
}
// 开始播放
[player play];
}
//暂停播放事件
-(void)btn1Action:(UIButton *)bt1
{
// 暂停播放
[player pause];
}
//停止播放事件
-(void)btn2Action:(UIButton *)bt2
{
// 播放停止
[player stop];
player = nil;
}
///////////音频代码(block方法)功能////////////
- (void)viewDidLoad {
[super viewDidLoad];
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
dispatch_async(queue, ^{
// 创建一个定时器
[NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(timerAction:) userInfo:nil repeats:YES];
// 保持线程活跃
[[NSRunLoop currentRunLoop] run];
});
// 播放
play1 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
play1.frame = CGRectMake(30, 360, 90, 35);
play1.backgroundColor = [UIColor redColor];
[play1 setTitle:@"播放" forState:UIControlStateNormal];
[play1 addTarget:self action:@selector(playAction:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:play1];
// 停止播放
stop = [UIButton buttonWithType:UIButtonTypeRoundedRect];
stop.frame = CGRectMake(30, 260, 90, 35);
stop.backgroundColor = [UIColor redColor];
[stop setTitle:@"停止" forState:UIControlStateNormal];
[stop addTarget:self action:@selector(stopAction:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:stop];
// 暂停播放
pause = [UIButton buttonWithType:UIButtonTypeRoundedRect];
pause.frame = CGRectMake(30, 160, 90, 35);
pause.backgroundColor = [UIColor redColor];
[pause setTitle:@"暂停" forState:UIControlStateNormal];
[pause addTarget:self action:@selector(pauseAction:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:pause];
// 声音UILabel
sound = [[UILabel alloc] initWithFrame:CGRectMake(90, 210, 100, 20)];
sound.text = @"声音:";
[self.view addSubview:sound];
// 声音UISlider
audioPlayer = [[UISlider alloc] initWithFrame:CGRectMake(150, 170, 200, 100)];
[audioPlayer addTarget:self action:@selector(audioSilderChanged:) forControlEvents:UIControlEventValueChanged];
[self.view addSubview:audioPlayer];
// 进度UISlider
_timeSlider = [[UISlider alloc] initWithFrame:CGRectMake(160, 270, 150, 100)];
[_timeSlider addTarget:self action:@selector(timeSliderChanged:) forControlEvents:UIControlEventValueChanged];
[self.view addSubview:_timeSlider];
// 当前进度
_startTimeLabel = [[UILabel alloc] initWithFrame:CGRectMake(130, 270, 100, 100)];
[self.view addSubview:_startTimeLabel];
// 音乐总长
_endTimeLabel = [[UILabel alloc] initWithFrame:CGRectMake(320, 270, 100, 100)];
[self.view addSubview:_endTimeLabel];
_audioPlayer.delegate = self;
}
- (void)timerAction:(NSTimer *)timer
{
// 回到主线程刷新UI
dispatch_async(dispatch_get_main_queue(), ^{
_startTimeLabel.text = [NSString stringWithFormat:@"%d",(int)_audioPlayer.currentTime];
_timeSlider.value = _audioPlayer.currentTime;
});
}
-(void)pauseAction:(UIButton *)pause{
[_audioPlayer pause];
}
-(void)stopAction:(UIButton *)stop{
[_audioPlayer stop];
_audioPlayer = nil;
}
-(void)audioSilderChanged:(UISlider *)audio{
UISlider *slider = (UISlider *)audioPlayer;
// 7.设置音量的大小
_audioPlayer.volume = slider.value;
}
-(void)playAction:(UIButton *)pl{
if (_audioPlayer == nil) {
// 1.音乐文件的路径
NSString *urlString = [[NSBundle mainBundle] pathForResource:@"王绎龙-邻家美眉" ofType:@"mp3"];
// 2.转化成本地路径
NSURL *url = [NSURL fileURLWithPath:urlString];
// 3.创建播放对象
_audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:nil];
_audioPlayer.numberOfLoops = -1;
// 5.记录音乐的总时长 duration声音的持续时间
_startTimeLabel.text = @"0";
// 音乐总长UILabel
_endTimeLabel.text = [NSString stringWithFormat:@"%d",(int)_audioPlayer.duration];
// 6.设置滑条的参数
_timeSlider.minimumValue = 0;
_timeSlider.maximumValue = _audioPlayer.duration;
// 7.设置音量的大小
_audioPlayer.volume = 0.0;
}
// 5.播放
[_audioPlayer play];
}
-(void)timeSliderChanged:(UISlider *)time{
// 播放进度的改变
_audioPlayer.currentTime = _timeSlider.value;
}
@end
/////////音频(系统)///////////
-----设置支持后台播放------
NSError *error;
AVAudioSession *audionSession = [AVAudioSession sharedInstance];
[audionSession setCategory:AVAudioSessionCategoryPlayback error:&error];
// 1.获取音频的url
NSString *urlString = @"
http://yinyueshiting.baidu.com/data2/music/122092051/693143255600128.mp3?xcode=fcdab48f5e8e1bfeabce6f1886feb9701b23d208676f9a7d";
NSURL *url = [NSURL URLWithString:urlString];
// 2.创建播放对象
// 01.创建播放元素
_playerItem = [[AVPlayerItem alloc] initWithURL:url];
_player = [[AVPlayer alloc] initWithPlayerItem:_playerItem];
// 播放
[_player play];
// 暂停
// [_player pause];
*/
// 3.注册系统声音 (测试一些系统功能的时候,模拟器可能不好使)
// 1.获取路径
NSString *urlString = [[NSBundle mainBundle] pathForResource:@"44th Street Medium" ofType:@"caf"];
NSURL *fileUrl = [NSURL fileURLWithPath:urlString];
// 2.创建创建系统生意的标识
SystemSoundID soundID ;
// 3.注册系统声音
AudioServicesCreateSystemSoundID((__bridge CFURLRef)fileUrl, &soundID)
;
// 4.播放
AudioServicesPlaySystemSound(soundID);
// 设置震动
AudioServicesPlaySystemSound(kSystemSoundID_Vibrate);
}
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
// 获取音乐的总时长
NSLog(@"%lld",_playerItem.duration.value/_playerItem.duration.timescale);
}
///////////音频(代理方式)////////////
button(play、stop、pause暂停)略写
//从budle路径下读取音频文件 轻音乐 - 萨克斯回家这个文件名是你的歌曲名字,mp3是你的音频格式
NSString *string = [[NSBundle mainBundle] pathForResource:@"王绎龙-邻家美眉" ofType:@"mp3"];
//把音频文件转换成url格式
NSURL *url = [NSURL fileURLWithPath:string];
//初始化音频类 并且添加播放文件
avAudioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:nil];
//设置代理
avAudioPlayer.delegate = self;
//设置初始音量大小
// avAudioPlayer.volume = 1;
//设置音乐播放次数 -1为一直循环
avAudioPlayer.numberOfLoops = 0;
//预播放
[avAudioPlayer prepareToPlay];
//初始化一个播放进度条
progressV = [[UIProgressView alloc] initWithFrame:CGRectMake(20, 50, 200, 20)];
[self.view addSubview:progressV];
//用NSTimer来监控音频播放进度
timer = [NSTimer scheduledTimerWithTimeInterval:0.1 target:self
selector:@selector(playProgress)
userInfo:nil repeats:YES];
//初始化音量控制
volumeSlider = [[UISlider alloc] initWithFrame:CGRectMake(20, 70, 200, 20)];
[volumeSlider addTarget:self action:@selector(volumeChange)
forControlEvents:UIControlEventValueChanged];
//设置最小音量
volumeSlider.minimumValue = 0.0f;
//设置最大音量
volumeSlider.maximumValue = 10.0f;
//初始化音量为多少
volumeSlider.value = 5.0f;
[self.view addSubview:volumeSlider];
//声音开关控件(静音)
UISwitch *swith = [[UISwitch alloc] initWithFrame:CGRectMake(100, 20, 60, 40)];
[swith addTarget:self action:@selector(onOrOff:) forControlEvents:UIControlEventValueChanged];
//默认状态为打开
swith.on = YES;
[self.view addSubview:swith];
}
- (void)play
{
[avAudioPlayer play];
}
- (void)pause
{
[avAudioPlayer pause];
}
- (void)stop
{
avAudioPlayer.currentTime = 0;
[avAudioPlayer stop];
}
- (void)playProgress
{
progressV.progress = avAudioPlayer.currentTime/avAudioPlayer.duration;
}
- (void)onOrOff:(UISwitch *)sender
{
avAudioPlayer.volume = sender.on;
}
- (void)volumeChange
{
avAudioPlayer.volume = volumeSlider.value;
}
//播放完成时调用的方法 (代理里的方法),需要设置代理才可以调用
- (void)audioPlayerDidFinishPlaying:(AVAudioPlayer *)player successfully:(BOOL)flag
{
[timer invalidate]; //NSTimer暂停 invalidate 使...无效;
NSLog(@"播放结束才会调用");
}
二、视频
MPMoviePlayerControlle想要实现视频播放也得导入库文件MediaPlayer.framework
//导入头文件
#import <MediaPlayer/MediaPlayer.h>
// 1.自定义播放视图的大小
MPMoviePlayerController *_mpPlayerController;
//
// 1.自定义播放界面
//创建播放对象
_mpPlayerController = [[MPMoviePlayerController alloc] initWithContentURL:url];
// 设置播放视图的大小
_mpPlayerController.view.frame = CGRectMake(20, 20, 320, 140);
//添加的时候要注意_mpPlayerController.view
[self.view addSubview:_mpPlayerController.view];
}
// 播放
[_mpPlayerController play];
代码:
// 弹出播放
Broadcast = [UIButton buttonWithType:UIButtonTypeRoundedRect];
Broadcast.frame = CGRectMake(150, 170, 100, 100);
Broadcast.tintColor = [UIColor redColor];
[Broadcast setTitle:@"播放" forState:UIControlStateNormal];
[Broadcast addTarget:self action:@selector(begingAction:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:Broadcast];
// 暂停
Broadcast1 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
Broadcast1.frame = CGRectMake(150, 260, 100, 100);
Broadcast1.tintColor = [UIColor redColor];
[Broadcast1 setTitle:@"暂停" forState:UIControlStateNormal];
[Broadcast1 addTarget:self action:@selector(begingAction1:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:Broadcast1];
// 停止
Broadcast2 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
Broadcast2.frame = CGRectMake(150, 360, 100, 100);
Broadcast2.tintColor = [UIColor redColor];
[Broadcast2 setTitle:@"停止" forState:UIControlStateNormal];
[Broadcast2 addTarget:self action:@selector(begingAction2:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:Broadcast2];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(exitFullScreen:) name: MPMoviePlayerDidExitFullscreenNotification object:nil];
}
-(void)begingAction:(UIButton *)beging
{
if (_mpPlayerController == nil){
// 1.自定义播放界面
NSURL *url = [NSURL URLWithString:@"
http://vf1.mtime.cn/Video/2012/04/23/mp4/120423212602431929.mp4"];
_mpPlayerController = [[MPMoviePlayerController alloc] initWithContentURL:url];
// 设置播放视图的大小
_mpPlayerController.view.frame = CGRectMake(20, 20, 320, 140);
[self.view addSubview:_mpPlayerController.view];
}
// 播放
[_mpPlayerController play];
}
-(void)begingAction1:(UIButton *)beging1
{
[_mpPlayerController pause];
}
-(void)begingAction2:(UIButton *)beging2
{
[_mpPlayerController stop];
_mpPlayerController = nil;
}
-(void)exitFullScreen:(NSNotification *)notification{
[mpc.view removeFromSuperview];
NSLog(@"remove player");
}
@end
音频视频
标签:
原文地址:http://www.cnblogs.com/wxzboke/p/5044106.html