AVAudioPlayer,AVPlayer 使用
这两者简单使用,话不多说,我们直接来看代码吧。
#import <UIKit/UIKit.h>
#import <AVFoundation/AVFoundation.h> // xcode6.0以后 直接导入该框架即可
@interface ViewController : UIViewController
{
__weak IBOutlet UILabel *_timeLabel;//时间显示Label
}
@property (weak, nonatomic) IBOutlet UISlider *Slider; //播放进度滑动条
@property (weak, nonatomic) IBOutlet UISlider *voliSlider; //播放进度滑动条
@end
#import "ViewController.h"
@interface ViewController ()
{
AVAudioPlayer *_player; //只能播放本地音乐
// AVPlayer *_player; //能播放本地音乐 和 流媒体音乐
//记录播放时间
NSTimer *timer;
// BOOL isPlaying;
}
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
//———————————————————————————AVAudioPlayer————————————————————————————————
// NSURL *url1=[NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"music1.mp3" ofType:nil]];
NSURL *url2=[NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"Taylor Swift - Blank Space [mqms].mp3" ofType:nil]];
//只能播放本地音乐
_player=[[AVAudioPlayer alloc] initWithContentsOfURL:url2 error:nil];
//播放总时间
_Slider.maximumValue=_player.duration;
//———————————————————————————AVPlayer————————————————————————————————————
// NSString *urlPath=@"http://qqmusic.4ftt.com/m/101369814.mp3?20150318133115";
// NSURL *url=[NSURL URLWithString:urlPath];
// //能播放本地音乐 和 流媒体音乐
// _player=[[AVPlayer alloc] initWithURL:url];
//———————————————————————————————————————————————————————————————————————
/*
AVAudioPlayer 与 AVPlayer 共同属性
*/
//播放次数 默认为0(播放一次)1 即是 播放2次 -1或者无穷大的数 为无限循环
_player.numberOfLoops=1;
//播放音量默认1
_player.volume=.5;
//播放音量
_voliSlider.value=.5;
// [_player play]; //播放
// [_player stop]; //停止
// [_player pause]; //暂停
}
//播放事件
- (IBAction)PlayerAction:(UIButton *)sender {
if (_player.isPlaying) {
[_player pause];
[timer invalidate];
}else{
[_player play];
//开启定时器
timer=[NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(timerAction:) userInfo:nil repeats:YES];
}
}
//定时器事件
- (void)timerAction:(NSTimer *)time
{
//播放滑块的值设定为音乐播放当前时间
_Slider.value=_player.currentTime;
NSInteger timerun=(NSInteger)_player.currentTime;
//时间 67 格式转化为 文本 01:07 显示样式
if (timerun%60<10)
{
if (timerun/60>0) {
NSString *test=[NSString stringWithFormat:@"0%ld:0%ld",timerun/60,timerun-60*(timerun/60)];
_timeLabel.text=test;
}else
{
NSString *test=[NSString stringWithFormat:@"0%ld:0%ld",timerun/60,timerun];
_timeLabel.text=test;
}
}else if(timerun%60>=10)
{
if (timerun/60>0) {
NSString *test=[NSString stringWithFormat:@"0%ld:%ld",timerun/60,timerun-60*(timerun/60)];
_timeLabel.text=test;
}else
{
NSString *test=[NSString stringWithFormat:@"0%ld:%ld",timerun/60,timerun];
_timeLabel.text=test;
}
}
}
//播放进度滑动事件
- (IBAction)Slider:(UISlider *)sender {
//播放当前时间等于滑动条滑动的值
_player.currentTime=sender.value;
}
//播放音量滑动事件
- (IBAction)voliAction:(UISlider *)sender {
_player.volume=sender.value;
}
@end
上述控件都在 Main.storyboard 画好了 两个UISlider也连接好了事件,相信这些简单操作难不倒读者吧,好了大家去试一试吧。
AVAudioPlayer,AVPlayer 使用相关推荐
- iphone开发之音频播放类AVAudioPlayer的使用——加纯代码案例
1.本节目标 (1)使用AVAudioPlayer类播放音频 (2)掌握后台播放音乐 (3)自定义后台任务 (5)使用MPMoviePlayer 2.IOS音频播放概述 (1)IOS系统中的音频播放方 ...
- iOS 使用AVAudioPlayer开发录音功能
最近要做一个类似对讲的功能,所以需要用到录音上传,然后再播放的功能. 一.音频格式分析 因为之前没研究过音频这块,所以很多音频格式都是第一次见. AAC: AAC其实是"高级音频编码(adv ...
- ios中AVAudioPlayer音频播放器
IOS中有三种播放音频的方式:AVAudioPlayer.音频服务.音频队列. AVAudioPlayer在AVFoundation框架下,所以我们要导入AVFoundation.framework. ...
- AvaudioPlayer
// // ViewController.m // 播放 // // Created by 草帽~小子 on 2017/6/30. // Copyright © 2017年 HLJ. All ...
- iOS 录音功能的实现
这两天也调了一下ios的录音,原文链接:http://www.iphoneam.com/blog/index.php?title=using-the-iphone-to-record-audio-a- ...
- iOS音频播放(一):概述
(本文转自码农人生) 前言 从事音乐相关的app开发也已经有一段时日了,在这过程中app的播放器几经修改,我也因此对于iOS下的音频播放实现有了一定的研究.写这个 系列的博客目的一方面希望能够抛砖引玉 ...
- iOS音频播放 (二):AudioSession 转
原文出处 :http://msching.github.io/blog/2014/07/08/audio-in-ios-2/ 前言 本篇为<iOS音频播放>系列的第二篇. 在实施前一篇中所 ...
- iOS音频播放 (一):概述 转
2019独角兽企业重金招聘Python工程师标准>>> 基础 先来简单了解一下一些基础的音频知识. 目前我们在计算机上进行音频播放都需要依赖于音频文件,音频文件的生成过程是将声音信息 ...
- Java中AudioFileStream_iOS音频学习一之AudioFileStream
音乐一直是我的爱好,作为一名开发,同时我也想知道这些音乐是怎么播放的,音效是如何改变的,如何升降调,一个音乐播放器是怎么实现的.从而开启我的音频学习之路 基本知识 人耳所能听到的声音,最低的频率是从2 ...
最新文章
- 网络异常_网络异常易频发,流量分析来排查
- ui设计的文字怎样提高设计感呢?
- 《敏捷时代》作者访谈录
- 闷热夏日这个空调太疯狂!
- Android-JSNative交互的几种可行性方案H5白屏问题解决方式
- 逆波兰表达式简单介绍
- linux网络编程——客户端编程
- OpenCV学习(22) opencv中使用kmeans算法
- 激活BI Content
- Excel显示完整的年月日乱码解决方法
- 如何开启深度学习之旅?这三大类125篇论文为你导航(附资源下载)
- 强化学习(reinforcement learning)教程(后面是翻译)
- Luogu1514 引水入城
- Centos安装Lammps教程——intel、openmpi、fftw环境
- JSON值的获取以及遍历 [JSON]
- linux rsh通信实现_RSH的网络通信细节
- 学习笔记0518----nginx和php-fpm配置
- np.mat如何使用
- 全球存算一体技术研究及量产情况最新进展(收录于存算一体芯片赛道投资融资分析)
- 教你用PixiJs实现复杂动画
热门文章
- 武汉工程大学第三届ACM程序设计选拔赛
- oracle12541 linux,PLSQL连接Linux上的oracle数据库出现,ORA-12541 TNS 无监听程序
- 编译阶段打印宏定义的内容
- 不后悔学计算机,这5大专业学了不后悔,越老越吃香,年薪可达50万!
- 智慧城市规划大数据系统软件解决方案
- 制作 JS 广告的简易入门(二)利用 CSS3 技术制作广告
- 【电力电子技术速通】六、逆变电路(一)电压型与电流型逆变电路
- C#报错:未将对象设置到对象实例 Object reference not set to an instance of an object
- P3166 [CQOI2014]数三角形(组合数学)
- 有谁见过八股文的天花板?阿里P8看了也晒干了沉默