用button简单控制音乐的播放与暂停
2016-03-09 22:25
399 查看
ViewController.h
//定义一个按钮button
@property(strong,nonatomic)UIButton *button;
ViewController.m
#import "ViewController.h"
//导入头文件
#import <AVFoundation/AVFoundation.h>
@interface ViewController ()
//定义一个player的属性
@property (nonatomic,strong)AVAudioPlayer *player;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
//获取MP3文件的全路径
NSString *musicfiledpath=[[NSBundle mainBundle] pathForResource:@"刘德华 - 冰雨" ofType:@"mp3"];
//也是获取路径
NSURL *musicURL=[[NSURL alloc] initFileURLWithPath:musicfiledpath];
//创建名叫theplay的对象
AVAudioPlayer *thePlay=[[AVAudioPlayer alloc] initWithContentsOfURL:musicURL error:nil];
//设置button的位置
self.button=[[UIButton alloc] initWithFrame:CGRectMake(150, 150, 100, 50)];
//按钮的名称
[self.button setTitle:@"播放+暂停" forState:UIControlStateNormal];
//按钮的背景色
self.button.backgroundColor=[UIColor grayColor];
[self.view addSubview:self.button];
//响应播放事件
[self.button addTarget:self action:@selector(testplayer) forControlEvents:UIControlEventTouchUpInside];
//创建播放器
self.player=thePlay;
}
-(void)testplayer
{
if(self.button.tag==0)
{
//预播放
[self.player prepareToPlay];
//设置音乐播放的次数,-1为循环播放
self.player.numberOfLoops=-1;
//播放
[self.player play];
self.button.tag=1;
}
else
{
[self.player stop];
self.button.tag=0;
}
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
//定义一个按钮button
@property(strong,nonatomic)UIButton *button;
ViewController.m
#import "ViewController.h"
//导入头文件
#import <AVFoundation/AVFoundation.h>
@interface ViewController ()
//定义一个player的属性
@property (nonatomic,strong)AVAudioPlayer *player;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
//获取MP3文件的全路径
NSString *musicfiledpath=[[NSBundle mainBundle] pathForResource:@"刘德华 - 冰雨" ofType:@"mp3"];
//也是获取路径
NSURL *musicURL=[[NSURL alloc] initFileURLWithPath:musicfiledpath];
//创建名叫theplay的对象
AVAudioPlayer *thePlay=[[AVAudioPlayer alloc] initWithContentsOfURL:musicURL error:nil];
//设置button的位置
self.button=[[UIButton alloc] initWithFrame:CGRectMake(150, 150, 100, 50)];
//按钮的名称
[self.button setTitle:@"播放+暂停" forState:UIControlStateNormal];
//按钮的背景色
self.button.backgroundColor=[UIColor grayColor];
[self.view addSubview:self.button];
//响应播放事件
[self.button addTarget:self action:@selector(testplayer) forControlEvents:UIControlEventTouchUpInside];
//创建播放器
self.player=thePlay;
}
-(void)testplayer
{
if(self.button.tag==0)
{
//预播放
[self.player prepareToPlay];
//设置音乐播放的次数,-1为循环播放
self.player.numberOfLoops=-1;
//播放
[self.player play];
self.button.tag=1;
}
else
{
[self.player stop];
self.button.tag=0;
}
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
相关文章推荐
- 第四十七天
- 自我介绍
- 判断是否是顺子
- centos 7 下 yum -y install ntp 出现/var/run/yum.pid 已被锁定,PID 为 4054 的另一个程序正在运行。
- ConditionVariable在Android应用开发中的用法
- ZeeKeeper 安装和运行
- 区间k大数查询
- c++上机报告1-2
- php之跳转
- 分页置换算法
- php curl (get post)
- 第一次实验/2
- Android Studio 代码自动提示无效
- Struts1和Struts2的区别和对比(完整版)
- Android 获得一个应用的权限
- STL笔记(5)——空间配置器Allocator(三)
- iframe中跨域cookie丢失问题java解决
- 左旋转字符串
- 从上到下按层打印二叉树
- Object-C高级编程读书笔记(3)——Block的变量截取