分享一个好用的快递查询API
1. 关于AVAudioPlayer相关介绍
1.1 使用AVAudioPlayer播放音频,步骤相对简单,先建立一个AVAudioPlayer对象,再利用这个对象对音频数据进行相关的操作。
1.2 AVAudioPlayer主要有一下三类方法
(1)进行播放控制
方法的含义从英文名中可以很容易看出
- (BOOL)prepareToPlay; /* get ready to play the sound. happens automatically on play. */
- (BOOL)play; /* sound is played asynchronously. */
- (BOOL)playAtTime:(NSTimeInterval)time NS_AVAILABLE(10_7, 4_0); /* play a sound some time in the future. time is an absolute time based on and greater than deviceCurrentTime. */
- (void)pause; /* pauses playback, but remains ready to play. */
- (void)stop; /* stops playback. no longer ready to play. */
(2)加载音频文件
- (nullable instancetype)initWithContentsOfURL:(NSURL *)url error:(NSError **)outError;
- (nullable instancetype)initWithData:(NSData *)data error:(NSError **)outError;
- (nullable instancetype)initWithContentsOfURL:(NSURL *)url fileTypeHint:(NSString * __nullable)utiString error:(NSError **)outError NS_AVAILABLE(10_9, 7_0);
- (nullable instancetype)initWithData:(NSData *)data fileTypeHint:(NSString * __nullable)utiString error:(NSError **)outError NS_AVAILABLE(10_9, 7_0);
(3)利用如下属性获得音频文件的相关信息(以下只列出常用的)
@property(readonly, getter=isPlaying) BOOL playing; /* is it playing or not? */
@property(readonly) NSUInteger numberOfChannels;//得到音频的声道数目
@property(readonly) NSTimeInterval duration; /* the duration of the sound. */
@property NSTimeInterval currentTime;/* returns the current time associated with the output device */
更多内容可以参考AVAudioPlayer官方API。
2. 实现简单的播放器
2.1 功能介绍
简单的不能再简单:播放,暂停,停止。动态显示播放时间长度
程序截图如下
全部的程序代码放到了GitHub上: LeeLom GitHub
2.2 实现步骤介绍
(一)首先在Storyboard中建立一个UIProgressView,两个UIButton,一个UILabel。并且为他们分别建立Outlet。UIButton还要建立Action。
#import <UIKit/UIKit.h>
@interface ViewController : UIViewController
@property (strong, nonatomic) IBOutlet UIButton *btn1;
@property (strong, nonatomic) IBOutlet UIButton *btn2;
@property (strong, nonatomic) IBOutlet UILabel *show;
@property (strong, nonatomic) IBOutlet UIProgressView *prog;
- (IBAction)playMusic:(id)sender;
- (IBAction)StopMusic:(id)sender;
@end
(二)具体实现代码如下:
#import "ViewController.h"
#import <AVFoundation/AVFoundation.h>
@interface ViewController ()<AVAudioPlayerDelegate>
@end
@implementation ViewController
AVAudioPlayer* audioPlayer;
UIImage* playImage;
UIImage* stopImage;
UIImage* pauseImage;
NSTimer* timer;
CGFloat durationTime;
- (void)viewDidLoad {
[super viewDidLoad];
//初始化变量
playImage = [UIImage imageNamed:@"Play.png"];
stopImage = [UIImage imageNamed:@"Stop.png"];
pauseImage = [UIImage imageNamed:@"Pause.png"];
//设置按钮图片
[self.btn1 setImage:playImage forState:UIControlStateNormal];
[self.btn2 setImage:stopImage forState:UIControlStateNormal];
//获取音频文件
NSURL* fileURL = [[NSBundle mainBundle]URLForResource:@"bzj" withExtension:@".mp3"];
//创建AVAudioPlayer对象
audioPlayer = [[AVAudioPlayer alloc]initWithContentsOfURL:fileURL error:nil];
//设置UILabel输出信息
NSString* msg = [NSString stringWithFormat:@"音频声道数目:%lu",(unsigned long)audioPlayer.numberOfChannels];
self.show.text = msg;
//UIlabel自动换行
self.show.lineBreakMode = NSLineBreakByWordWrapping;
self.show.numberOfLines = 0;
durationTime = audioPlayer.duration;
audioPlayer.delegate = self;
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
-(void)audioPlayerDidFinishPlaying:(AVAudioPlayer *)player successfully:(BOOL)flag{
if (player == audioPlayer && flag) {
NSLog(@"播放完成");
[self.btn1 setImage:playImage forState:UIControlStateNormal];
}
}
-(void)audioPlayerBeginInterruption:(AVAudioPlayer *)player{
if (player == audioPlayer) {
NSLog(@"被打断");
}
}
- (IBAction)playMusic:(id)sender {
if (audioPlayer.playing) {
//暂停播放
[self.btn1 setImage:pauseImage forState:UIControlStateNormal];
[audioPlayer pause];
}
else{
//播放音频
[self.btn1 setImage:playImage forState:UIControlStateNormal];
[audioPlayer play];
}
if (timer == nil) {
timer = [NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(updateProg) userInfo:nil repeats:YES];
}
}
- (IBAction)StopMusic:(id)sender {
[audioPlayer stop];
[timer invalidate];
timer = nil;
//音频数据初始化
self.prog.progress = 0;
audioPlayer.currentTime = 0;
}
-(void)updateProg{
self.prog.progress = audioPlayer.currentTime/durationTime;
}
@end