AvaudioPlayer
来源:互联网 发布:wps不能使用数据透视表 编辑:程序博客网 时间:2024/06/02 05:04
//
// ViewController.m
// 播放
//
// Created by 草帽~小子 on 2017/6/30.
// Copyright © 2017年 HLJ. All rights reserved.
//
#import "ViewController.h"
#import <AVFoundation/AVFoundation.h>
@interface ViewController ()<AVAudioPlayerDelegate>
@property (nonatomic,strong) AVAudioPlayer *avPlayer;
@property (nonatomic,strong) NSTimer *timer;
@property (nonatomic,strong) UIProgressView *progress;
@end
@implementation ViewController
- (void)viewDidLoad {
[superviewDidLoad];
UIButton *play = [UIButtonbuttonWithType:UIButtonTypeSystem];
play.frame =CGRectMake(75,200, 100,50);
play.backgroundColor = [UIColororangeColor];
[play setTitle:@"play"forState:UIControlStateNormal];
[self.viewaddSubview:play];
[play addTarget:selfaction:@selector(play:)forControlEvents:UIControlEventTouchUpInside];
UIButton *pause = [UIButtonbuttonWithType:UIButtonTypeSystem];
pause.frame =CGRectMake(200,200, 100,50);
pause.backgroundColor = [UIColororangeColor];
[pause setTitle:@"pause"forState:UIControlStateNormal];
[self.viewaddSubview:pause];
[pause addTarget:selfaction:@selector(pause:)forControlEvents:UIControlEventTouchUpInside];
self.progress = [[UIProgressViewalloc] initWithFrame:CGRectMake(20,300, self.view.frame.size.width - 40, 200)];
self.progress.backgroundColor = [UIColor cyanColor];
self.progress.tintColor = [UIColorpurpleColor];
self.progress.progressViewStyle = UIProgressViewStyleDefault;
[self.viewaddSubview:self.progress];
//更改进度条高度
//self.progress.transform = CGAffineTransformMakeScale(1.0f,20.0f);
//UIImage* imgTemp = [UIImage imageNamed:@"btn_sound_h"];
//CGFloat width = imgTemp.size.width/2.0;
//CGFloat height = imgTemp.size.height/2.0;
//UIImage *img = [[UIImage imageNamed:@"btn_manual_n"] resizableImageWithCapInsets:UIEdgeInsetsMake(height, width, height, width)];
//UIImage *imgProgress = [[UIImage imageNamed:@"progress_on.png"]resizableImageWithCapInsets:UIEdgeInsetsMake(height, width, height, width)];
//[self.progress setProgressImage:imgProgress];
//[self.progress setTrackImage:img];
///self.progress.progressImage = img;
//背景图片
//self.progress.trackImage = [UIImage imageNamed:@"btn_sound_h"];
NSLog(@"sdfasdfasfs%@",self.progress.observedProgress);
// Do any additional setup after loading the view, typically from a nib.
}
/**
*定时器
*/
- (NSTimer *)timer {
if (!_timer) {
_timer = [NSTimerscheduledTimerWithTimeInterval:0.5target:selfselector:@selector(timer:)userInfo:nilrepeats:YES] ;
}
return_timer;
}
- (void)timer:(NSTimer *)timer {
float progress =self.avPlayer.currentTime /self.avPlayer.duration;
[self.progresssetProgress:progress animated:YES];
}
/*
*创建播放器
*/
- (AVAudioPlayer *)avPlayer {
if(!_avPlayer) {
//NSURL *url = [[NSBundle mainBundle] URLForResource:@"AVAudioPlayer" withExtension:@"mp3"];
//NSLog(@"%@", url);
//关闭手机左侧静音按钮,会静音,用的手机铃声播放器
NSString *urlStr = [[NSBundlemainBundle] pathForResource:@"AVAudioPlayer.mp3"ofType:nil];
NSURL *url=[NSURLfileURLWithPath:urlStr];
NSError *error =nil;
//初始化播放器,文件路径只支持url,不支持http url
_avPlayer = [[AVAudioPlayeralloc] initWithContentsOfURL:urlerror:&error];
//播放器属性 -1循环
_avPlayer.numberOfLoops =0;
_avPlayer.delegate =self;
//加载文件到缓存区
[_avPlayerprepareToPlay];
if (error) {
returnnil;
}
}
return _avPlayer;
}
/**
*播放音频
*/
- (void)play:(UIButton *)button {
if (![_avPlayerisPlaying]) {
[self.avPlayerplay];
self.timer.fireDate = [NSDatedistantPast];
NSLog(@"sfsfs");
}
}
/**
*暂停播放
*/
- (void)pause:(UIButton *)button {
if ([_avPlayerisPlaying]) {
[_avPlayerpause];
self.timer.fireDate = [NSDate distantFuture];////暂停定时器,注意不能调用invalidate方法,此方法会取消,之后无法恢复
}
}
- AVAudioPlayer
- AVAudioPlayer
- AVAudioPlayer
- AvaudioPlayer
- AVAudioPlayer 类
- 音频AVAudioPlayer
- AVAudioRecorder&AVAudioPlayer
- ios AVAudioPlayer 后台播放
- AVAudioPlayer播放音频文件
- AVAudioPlayer后台暂停bug
- IOS-AVAudioPlayer文档翻译
- AVAudioPlayer 的使用
- UISlider to control AVAudioPlayer
- AVAudioPlayer的使用
- AVAudioPlayer不支持流媒体
- AVPlayer vs. AVAudioPlayer
- 音频播放 AVPlayer AVAudioPlayer
- AVAudioPlayer的使用
- GitHub hello 入门
- poj 1679 The Unique MST (次小生成树)
- 【Firefox os】【kaios】创建一个ipdl
- 深度学习框架caffe及py-faster-rcnn详细配置安装过程
- Guava(七)外面很多坏银地
- AvaudioPlayer
- hdu6126 Give out candies(SPFA+Dinic)
- Docker命令详解
- 成为优秀算法工程师的唯一条件
- SQL的基本曾删改查
- 拓扑排序
- 大数据string八进制转为string十六进制(通过string的二进制)
- caffe框架下目标检测——faster-rcnn实战篇操作
- [RK3288][Android7.1.2] Launcher3 源码阅读之step4:详解Launcher的OnCreate方法的C部分