//音频播放器
// ViewController.h
// AVAudioPlayer
//
// Created by mouweng on 17/8/26.
// Copyright © 2017年 mouweng. All rights reserved.
//
#import <UIKit/UIKit.h>
//导入音频播放系统库文件
#import <AVFoundation/AVFoundation.h>
@interface ViewController : UIViewController<AVAudioPlayerDelegate>//音频播放协议
{
//播放暂停停止按钮
UIButton *_btnPlay;
UIButton *_btnPause;
UIButton *_btnStop;
UIProgressView *_musicProgress; //音乐播放进度
UISlider *_volumeSlider; //声音大小调整滑动条
UISwitch *_volumeOn; //静音开关
AVAudioPlayer *_player ; //音频播放器
NSTimer *_timer; //定义一个定时器,可以跟进进度
}
@end
//
// ViewController.m
// AVAudioPlayer
//
// Created by mouweng on 17/8/26.
// Copyright © 2017年 mouweng. All rights reserved.
//
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
_btnPlay = [UIButton buttonWithType:UIButtonTypeRoundedRect];
_btnPlay.frame = CGRectMake(100, 100, 100, 40);
[_btnPlay setTitle:@"播放" forState:UIControlStateNormal];
[_btnPlay addTarget:self action:@selector(pressPlay) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:_btnPlay];
_btnPause = [UIButton buttonWithType:UIButtonTypeRoundedRect];
_btnPause.frame = CGRectMake(100, 160, 100, 40);
[_btnPause setTitle:@"暂停" forState:UIControlStateNormal];
[_btnPause addTarget:self action:@selector(pressPause) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:_btnPause];
_btnStop = [UIButton buttonWithType:UIButtonTypeRoundedRect];
_btnStop.frame = CGRectMake(100, 220, 100, 40);
[_btnStop setTitle:@"停止" forState:UIControlStateNormal];
[_btnStop addTarget:self action:@selector(pressStop) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:_btnStop];
//创建进度条
_musicProgress = [[UIProgressView alloc] initWithFrame:CGRectMake(10, 300, 300, 20)];
_musicProgress.progress = 0;
[self.view addSubview:_musicProgress];
//创建声音条
_volumeSlider = [[UISlider alloc] initWithFrame:CGRectMake(10, 380, 300, 20)];
_volumeSlider.maximumValue = 100;
_volumeSlider.minimumValue = 0;
_volumeSlider.value = 50;
[_volumeSlider addTarget:self action:@selector(volumeChange:) forControlEvents:UIControlEventValueChanged];
[self.view addSubview:_volumeSlider];
_player.delegate = self;
[self createPlayer];
}
//当音乐放完时调用
- (void) audioPlayerDidFinishPlaying:(AVAudioPlayer *)player successfully:(BOOL)flag
{
[_timer invalidate];
}
- (void)createPlayer
{
NSString *str = [[NSBundle mainBundle] pathForResource:@"Music" ofType:@"mp3"];
NSURL *_urlMusic = [NSURL fileURLWithPath:str];
//创建音频播放器对象2
_player = [[AVAudioPlayer alloc] initWithContentsOfURL:_urlMusic error:nil];
[_player prepareToPlay];//准备播放(转码解码工作)
_player.numberOfLoops = 1;//循环播放的次数(-1表示无限循环)
_player.volume = 0.5;//设置音量大小
//第一个时间间隔0.1秒调用一次
_timer = [NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(updateTime) userInfo:nil repeats:YES];//定义一个定时器对象,用于跟新进度条
}
- (void)pressPlay
{
NSLog(@"play");
[_player play];
}
- (void)pressPause
{
NSLog(@"pause");
[_player pause];
}
- (void)pressStop
{
NSLog(@"stop");
[_player stop];
_player.currentTime = 0;//当前播放时前清零
}
- (void)volumeChange:(UISlider *)slider
{
NSLog(@"%f",slider.value);
_player .volume = slider.value/100;
}
- (void)updateTime
{
_musicProgress.progress = _player.currentTime/_player.duration;
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
//视频播放器
// ViewController.h
// MPMoviePlayerViewConrroller
//
// Created by mouweng on 17/8/27.
// Copyright © 2017年 mouweng. All rights reserved.
//
#import <UIKit/UIKit.h>
//导入苹果的官方播放器头文件(封装了音频视频的播放类和其编码解码基础类库)
#import <MediaPlayer/MediaPlayer.h>
@interface ViewController : UIViewController
{
//定义一个播放器对象
MPMoviePlayerController *_playerController;
//定义一个播放器视图控制器
MPMoviePlayerViewController *_playerView;
}
@end
//
// ViewController.m
// MPMoviePlayerViewConrroller
//
// Created by mouweng on 17/8/27.
// Copyright © 2017年 mouweng. All rights reserved.
//
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (void) touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
NSString *strURL = @"/Users/apple/Desktop/QQ20170826-185026.mp4";
NSURL *url = [NSURL fileURLWithPath:strURL];
// _playerController = [[MPMoviePlayerController alloc] initWithContentURL:url];
// _playerController.view.frame = self.view.bounds;
// [_playerController prepareToPlay];//视频处理的编解码的过程
// [self.view addSubview:_playerController.view];
//播放试图控制器
_playerView = [[MPMoviePlayerViewController alloc] initWithContentURL:url];
_playerView.view.frame = self.view.bounds;
[self.view addSubview:_playerView.view];
}
@end