iOS开发之AVFoundation

说再多还不如上代码:吐舌笑脸,当然当你要使用某个框架的时候别忘记导入就是了。
#import <AVFoundation/AVFoundation.h>
#import "FKViewController.h"

@interface FKViewController ()

@end

@implementation FKViewController
//播放器
AVAudioPlayer* audioPlayer;
//播放图片
UIImage* playImage;
//暂停图片
UIImage* pauseImage;
//停止图片
UIImage* stopImage;
//播放时间
CGFloat durationTime;
//定时器
NSTimer* timer;
- (void)viewDidLoad
{
	[super viewDidLoad];
        //初始化图片
	playImage = [UIImage imageNamed:@"play.png"];
	pauseImage = [UIImage imageNamed:@"pause.png"];
	stopImage = [UIImage imageNamed:@"stop.png"];
	// 为两个按钮设置图片
	[self.bn1 setImage:playImage forState:UIControlStateNormal];
	[self.bn2 setImage:stopImage forState:UIControlStateNormal];
	// 获取要播放的音频文件的URL
	NSURL* fileURL = [[NSBundle mainBundle]
					  URLForResource:@"star" withExtension:@"mp3"];
	// 创建AVAudioPlayer对象
	audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:fileURL
		error: nil];
	NSString* msg = [NSString stringWithFormat:@"音频文件的声道数:%lu\n音频文件的持续时间:%g",(unsigned long)audioPlayer.numberOfChannels,
		audioPlayer.duration];
	self.show.text = msg;
	durationTime = audioPlayer.duration;
	// 将循环次数设为-1,用于指定该音频文件循环播放
//	audioPlayer.numberOfLoops = -1;
	// 为AVAudioPlayer设置代理,监听它的播放事件
	audioPlayer.delegate = self;
}
#pragma  mark - delegate for avaudioPlayer
// 当AVAudioPlayer播放完成收将会自动激发该方法
- (void)audioPlayerDidFinishPlaying:(AVAudioPlayer *)player
					   successfully:(BOOL)flag
{
	if (player == audioPlayer && flag)
	{
		NSLog(@"播放完成!!");
		[self.bn1 setImage:playImage forState:UIControlStateNormal];
	}
}
//播放的时候被打断
- (void)audioPlayerBeginInterruption:(AVAudioPlayer *)player
{
	if (player == audioPlayer)
	{
		NSLog(@"被中断!!");
	}
}
#pragma mark - action
- (IBAction)play:(id)sender {
	// 如果当前正在播放
	if (audioPlayer.playing)
	{
		// 暂停播放
		[audioPlayer pause];
		[sender setImage:playImage forState:UIControlStateNormal];
	}
	else
	{
		// 播放音频
		[audioPlayer play];
		[sender setImage:pauseImage forState:UIControlStateNormal];
	}
	// 如果timer为nil,执行如下方法
	if (timer == nil)
	{
		// 周期性地执行某个方法
		timer = [NSTimer scheduledTimerWithTimeInterval:0.1 target:self
			selector:@selector(updateProg) userInfo:nil repeats:YES];
	}
}
- (IBAction)stop:(id)sender {
	// 停止播放音频
	[audioPlayer stop];
    //移除定时器
	[timer invalidate];
	timer = nil;
}
- (void) updateProg
{
	self.prog.progress = audioPlayer.currentTime / durationTime;
}
@end

 

你可能感兴趣的:(iOS开发之AVFoundation)