How can I implement a volume meter for a song curr

2019-07-29 04:27发布

问题:

I'm very new to core audio and I just would like some help in coding up a little volume meter for whatever's being outputted through headphones or built-in speaker, like a dB meter. I have the following code, and have been trying to go through the apple source project "SpeakHere", but it's a nightmare trying to go through all that, without knowing how it works first... Could anyone shed some light?

Here's the code I have so far...

(void)displayWaveForm 
{
 while (musicIsPlaying == YES {
  NSLog(@"%f",sizeof(AudioQueueLevelMeterState));
 }
}

(IBAction)playMusic 
{
 if (musicIsPlaying == NO) {
  NSURL *url = [NSURL fileURLWithPath:[NSString stringWithFormat:@"%@/track7.wav",[[NSBundle mainBundle] resourcePath]]];

  NSError *error;

  music = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:&error];
  music.numberOfLoops = -1;

  music.volume = 0.5;
  [music play];
  musicIsPlaying = YES;
  [self displayWaveForm];
 }

 else {
  [music pause];
  musicIsPlaying = NO;
 }

}

回答1:

you can use metering with the AVAudioPlayer class, first enable it then get the average power to use as your meter data avTouch has a working example