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];
        }
    }
  • 相关阅读:
    Win10 UWP Tile Generator
    Win10 BackgroundTask
    UWP Tiles
    UWP Ad
    Win10 build package error collections
    Win10 八步打通 Nuget 发布打包
    Win10 UI入门 pivot multiable DataTemplate
    Win10 UI入门 导航滑动条 求UWP工作
    UWP Control Toolkit Collections 求UWP工作
    Win10 UI入门 SliderRectangle
  • 原文地址:https://www.cnblogs.com/fengmin/p/5526156.html
Copyright © 2011-2022 走看看