AVAudioPlayer播放音乐
1:首先创建一个新的项目,继承自UIViewController
2:导入框架AVFoundation.framework
右键工程名,在Build Phases的Link Binary With Libraries中的+号,找到AVFoundation.framework添加即可
3,导入音乐
4:添加代理AVAudioPlayerDelegate
5代码如下
//
// ViewController.m
// PlayMusic
//
// Created by summer on 16/4/4.
// Copyright © 2016年 summer. All rights reserved.
//
#import "ViewController.h"
#import <AVFoundation/AVFoundation.h>
@interface ViewController ()<AVAudioPlayerDelegate>
@property(nonatomic,strong)AVAudioPlayer *player;
@property(nonatomic,strong)NSTimer *timer;
@property(nonatomic,strong) UISlider *slider;
@property(nonatomic,strong)UIProgressView *progress;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
//添加音乐
NSURL *url=[[NSBundle mainBundle]URLForResource:@"Need You Now.wav" withExtension:Nil];
NSLog(@"%@",url);
self.player=[[AVAudioPlayer alloc]initWithContentsOfURL:url error:Nil];
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error:nil ];
//设置音量大小
self.player.volume=5;
//设置循环次数-1是无限循环,1就是1次
self.player.numberOfLoops=1;
//设置代理
self.player.delegate=self;
//准备播放
[self.player prepareToPlay];
//添加3个button,和响应事件
UIButton *btnPlay=[[UIButton alloc]initWithFrame:CGRectMake(100, 100, 120, 50)];
btnPlay.backgroundColor=[UIColor cyanColor];
[btnPlay.layer setMasksToBounds:YES];
[btnPlay.layer setCornerRadius:5];
[btnPlay setTitle:@"点击播放音乐" forState:UIControlStateNormal];
[btnPlay addTarget:self action:@selector(PlayMusic) forControlEvents:UIControlEventTouchUpInside];
UIButton *btnSuspend=[[UIButton alloc]initWithFrame:CGRectMake(100, 200, 120, 50)];
btnSuspend.backgroundColor=[UIColor cyanColor];
[btnSuspend.layer setMasksToBounds:YES];
[btnSuspend.layer setCornerRadius:5];
[btnSuspend setTitle:@"点击暂停音乐" forState:UIControlStateNormal];
[btnSuspend addTarget:self action:@selector(SuspendMusic) forControlEvents:UIControlEventTouchUpInside];
UIButton *btnStop=[[UIButton alloc]initWithFrame:CGRectMake(100, 300, 120, 50)];
btnStop.backgroundColor=[UIColor cyanColor];
[btnStop.layer setMasksToBounds:YES];
[btnStop.layer setCornerRadius:5];
[btnStop setTitle:@"点击停止音乐" forState:UIControlStateNormal];
[btnStop addTarget:self action:@selector(StopMusic) forControlEvents:UIControlEventTouchUpInside];
//设置一个switch,添加事件
UISwitch *swi=[[UISwitch alloc]initWithFrame:CGRectMake(100, 380, 100, 40)];
swi.on=YES;
[swi addTarget:self action:@selector(switchChange:) forControlEvents:UIControlEventValueChanged];
//设置一个进度条,用 NStimer来更新进度条
self.progress=[[UIProgressView alloc]initWithFrame:CGRectMake(100, 430, 200, 30)];
self.timer=[NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(playProgress) userInfo:nil repeats:YES];
//设置一个音量空的slider,并添加响应事件
self.slider=[[UISlider alloc]initWithFrame:CGRectMake(100, 480, 200, 20)];
[self.slider addTarget:self action:@selector(volumeChange) forControlEvents:UIControlEventValueChanged];
//设置声音的最小值
self.slider.minimumValue=0.0f;
//设置声音的最大值
self.slider.maximumValue=10.0f;
//设置声音的当前值
self.slider.value=5.0f;
//加入视图中显示
[self.view addSubview:self.slider];
[self.view addSubview:self.progress];
[self.view addSubview:swi];
[self.view addSubview:btnPlay];
[self.view addSubview:btnSuspend];
[self.view addSubview:btnStop];
//AudioServicesPlaySystemSound(sound);
// Do any additional setup after loading the view, typically from a nib.
}
-(void) PlayMusic{
//开始播放
[self.player play];
}
-(void)SuspendMusic{
//暂停音乐
[self.player pause];
}
-(void)StopMusic{
//停止播放,音乐播放时间置为0
self.player.currentTime=0;
[self.player stop];
}
-(void)playProgress{
//进度条的时间是当前时间/音乐的总时间
self.progress.progress=self.player.currentTime/self.player.duration;
}
-(void)switchChange:(UISwitch *)sender{
self.player.volume=sender.on;
}
-(void)volumeChange{
self.player.volume=self.slider.value;
}
-(void)audioPlayerDidFinishPlaying:(AVAudioPlayer *)player successfully:(BOOL)flag{
//音乐播放完时调用的事件,都清空
[self.timer invalidate];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
AVAudioPlayer播放音乐的更多相关文章
- iOS-----使用AVAudioPlayer播放音乐
使用AVAudioPlayer播放音乐 AVAudioPlayer是一个属于AVFoundation.framework的类.它作用类似于一个功能强大的播放器.AVAudioPlayer支持广泛的音频 ...
- iOS音频与视频的开发(一)-使用AVAudioPlayer播放音乐、使用AVPlayerViewController播放视频
iOS的多媒体支持非常强大,它提供了多套支持多媒体的API,无论是音频.视频的播放,还是录制,iOS都提供了多种API支持.借助于这些API的支持,iOS应用既可以查看.播放手机相册中的照片.视频,也 ...
- iOS8 用AVAudioPlayer播放音乐(Swift)
AVAudioPlayer 类提供了播放音频文件的功能,在本次教程中,我们将对一个音乐文件进行播放暂停和停止操作,此外还会显示标题和播放时间.本次教程使用iOS8和Xcod6.3.1 打开Xcode创 ...
- iOS -- AVAudioPlayer播放音乐
一. AVAudioPlayer: 声明音乐控件AVAudioPlayer,必须声明为全局属性变量,否则可能不会播放,AVAudioPlayer只能播 ...
- ios学习:AVAudioPlayer播放音乐文件及读取ipod库中的音乐文件
首先要导入AVFoundation框架及 #import <AVFoundation/AVFoundation.h>头文件 注意:要在真机上调试 下面是ipad上的调试效果 下面是代码,代 ...
- iOS开发系列--扩展--播放音乐库中的音乐
众所周知音乐是iOS的重要组成播放,无论是iPod.iTouch.iPhone还是iPad都可以在iTunes购买音乐或添加本地音乐到音乐 库中同步到你的iOS设备.在MediaPlayer.fram ...
- iPhone播放音乐
来源:http://blog.csdn.net/htttw/article/details/7842295 iPhone播放音乐 今天我们简要介绍如何在iPhone中播放音乐: 强烈建议你参考官方文档 ...
- IOS播放音乐和音效
1.播放音效 1.1 首先获取到音效文件路径 NSString *path = [[NSBundle mainBundle] pathForResource:soundFileName ofType: ...
- IOS AVAUDIOPLAYER 播放器使用
1. 导入 AVFoundation.framework 2.导入头文件 #import <AVFoundation/AVFoundation.h> 3. player = [[AVAu ...
随机推荐
- c语言智能指针 附完整示例代码
是的,你没有看错, 不是c++不是c#, 就是你认识的那个c语言. 在很长一段时间里,c的内存管理问题, 层出不穷,不是编写的时候特别费劲繁琐, 就是碰到内存泄漏排查的各种困难, 特别在多线程环境下, ...
- 20155328 2016-2017-2 《Java程序设计》第四周学习总结
学号 2016-2017-2 <Java程序设计>第四周学习总结 教材学习内容总结 继承:避免多各类间重复定义行为,extends关键字表示继承后再扩充原本没有的行为.如果没有使用exte ...
- 虚拟机与Linux的初体验
很早的时候就知道虚拟机这个神奇东西的存在,但也仅仅是只闻其名,未见其身.后来在信息安全素质教育的这门课程上,为了做木马实验.暴力破解实验以及邮件窃取实验,这才比较直接的接触到了虚拟机.当我看着在另一个 ...
- addClass+siblings+removeClass用意:
$(this).addClass("li_add").siblings().removeClass("li_add").children('.floor2'). ...
- c++ 自定义数据结构运用
教学内容: 定义结构 定义结构变量 访问结构成员 定义结构数组 实例运用 例:记录学生到校时间(精确到秒) struct mytime { int hour;//时 int min;//分 ...
- 【洛谷P2245】星际导航
题面 题解 \(kruskal\)重构树板子题??(大雾 因为重构树上两点之间的\(LCA\)的权值就是原图上最小生成树上的瓶颈. 所以建个重构树,跑\(LCA\)即可. 代码 #include< ...
- Elasticsearch6.2集群搭建
Elasticsearch6.2集群搭建 2018年04月02日 11:07:45 这个名字想了很久 阅读数:14154 版权声明:本博客为学习.笔记之用,以笔记形式记录学习的知识与感悟.学习过 ...
- Elasticsearch5.x版本中对Text类型进行聚合时提示illegal_argument_exception
Having this field in my mapping "answer": { "type": "text", "fiel ...
- OpenCL入门:(二:用GPU计算两个数组和)
本文编写一个计算两个数组和的程序,用CPU和GPU分别运算,计算运算时间,并且校验最后的运算结果.文中代码偏多,原理建议阅读下面文章,文中介绍了OpenCL相关名词概念. http://opencl. ...
- ffmpeg 压缩H265 Windows 硬件编码
硬件NVIDIA:ffmpeg.exe -i input.avi -c:v hevc_nvenc -preset:v fast output.mp4 软件 :ffmpeg.exe - ...