iOS development to achieve audio playback function

  • 2020-05-17 06:36:06
  • OfStack

Audio playback

1, the introduction

- function introduction

It is used to play long audio, instructions and music. AVFoundation is used

- framework introduction

* AVAudioPlayer

* initialization:

Note:

(3) the music playing object of global variable must be declared, or the music playing object of property can be played

(4) when exiting the playing page, 1 must empty the playing object and delegate at the same time

Import framework: #import < AVFoundation/AVFoundation.h >

Declare global variables


@interface ViewController ()<AVAudioPlayerDelegate>

{

  AVAudioPlayer *audioPlayer;

}

@end

Basic properties of audio

Preplay [audioPlayer prepareToPlay];

Gets the track audioPlayer.numberOfChannels of the current music

audioPlayer.currentTime // current playing time

audioPlayer. playing// to determine if it is playing

audioPlayer. numberOfLoops; // set the loop to play this time

audioPlayer.duration get the time to play the audio

audioPlayer.pan sets left and right channel effects

audioPlayer.volume setting volume 0.0- 1.0 is 1 percent

// the setting rate must be enableRate to YES; audioPlayer. enableRate = YES; audioPlayer. rate = 3.0;

Volume audioPlayer. volume = 0.1;

Set the number of playback times to be an infinite loop, 0 is 1, 1 is 2, and so on, audioPlayer.numberOfLoops = 0;

Get the current peak audioPlayer peakPowerForChannel:2

The average peak
audioPlayer averagePowerForChannel:2
Several proxy methods for audio playback


Called when play is complete
- (void)audioPlayerDidFinishPlaying:(AVAudioPlayer *)player successfully:(BOOL)flag{
NSLog(@" play finished ");
}
Called when there is an error in the decoding
- (void)audioPlayerDecodeErrorDidOccur:(AVAudioPlayer *)player error:(NSError * __nullable)error{

}
// start when interrupted
- (void)audioPlayerBeginInterruption:(AVAudioPlayer *)player{

}
Called after the interrupt ends
- (void)audioPlayerEndInterruption:(AVAudioPlayer *)player withOptions:(NSUInteger)flags{

}

Instance parsing audio playback

Initialize 1 button

In ViewDidLoad


UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
  button.frame = CGRectMake(100, 100, 100, 100);
  button.backgroundColor = [UIColor brownColor];
  [button setTitle:@"Play" forState:UIControlStateNormal];
  [button setTitle:@"Pause" forState:UIControlStateSelected];
  [button addTarget:self action:@selector(play:) forControlEvents:UIControlEventTouchUpInside];
  [self.view addSubview:button];

   [self playMusicWithName:@"TFBOYS- Handbook of youth training .mp3"];

Button trigger method:


- (void)play:(UIButton *)sender{
  sender.selected = !sender.selected;
  sender.selected != YES ? [audioPlayer pause]:[audioPlayer play];
}


- (void)playMusicWithName:(NSString *)name{
  NSError *error;
  
//   create 1 Music playback object 
  audioPlayer = [[AVAudioPlayer alloc]initWithContentsOfURL:[[NSBundle mainBundle]URLForResource:name withExtension:nil] error:&error];
  if (error) {
    NSLog(@"%@",error);
  }
 
   Play in advance 
  [audioPlayer prepareToPlay];
  
   play   I'm not going to write it here   But it's a necessary step 
  [audioPlayer play];
  
   To obtain   The track of current music 
  NSLog(@"%ld",audioPlayer.numberOfChannels);
  
  durtion : get the time to play the audio 
  
   Set the channel  -1.0 On the left  0.0 In the middle  1.0 right 
  audioPlayer.pan = 0.0;
  

   The volume 
  audioPlayer.volume = 0.1;
  
   Set the rate   You must set up enableRate for YES
  audioPlayer.enableRate = YES;
   Set the rate  0.5 is 1 Half the speed of the  1.0 ordinary  2.0 Double the rate 
  audioPlayer.rate = 1.0;
  
  currentTime  For the time 
   Get the peak   You must set up meteringEnabled for YES
  audioPlayer.meteringEnabled = YES;
   Update the peak 
  [audioPlayer updateMeters];  
   Get the current peak 
  NSLog(@" Current peak: %f",[audioPlayer peakPowerForChannel:2]);
  NSLog(@" The average peak %f",[audioPlayer averagePowerForChannel:2]);
  
   Set play times   Negative Numbers go on forever  0  is 1 time  1 Is two times   So on 
  audioPlayer.numberOfLoops = 0;
   Hang up the agent 
  audioPlayer.delegate = self;
  
}



Related articles: