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播放音乐的更多相关文章

  1. iOS-----使用AVAudioPlayer播放音乐

    使用AVAudioPlayer播放音乐 AVAudioPlayer是一个属于AVFoundation.framework的类.它作用类似于一个功能强大的播放器.AVAudioPlayer支持广泛的音频 ...

  2. iOS音频与视频的开发(一)-使用AVAudioPlayer播放音乐、使用AVPlayerViewController播放视频

    iOS的多媒体支持非常强大,它提供了多套支持多媒体的API,无论是音频.视频的播放,还是录制,iOS都提供了多种API支持.借助于这些API的支持,iOS应用既可以查看.播放手机相册中的照片.视频,也 ...

  3. iOS8 用AVAudioPlayer播放音乐(Swift)

    AVAudioPlayer 类提供了播放音频文件的功能,在本次教程中,我们将对一个音乐文件进行播放暂停和停止操作,此外还会显示标题和播放时间.本次教程使用iOS8和Xcod6.3.1 打开Xcode创 ...

  4. iOS -- AVAudioPlayer播放音乐

    一. AVAudioPlayer:                          声明音乐控件AVAudioPlayer,必须声明为全局属性变量,否则可能不会播放,AVAudioPlayer只能播 ...

  5. ios学习:AVAudioPlayer播放音乐文件及读取ipod库中的音乐文件

    首先要导入AVFoundation框架及 #import <AVFoundation/AVFoundation.h>头文件 注意:要在真机上调试 下面是ipad上的调试效果 下面是代码,代 ...

  6. iOS开发系列--扩展--播放音乐库中的音乐

    众所周知音乐是iOS的重要组成播放,无论是iPod.iTouch.iPhone还是iPad都可以在iTunes购买音乐或添加本地音乐到音乐 库中同步到你的iOS设备.在MediaPlayer.fram ...

  7. iPhone播放音乐

    来源:http://blog.csdn.net/htttw/article/details/7842295 iPhone播放音乐 今天我们简要介绍如何在iPhone中播放音乐: 强烈建议你参考官方文档 ...

  8. IOS播放音乐和音效

    1.播放音效 1.1 首先获取到音效文件路径 NSString *path = [[NSBundle mainBundle] pathForResource:soundFileName ofType: ...

  9. IOS AVAUDIOPLAYER 播放器使用

    1. 导入 AVFoundation.framework 2.导入头文件  #import <AVFoundation/AVFoundation.h> 3. player = [[AVAu ...

随机推荐

  1. 复数 一级ADT实现

    COMPLEX.h /* typedef struct { float RE; //实部 float IM; //虚部 }Complex; */ typedef struct complex * Co ...

  2. 2017-2018-1 20155320加分项目——pwd的实现

    2017-2018-1 20155320加分项目--pwd的实现 1 学习pwd命令 2 研究pwd实现需要的系统调用(man -k; grep),写出伪代码 3 实现mypwd 4 测试mypwd ...

  3. 20155333 2016-2017-2 《Java程序设计》第三周学习总结

    20155333 2016-2017-2 <Java程序设计>第三周学习总结 教材学习内容总结 第四章 类定义时使用class关键词,名称使用Clothes,建立实例要使用new关键词. ...

  4. vim 查找

    一.用/和?的区别:/后跟查找的字符串.vim会显示文本中第一个出现的字符串.?后跟查找的字符串.vim会显示文本中最后一个出现的字符串.二.注意事项:不管用/还是?查找到第一个字符串后,按回车,vi ...

  5. [agc001E]BBQ Hard[组合数性质+dp]

    Description 传送门 Solution 题目简化后要求的实际上是$\sum _{i=1}^{n-1}\sum _{j=i+1}^{n}C^{A[i]+A[j]}_{A[i]+A[j]+B[i ...

  6. struts2官方 中文教程 系列四:Action

    先贴个本帖的地址,免得其它网站被爬去了struts2教程 官方系列四:Action  即 http://www.cnblogs.com/linghaoxinpian/p/6905521.html 下载 ...

  7. cogs1341 永无乡

    cogs1341 永无乡 打了一发替罪羊树. 鬼故事:替罪羊树去掉重构(变成裸的二叉排序树)依然跑得过= = 启发式合并.每次把小的里面所有东西往大的里面一丢,每个点最多被丢\(log_2n\)次(丢 ...

  8. 180718-jar包执行传参使用小结

    jar包执行时传参的使用姿势 虽说我们现在大多不太直接使用jar包运行方式,目前比较主流的是将自己的服务丢在某个容器中(如tomcat,jetty等)运行,比如我之前所属的电商公司,就是将项目打包为w ...

  9. Hive中使用sql的注意事项

    一.别名的使用 定义别名:columnA as X 不需要使用单引号 使用别名:不与where同时使用 花式报错-->有说hive不支持where后使用别名 二.GROUP BY select ...

  10. 生成dataset的几种方式

    1.常用的方式通过sparksession读取外部文件或者数据生成dataset(这里就不讲了)  注: 生成Row对象的方法提一下:RowFactory.create(x,y,z),取Row中的数据 ...