zoukankan      html  css  js  c++  java
  • 播放音频

    #import <UIKit/UIKit.h>
    #import <AVFoundation/AVFoundation.h>
    #import <AudioToolbox/AudioToolbox.h>
    
    @interface ViewController : UIViewController<AVAudioPlayerDelegate>
    
    @property (weak, nonatomic) IBOutlet UILabel *averageLabel;
    @property (weak, nonatomic) IBOutlet UILabel *peakLabel;
    @property (weak, nonatomic) IBOutlet UISlider *rateSlider;
    @property (weak, nonatomic) IBOutlet UISlider *panSlider;
    @property (weak, nonatomic) IBOutlet UISlider *volumeSlider;
    @property (strong, nonatomic) AVAudioPlayer *player;
    
    - (IBAction)updateRate:(id)sender;
    - (IBAction)updatePan:(id)sender;
    - (IBAction)updateVolume:(id)sender;
    - (IBAction)playVibrateSound:(id)sender;
    - (IBAction)startPlayer:(id)sender;
    - (IBAction)pausePlayer:(id)sender;
    
    @end
    #import "ViewController.h"
    
    @interface ViewController ()
    
    @end
    
    @implementation ViewController
    @synthesize averageLabel;
    @synthesize peakLabel;
    @synthesize rateSlider;
    @synthesize panSlider;
    @synthesize volumeSlider;
    
    - (void)viewDidLoad
    {
        [super viewDidLoad];
        // Do any additional setup after loading the view, typically from a nib.
        NSString *fileName = @"midnight-ride"; // Change this to your own file
        NSString *fileType = @"mp3";
        NSString *soundFilePath = [[NSBundle mainBundle] pathForResource:fileName ofType:fileType];
        NSURL *soundFileURL = [NSURL fileURLWithPath:soundFilePath];
        
        NSError *error;
        self.player = [[AVAudioPlayer alloc] initWithContentsOfURL:soundFileURL error:&error];
        if (error)
        {
            NSLog(@"Error creating the audio player: %@", error);
        }
        self.player.enableRate = YES; //Allows us to change the playback rate.
        self.player.meteringEnabled = YES; //Allows us to monitor levels
        self.player.delegate = self;
        self.volumeSlider.value = self.player.volume;
        self.rateSlider.value = self.player.rate;
        self.panSlider.value = self.player.pan;
        
        [self.player prepareToPlay]; //Preload audio to decrease lag
        
        [NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(updateLabels) userInfo:nil repeats:YES];
    }
    
    -(void)updateLabels
    {
        [self.player updateMeters];
        self.averageLabel.text = [NSString stringWithFormat:@"%f", [self.player averagePowerForChannel:0]];
        self.peakLabel.text = [NSString stringWithFormat:@"%f", [self.player peakPowerForChannel:0]];
    }
    
    - (IBAction)updateRate:(id)sender
    {
        self.player.rate = self.rateSlider.value;
    }
    
    - (IBAction)updatePan:(id)sender
    {
        self.player.pan = self.panSlider.value;
    }
    
    - (IBAction)updateVolume:(id)sender
    {
        self.player.volume = self.volumeSlider.value;
    }
    
    - (IBAction)playVibrateSound:(id)sender
    {
        AudioServicesPlaySystemSound(kSystemSoundID_Vibrate);
    }
    
    - (IBAction)startPlayer:(id)sender
    {
        [self.player play];
    }
    
    - (IBAction)pausePlayer:(id)sender
    {
        [self.player pause];
    }
    
    -(void)audioPlayerDecodeErrorDidOccur:(AVAudioPlayer *)player error:(NSError *)error
    {
        NSLog(@"Error playing file: %@", error);
    }
    
    - (void)audioPlayerEndInterruption:(AVAudioPlayer *)player withOptions:(NSUInteger)flags
    {
        if (flags == AVAudioSessionInterruptionOptionShouldResume)
        {
            [self.player play];
        }
    }
  • 相关阅读:
    设计模式之构造模式
    设计模式之创建模式
    用mongodb 固定集合实现只保留固定数量的记录,自动淘汰老旧数据
    多线程何如获取返回值
    基于redis的消息订阅与发布
    multiple类型的select option在django后台如何取值
    使用redis分布式锁解决并发线程资源共享问题
    数据库架构
    MongoDB数据库设计中6条重要的经验法则
    【mysql】开启binlog后异常:impossible to write to binary log since BINLOG_FORMAT = STATEMENT
  • 原文地址:https://www.cnblogs.com/fengmin/p/5526156.html
Copyright © 2011-2022 走看看