iOS中的音频播放

//////////AVAudioPlayer////////////

#import <AVFoundation/AVFoundation.h>

#import "ViewController.h"


@interface ViewController ()

{

    AVAudioPlayer *audioPlayer;

}


@property (weak, nonatomic) IBOutlet UISlider *volumeSlider;

@property (weak, nonatomic) IBOutlet UISlider *currentSlider;

@property (weak, nonatomic) IBOutlet UILabel *timeLabel;


- (IBAction)volumeSlider:(UISlider *)sender;

- (IBAction)currentSlider:(UISlider *)sender;


@end


@implementation ViewController


- (void)viewDidLoad {

    [super viewDidLoad];

    //找到本地音频文件

    NSString *filePath = [[NSBundle mainBundle] pathForResource:@"天涯—张利鹏" ofType:@"mp3"];

    

    //包装成url,调用本地文件使用fileURLWithPath方法 远程文件调用使用URLWithString

    NSURL *url = [NSURL fileURLWithPath:filePath];

    

    audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:NULL];

    

    

    //准备播放 也可以不写

    [audioPlayer prepareToPlay];

    

    //遇到play就播放

//    if ([audioPlayer play]) {

//        NSLog(@"开始播放音乐了");

//    }

    //设置滑块的区间范围

    _volumeSlider.minimumValue = 0;

    _volumeSlider.maximumValue = 1;

    

    _currentSlider.minimumValue = 0;

    _currentSlider.maximumValue = audioPlayer.duration//总时长

    

    [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(timerAction:) userInfo:nil repeats:YES];

}


- (void)timerAction:(NSTimer *)timer {

    

    if (audioPlayer.playing) {

        NSString *currentTime = [NSString stringWithFormat:@"%.0f",audioPlayer.currentTime];

        _timeLabel.text = currentTime;

        

        _currentSlider.value = audioPlayer.currentTime;

    }

    

}


- (IBAction)currentSlider:(UISlider *)sender {

    //设置当前的播放进度时间

    audioPlayer.currentTime = sender.value;

}


- (IBAction)volumeSlider:(UISlider *)sender {

    

    //设置当前的播放进度时间

    audioPlayer.currentTime = sender.value;

}


- (IBAction)playAction:(UIButton *)sender {

    //播放器是否正在播放

    BOOL playing = audioPlayer.playing;

    if (playing) { //正在播放

        //暂停

        [audioPlayer pause];

        [sender setTitle:@"播放" forState:UIControlStateNormal];

    } else {

        [audioPlayer play];

        [sender setTitle:@"暂停" forState:UIControlStateNormal];

    }

}

@end



//////////AVPlayer//////////// 

修改- (void)viewDidLoad代码如下

- (void)viewDidLoad

{

    [super viewDidLoad];

    

    NSError *error;

    //设置音频会话支持后台播放

    [[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error:&error];

    

    //播放远程地址

    NSString *mp3Url = @"http://mp3a.9ku.com:1234/qzonemp3/chenchusheng001.mp3";

    NSURL *url = [NSURL URLWithString:mp3Url];

    

    player = [[AVPlayer alloc] initWithURL:url];

    [player play];//获取流媒体数据播放  

}

运行


//播放远程地址

NSString *mp3Url = @"http://music.baidutt.com/up/kwcawcca/ydsspc.mp3";

NSURL *url = [NSURL URLWithString:mp3Url];


player = [[AVPlayer alloc] initWithURL:url];

[player play];//获取流媒体数据播放


注释掉 在其下加入

//播放本地文件

NSString *filePath = [[NSBundle mainBundle] pathForResource:@"第一夫人" ofType:@"mp3"];

NSURL *url = [NSURL fileURLWithPath:filePath];

player = [[AVPlayer alloc] initWithURL:url];

[player play];


运行





////////// AudioToolbox.framework ////////////


//先导入AudioToolbox.framework框架

- (IBAction)playAction:(UIButton *)sender{

    NSString *filePath = [[NSBundle mainBundle] pathForResource:@"44th Street Medium" ofType:@"caf"];

    NSURL *url = [NSURL fileURLWithPath:filePath];

    

    //unsigned long soundId;

    SystemSoundID  soundId;

    

    //url地址注册系统声音

    AudioServicesCreateSystemSoundID((__bridge CFURLRef)url, &soundId);

    

    //播放系统声音

    AudioServicesPlaySystemSound(soundId);

      

   ***********播放震动  (模拟器无法模拟震动)*************

    AudioServicesPlaySystemSound(kSystemSoundID_Vibrate);


    

}







评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值