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 ...
随机推荐
- devise定义多个authentication_keys
在你的model中加入 def self.find_for_database_authentication(warden_conditions) conditions = warden_conditi ...
- Java HashMap 源代码分析
Java HashMap jdk 1.8 Java8相对于java7来说HashMap变化比较大,在hash冲突严重的时候java7会退化为链表,Java8会退化为TreeMap 我们先来看一下类图: ...
- x01.gamelab: An Tank 3D Model
准备 1. 安装 OpenGL 及添加 python 引用参见我的置顶随笔. 2. 下载源代码: http://download.csdn.net/download/china_x01/1013310 ...
- js 修改字符串中某些字符的样式
var str = 'abcdefghijklmnobqrstuvwxyz'; function HightLight(e){ var reg = new RegExp(e, 'g') str = s ...
- Websocket 临时参考网站
https://blog.csdn.net/SGuniver_22/article/details/74273839 https://www.zhihu.com/question/20215561 h ...
- 【转】Excel-VBA操作文件四大方法之三
三.利用FileSystemObject对象来处理文件 FileSystemObject对象模型,是微软提供的专门用来访问计算机文件系统的,具有大量的属性.方法和事件.其使用面向对象的“object. ...
- 20155202 2016-2017-2《Java程序设计》课程总结
20155202 2016-2017-2<Java程序设计>课程总结 (按顺序)每周作业链接汇总 预备作业1:第一次写随笔,我眼中的师生关系--未来我的大学生活 预备作业2:第二次随笔-- ...
- 实验一 Java开发环境的熟悉(Linux+Eclipse)
实验一 Java开发环境的熟悉(Linux+Eclipse) 实验内容及步骤 使用JDK编译.运行简单的Java程序 打开windows下的cmd → 输入cd Code命令进入Code目录 → 输入 ...
- 20155220 2016-2017-2 《java程序设计》第二周学习总结
教材学习内容总结 3.1类型.变量与运算符 1)基本类型 byte 字节型 1 byte short 短整型 2 bytes int 整型 4 bytes long 长整型 8 bytes float ...
- 20155227 2016-2017-2 《Java程序设计》第二周学习总结
20155227 2016-2017-2 <Java程序设计>第二周学习总结 教材学习内容总结 本周学习的第三章内容与以前学过的C语言有很多共通的地方,学习起来还是比较快的. 主要的内容有 ...