Men的博客

欢迎光临!

0%

AVAudioPlayer

创建播放器对象

NSString *path=[[NSBundle mainBundle]pathForResource:@”lalala.mp3” ofType:nil];
_player =[[AVAudioPlayer alloc]initWithContentsOfURL:[NSURL fileURLWithPath:path] error:nil];
//预加准备
[_player prepareToPlay];

//添加一个按钮:开始播放
[self.view addSystemButtonWithFrame:CGRectMake(100, 100, 100, 30) title:@”播放” action:^(UIButton *button) {
[_player play];
}];

//添加一个按钮:停止播放
[self.view addSystemButtonWithFrame:CGRectMake(100, 150, 100, 30) title:@”暂停” action:^(UIButton *button) {
[_player pause];
}];
//滑块控件
_slider=[[UISlider alloc]initWithFrame:CGRectMake(100, 200, 100, 30)];
[self.view addSubview:_slider];
[_slider addTarget:self action:@selector(dealSlider:) forControlEvents:UIControlEventValueChanged];
_slider.value=1;
进度条
_progressView=[[UIProgressView alloc]initWithFrame:CGRectMake(50, 50, 200, 20)];
[self.view addSubview:_progressView];
[NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(dealTimer:) userInfo:nil repeats:YES];

}

-(void)dealTimer:(NSTimer *)t
{
_progressView.progress=_player.currentTime / _player.duration;
}
-(void)dealSlider:(UISlider *)s
{
_player.volume=s.value;
}

网络音乐的播放

1.使用AudioStreamer.h开源库
配置开源库
创建对象
{
AudioStreamer *_streamer;
}

NSString *urlString=@”http://music.baidu.com/data/music/file?link=http://yinyueshiting.baidu.com/data2/music/134378654/2191061104400128.mp3?xcode=ffd5ff02dfd6ef1a480aa81e262460cd6e2eede49a06f7cb&song_id=2191061";
// 如果是self调用时的警告可以
// __weak UIViewController *vc=self;

__block typeof (_streamer) t=_streamer;

[self.view addSystemButtonWithFrame:CGRectMake(100, 100, 100, 30) title:@”播放” action:^(UIButton *button) {
t =[[AudioStreamer alloc]initWithURL:[NSURL URLWithString:urlString]];
[t start];
}];

播放本地视频文件

//1、配置
//添加mediaPlayer
//如何播放在线视频文件

#import “ViewController.h”

#import <MediaPlayer/MediaPlayer.h>

#import “UIView+ZJQuickControl.h”

  • (void)viewDidLoad
    {
    [super viewDidLoad];

[self.view addSystemButtonWithFrame:CGRectMake(100, 100, 100, 30) title:@”播放” action:^(UIButton *button) {

NSString *path=[[NSBundle mainBundle]pathForResource:@”dzs.mp4” ofType:nil];
MPMoviePlayerViewController *mpvc=[[MPMoviePlayerViewController alloc]initWithContentURL:[NSURL fileURLWithPath:path]];
[self presentViewController:mpvc animated:YES completion:nil];
}];

播放在线视频

//http://quiet.local/baoman.mp4

[self.view addSystemButtonWithFrame:CGRectMake(100, 100, 100, 30) title:@”播放网络mp4” action:^(UIButton *button) {

NSString *path=@”http://quiet.local/baoman.mp4";
MPMoviePlayerViewController *mpvc=[[MPMoviePlayerViewController alloc]initWithContentURL:[NSURL URLWithString:path]];
[self presentViewController:mpvc animated:YES completion:nil];
}];