zoukankan      html  css  js  c++  java
  • OC-AVAudioPlayer的使用小记

    - (void)viewDidLoad {
    
        [super viewDidLoad];
    
        
    
        //设置音乐的后台播放,注意background mode中需要勾选上
    
        AVAudioSession *session = [AVAudioSession sharedInstance];
    
        [session setCategory:AVAudioSessionCategoryPlayback error:nil];
    
    }
    
     
    //开始播放
    - (IBAction)player:(id)sender {
    
        [self.player play];
    
    }
    
     
    //暂停播放
    - (IBAction)pause:(id)sender {
    
        [self.player pause];
    
    }
    
     
    //停止播放
    - (IBAction)stop:(id)sender {
    
        [self.player stop];
    
    }
    
     
    //前进5s
    - (IBAction)qianJin5s:(id)sender {
    
        self.player.currentTime += 5;
    
    }
    
     
    //后退5s
    - (IBAction)houTui5s:(id)sender {
    
        self.player.currentTime -= 5;
    
    }
    
     
    //快速播放
    - (IBAction)faster:(id)sender {
    
        self.player.rate = 2;
    
    }
    
     
    //播放一次
    - (IBAction)playOnce:(id)sender {
    
        self.player.numberOfLoops = 0;
    
    }
    
     
    //播放3次
    - (IBAction)playThirst:(id)sender {
    
        self.player.numberOfLoops = 2;
    
    }
    
     
    //循环播放
    - (IBAction)playAllTheTime:(id)sender {
    
        self.player.numberOfLoops = -1;
    
    }
    
     
    //获取一些基础信息
    - (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    
        NSLog(@"deviceCurrentTime=%f",self.player.deviceCurrentTime);
    
        NSLog(@"currentTime=%f",self.player.currentTime);
    
        NSLog(@"duration=%f", self.player.duration);
    
        NSLog(@"settings=%@", self.player.settings);
    
        
    
    }


    //听筒播放

    
    

    - (IBAction)tingTongPlay:(id)sender {

    
    
    
    
    
    

        [[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayAndRecord withOptions:0 error:nil];

    
    
    
    
    
    

    }

    
    
    
    
    
    
    
    
    
    
    
    
    

    //扬声器播放

    
    
    
    
    
    

    - (IBAction)outSpeakerPlayer:(id)sender {

        [[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayAndRecord withOptions:AVAudioSessionCategoryOptionDefaultToSpeaker error:nil];
    
    
    
    
    
    

    }

    //懒加载 

    - (AVAudioPlayer *)player {

    
    

        if (!_player) {

    
    

            //获取播放的路径 paomo.mp3   2018-11-27 10_36_51 1.wav

    
    

            NSURL *path = [[NSBundle mainBundle] URLForResource:@"paomo.mp3" withExtension:nil];

    
    

            //根据路径创建播放对象

    
    

            AVAudioPlayer *player = [[AVAudioPlayer alloc] initWithContentsOfURL:path error:nil];

    
    
    
    
    

            //如果这个属性不设置的话,那么不能设置倍速播放的功能

    
    

            player.enableRate = YES;

    
    

            

    
    

            //准备播放

    
    

            [player prepareToPlay];

    
    

            _player = player;

    
    

        }

    
    

        return _player;

    
    

    }



     

    AVAudioPlayer 这个框架,去看看苹果开发文档,还比较容易理解,此处只是贴上代码作为一个记录而已。

  • 相关阅读:
    iOS YSKit系列
    Xcode $(SRCROOT)和$(PROJECT_DIR)区别
    gcc -ldl 选项作用
    rpm 命令
    ls 命令详解
    Linux下用户组、文件权限详解
    服务不支持chkconfig的解决
    Linux 安装 MongoDB数据库
    CentOS 7 之前好好的,突然一天启动时黑屏,没有登陆界面了(配置 network-scripts 连网)
    vi 命令集
  • 原文地址:https://www.cnblogs.com/lyz0925/p/11772539.html
Copyright © 2011-2022 走看看