iOS实现音频播放

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/xiaoxingaiwo/article/details/81611123

首先看下要实现的界面:
这里写图片描述

.h文件代码

#import <UIKit/UIKit.h>
#import <AVFoundation/AVFoundation.h>
@interface ViewController : UIViewController<AVAudioPlayerDelegate>
{
    //播放按钮
    UIButton *_btnPlay;
    //暂停按钮
    UIButton *_btnPause;
    //停止播放
    UIButton *_btnStop;
    //音乐播放进度条视图
    UIProgressView *_musicProgress;
    //声音大小调整滑动条
    UISlider *_volimnSlider;
    //静音开关
    UISwitch *_columnOnl;
    //音频播放对象
    AVAudioPlayer *_player;
    //定义计数器
    NSTimer *_timer;
}

.m文件代码:

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    _btnPlay=[UIButton buttonWithType:UIButtonTypeRoundedRect];
    _btnPlay.frame=CGRectMake(100, 100, 100, 40);
    [_btnPlay setTitle:@"开始" forState:UIControlStateNormal];
    [_btnPlay addTarget:self action:@selector(pressPlay) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:_btnPlay];

    _btnPause=[UIButton buttonWithType:UIButtonTypeRoundedRect];
    _btnPause.frame=CGRectMake(100, 160, 100, 40);
    [_btnPause setTitle:@"暂停" forState:UIControlStateNormal];
    [_btnPause addTarget:self action:@selector(pressPause) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:_btnPause];

    _btnStop=[UIButton buttonWithType:UIButtonTypeRoundedRect];
    _btnStop.frame=CGRectMake(100, 220, 100, 40);
    [_btnStop setTitle:@"停止" forState:UIControlStateNormal];
    [_btnStop addTarget:self action:@selector(pressStop) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:_btnStop];
    //设置音频进度条
    _musicProgress=[[UIProgressView alloc]initWithFrame:CGRectMake(10, 380, 300, 20)];
    _musicProgress.progress=0;
    [self.view addSubview:_musicProgress];

    _volimnSlider=[[UISlider alloc]init];
    _volimnSlider.frame=CGRectMake(10, 380, 300, 20);
    _volimnSlider.maximumValue=100;
    _volimnSlider.minimumValue=0;
    [_volimnSlider addTarget:self action:@selector(valueChange:) forControlEvents:UIControlEventValueChanged];
    [self.view addSubview:_volimnSlider];
    [self createAVPlayer];
}
-(void)createAVPlayer
{
    NSString *str=[[NSBundle mainBundle]pathForResource:@"Honor" ofType:@"mp3"];
    NSURL *url=[NSURL fileURLWithPath:str];
    _player=[[AVAudioPlayer alloc]initWithContentsOfURL:url error:nil];
    //解码工作
    [_player prepareToPlay];
    _player.numberOfLoops=1;
    _player.volume=0.2;

    _timer=[NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(updateT) userInfo:nil repeats:YES];

    _player.delegate=self;
}
-(void)audioPlayerDidFinishPlaying:(AVAudioPlayer *)player successfully:(BOOL)flag
{
    [_timer invalidate];
}
-(void)updateT
{
    _musicProgress.progress=_player.currentTime/_player.duration;


}
-(void)pressPlay
{
    [_player play];
    NSLog(@"播放");
}
-(void)pressPause
{
    NSLog(@"暂停");
    [_player pause];
}
-(void)pressStop
{
    NSLog(@"停止");
    [_player stop];
    _player.currentTime=0;//当前播放时间清零
}
-(void)valueChange:(UISlider*)slider
{
    NSLog(@"%f",slider.value);
    _player.volume=slider.value/100;
}
@end

猜你喜欢

转载自blog.csdn.net/xiaoxingaiwo/article/details/81611123