iOS 多媒体(1)——音频的播放

使用AVAudioPlayer播放 引用AVFoundation类库

MainContoller.h代码:

#import <UIKit/UIKit.h>
#import <AVFoundation/AVFoundation.h>
@interface MainController : UIViewController<AVAudioPlayerDelegate>
{
    AVAudioPlayer *audioPaly;
}
@property (retain, nonatomic) IBOutlet UISlider *volumeSlider;

@property (retain, nonatomic) IBOutlet UILabel *timeLabel;
@property (retain, nonatomic) IBOutlet UISlider *currentTimeSlider;
- (IBAction)volumerAction:(id)sender;
- (IBAction)currentChange:(id)sender;
@property (retain, nonatomic) IBOutlet UIButton *btnPaly;
- (IBAction)btnPlay:(id)sender;

@end


MainContoller.h代码:

#import "MainController.h"

@interface MainController ()

@end

@implementation MainController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    NSString *path=[[NSBundle mainBundle]pathForResource:@"感谢" ofType:@"MP3"];
    NSURL *url=[NSURL fileURLWithPath:path];
    audioPaly=[[AVAudioPlayer alloc]initWithContentsOfURL:url error:nil];
    
    //设置delegate
    audioPaly.delegate=self;
    //准备播放
    [audioPaly prepareToPlay];
    
    //设置音量的最大最小值
    self.volumeSlider.minimumValue=0;
    self.volumeSlider.maximumValue=1;
    
    double allTime=audioPaly.duration;//总时长
    //设置进度的最值
    self.currentTimeSlider.minimumValue=0;
    self.currentTimeSlider.maximumValue=allTime;//总时长
    
    if (self.currentTimeSlider.value <allTime) {
         [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(timerAction:) userInfo:nil repeats:YES]; 
    }
  
    

    if ([audioPaly play]) {
        NSLog(@"开始播放");
    }
    
    if (audioPaly.playing) {
        [self.btnPaly  setTitle:@"暂停" forState:UIControlStateNormal];
    }
}

-(void)timerAction:(NSTimer *)timer
{
    double currentTime=audioPaly.currentTime;
    //可以设置为分钟的形式
    self.timeLabel.text=[[NSString alloc]initWithFormat:@"%.2f/%.2f",currentTime,audioPaly.duration];
    self.currentTimeSlider.value=audioPaly.currentTime;
    
    
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)dealloc {
    [_volumeSlider release];
   
    [_timeLabel release];
    [_currentTimeSlider release];
    [_btnPaly release];
    [super dealloc];
}
- (IBAction)volumerAction:(id)sender {
    
    audioPaly.volume=self.volumeSlider.value;
    
}

- (IBAction)currentChange:(id)sender {
    audioPaly.currentTime=self.currentTimeSlider.value;
}
- (IBAction)btnPlay:(id)sender {
    if (audioPaly.playing) {
        [audioPaly pause];
        [self.btnPaly  setTitle:@"播放" forState:UIControlStateNormal];
       
    }
    else
    {
        [audioPaly play];
        [self.btnPaly  setTitle:@"暂停" forState:UIControlStateNormal];
    }
}
#pragma mark----AvAudioPlayDlegate
/* audioPlayerDidFinishPlaying:successfully: is called when a sound has finished playing. This method is NOT called if the player is stopped due to an interruption. */
- (void)audioPlayerDidFinishPlaying:(AVAudioPlayer *)player successfully:(BOOL)flag
{
    NSLog(@"播放结束");
}
@end

.xib文件布局



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值