1.本地音乐播放
1.导入库:AVFoundation.framework
2.添加头文件:#import <AVFoundation/AVFoundation.h>
代码如下:
#import "ConfigViewController.h"
#import "UIView+DLQuickView.h"
#import <AVFoundation/AVFoundation.h>
@interface ConfigViewController ()
{
// UIProgressView *_playerProgress;
UISlider *_volumeSliser;
}
@property (strong, nonatomic) UIProgressView *playerProgress;
@property (strong, nonatomic) AVAudioPlayer *player;
@end
@implementation ConfigViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
// self.view.backgroundColor = [UIColor redColor];
//本地数据
NSData *data = [[NSData alloc] initWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"lalala.mp3" ofType:nil]];
_player = [[AVAudioPlayer alloc] initWithData:data error:nil];
//弱引用
__weak typeof (self)weakSelf = self;
[self.view addImageButtonWithFrame:CGRectMake(0, 0, 320, 50) title:@"播放" background:nil action:^(UIButton *button) {
[weakSelf.player play];
}];
[self.view addImageButtonWithFrame:CGRectMake(0, 80, 320, 50) title:@"停止" background:nil action:^(UIButton *button) {
[weakSelf.player stop];
weakSelf.playerProgress.progress = 0;
weakSelf.player.currentTime = 0;
}];
//进度显示
_playerProgress = [[UIProgressView alloc] initWithFrame:CGRectMake(50, 150, 253, 30)];
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(dealTap:)];
[_playerProgress addGestureRecognizer:tap];
[self.view addSubview:_playerProgress];
[NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(dealTimer) userInfo:nil repeats:YES];
//滑块控制音量
_volumeSliser = [[UISlider alloc] initWithFrame:CGRectMake(50, 180, 250, 30)];
_volumeSliser.maximumValue = 100;
_volumeSliser.minimumValue = 0;
_volumeSliser.value = 50;
[_volumeSliser addTarget:self action:@selector(clickSlider:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:_volumeSliser];
}
//音量控制
-(void)clickSlider:(UISlider *)slider
{
_player.volume = _volumeSliser.value/100.0;
}
-(void)dealTimer
{
if (_player.duration != 0) {
_playerProgress.progress = _player.currentTime/_player.duration;
}
}
//快进
-(void)dealTap:(UITapGestureRecognizer *)tap
{
CGPoint point = [tap locationInView:_playerProgress];
double time = _player.duration*(point.x/_playerProgress.frame.size.width);
NSLog(@"%f",time);
_playerProgress.progress = time;
_player.currentTime = time;
}
2.播放在线音乐
1.添加第三方框架:AudioStreamer
2.添加头文件
#import "AudioStreamer.h"
代码如下:
@property (strong, nonatomic) AudioStreamer *streamer;
......
[self audioStreamer];
}
-(void)audioStreamer
{
NSString *urlString = @"http://yinyueshiting.baidu.com/data2/music/239130183/1226741191429509661128.mp3?xcode=eee19589e99832ae94afa7066bd00d38c3ba3c8b80064e3b";
_streamer = [[AudioStreamer alloc] initWithURL:[NSURL URLWithString:urlString]];
//弱引用
__weak typeof (self)weakSelf = self;
[self.view addImageButtonWithFrame:CGRectMake(0, 210, 320, 50) title:@"在线音乐" background:nil action:^(UIButton *button) {
[weakSelf.streamer start];
}];
}
3.视频播放
1.库的配置:
#import "UIView+DLQuickView.h"
#import <MediaPlayer/MediaPlayer.h>
2.代码
-(void)mediaPlayer
{
__weak typeof(self) weakSelf = self;
[self.view addImageButtonWithFrame:CGRectMake(0, 0, 320, 100) title:@"播放本地视频" background:nil action:^(UIButton *button) {
NSString *path = [[NSBundle mainBundle] pathForResource:@"dzs.mp4" ofType:nil];
MPMoviePlayerViewController *player = [[MPMoviePlayerViewController alloc] initWithContentURL:[NSURL fileURLWithPath:path]];
[weakSelf presentViewController:player animated:YES completion:nil];
}];
//点播,用FLVCD得到地址
[self.view addImageButtonWithFrame:CGRectMake(0, 200, 320, 50) title:@"播放网络MP4文件" background:nil action:^(UIButton *button) {
NSString *urlString = @"http://k.youku.com/player/getFlvPath/sid/142962067839912ed08c1_00/st/mp4/fileid/030008040055269A84A7BF087AC0922C0A08E6-0A51-5380-BE6C-39580A30167D?K=6a0571f7e57bdeb6261e4c31&ctype=12&ev=1&oip=1931322792&token=5680&ep=cyaWE0%2BPVsgH7SDWgT8bMnjifCQOXP4J9h%2BFidJmALshTeq5kE%2FZw%2By3PIlCHv5oASIPEumCq6HnYjUSYfAyrGkQ2k2gOfqSi%2FaQ5a5awZgAFxo2c8vQwFSbRTjx";
MPMoviePlayerViewController *player = [[MPMoviePlayerViewController alloc] initWithContentURL:[NSURL URLWithString:urlString]];
[weakSelf presentViewController:player animated:YES completion:nil];
}];
//直播M3u8
//点播:想看哪儿就看哪
//点播:类似电视
[self.view addImageButtonWithFrame:CGRectMake(0, 250, 320, 50) title:@"播放直播M3u8文件" background:nil action:^(UIButton *button) {
NSString *urlString = @"http://219.232.160.141:5080/hls/c64024e7cd451ac19613345704f985fa.m3u8";
MPMoviePlayerViewController *player = [[MPMoviePlayerViewController alloc] initWithContentURL:[NSURL URLWithString:urlString]];
[weakSelf presentViewController:player animated:YES completion:nil];
}];
}
来源:https://www.cnblogs.com/NFli/p/4445539.html