音乐播放
#import "ViewController.h"
#import <AVFoundation/AVFoundation.h>
@interface ViewController ()<AVAudioPlayerDelegate>
{
AVAudioPlayer * avaudioplayer ; //播放器
UIProgressView * progressview; //播放进度
UISlider * volumslider; //声音控制
NSTimer * timer ; //监视音频播放进度
int num;
}
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
//初始化一个播放进度条
progressview = [[UIProgressView alloc]initWithFrame:CGRectMake(100, 100, 200, 30)];
[self.view addSubview:progressview];
//用NSTimer来控制音频的播放速度
timer = [NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(playProgress) userInfo:nil repeats:YES];
//初始化音量控制
volumslider = [[UISlider alloc]initWithFrame:CGRectMake(100, 180, 200, 30)];
//绑定方法
[volumslider addTarget:self action:@selector(volumeChange) forControlEvents:UIControlEventValueChanged];
//最大 最小音量 默认音量
volumslider.minimumValue = 0.0f;
volumslider.maximumValue = 10.0f;
volumslider.value = 5.0f;
[self.view addSubview:volumslider];
//初始化声音开关控件
UISwitch * OnSwith = [[UISwitch alloc]initWithFrame:CGRectMake(200, 230, 60, 40)];
[OnSwith addTarget:self action:@selector(OfforOn:) forControlEvents:UIControlEventValueChanged];
OnSwith.on = YES;
[self.view addSubview:OnSwith];
}
- (IBAction)btnForPlay:(id)sender {
//从budle路径下读取音频文件
NSString * path1 = [[NSBundle mainBundle]pathForResource:@"十年" ofType:@"mp3"];
NSURL * url1 = [NSURL fileURLWithPath:path1];
//初始化音频类
avaudioplayer = [[AVAudioPlayer alloc]initWithContentsOfURL:url1 error:nil];
avaudioplayer.delegate =self;
avaudioplayer.volume = 0.5;
//设置音乐播放次数,-1为循环
avaudioplayer.numberOfLoops = -1;
[avaudioplayer prepareToPlay];
[avaudioplayer play];
avaudioplayer.currentTime = progressview.progress * avaudioplayer.duration;
}
- (IBAction)btnForPause:(id)sender {
[avaudioplayer pause];
}
- (IBAction)btnForStop:(id)sender {
avaudioplayer.currentTime = 0;
[avaudioplayer stop];
}
- (IBAction)btnForPrevious:(id)sender {
}
- (IBAction)btnForNext:(id)sender {
}
-(void)playProgress
{
progressview.progress = avaudioplayer.currentTime / avaudioplayer.duration;
}
-(void)volumeChange
{
avaudioplayer.volume = volumslider.value;
}
-(void)OfforOn:(UISwitch *)sender
{
avaudioplayer.volume = sender.on;
}
-(void)audioPlayerDidFinishPlaying:(AVAudioPlayer *)player successfully:(BOOL)flag
{
[timer invalidate];
}
//上一首
- (IBAction)btnForPrevious:(id)sender {
if(mn>1)
{
mn--;
NSLog(@"%d",mn);
NSString * path4=[[NSBundle mainBundle]pathForResource:[NSString stringWithFormat:@"%d",mn] ofType:@"mp3"];
NSURL * url4=[NSURL fileURLWithPath:path4];
avAudioPlayer=[[AVAudioPlayer alloc]initWithContentsOfURL:url4 error:nil];
[avAudioPlayer play];
}
else
{
mn=3;
NSLog(@"%d",mn);
NSString * path5=[[NSBundle mainBundle]pathForResource:[NSString stringWithFormat:@"%d",mn] ofType:@"mp3"];
NSURL * url5=[NSURL fileURLWithPath:path5];
avAudioPlayer=[[AVAudioPlayer alloc]initWithContentsOfURL:url5 error:nil];
[avAudioPlayer play];
}
}
//下一首
- (IBAction)btnForNext:(id)sender {
if(mn<3)
{
mn++;
NSLog(@"%d",mn);
// NSString * path2=[[NSBundle mainBundle]pathForResource:@"%d" ofType:@"mp3"];
// NSURL * url3=[NSURL fileURLWithPath:path2];
NSString * path2=[[NSBundle mainBundle]pathForResource:[NSString stringWithFormat:@"%d",mn] ofType:@"mp3"];
NSURL * url3=[NSURL fileURLWithPath:path2];
avAudioPlayer=[[AVAudioPlayer alloc]initWithContentsOfURL:url3 error:nil];
[avAudioPlayer play];
}
else
{
mn=1;
NSLog(@"%d",mn);
NSString * path3=[[NSBundle mainBundle]pathForResource:[NSString stringWithFormat:@"%d",mn] ofType:@"mp3"];
NSURL * url4=[NSURL fileURLWithPath:path3];
avAudioPlayer=[[AVAudioPlayer alloc]initWithContentsOfURL:url4 error:nil];
[avAudioPlayer play];
}
}