How can I implement a volume meter for a song currently playing? (iPhone OS 3.1.3)

倾然丶 夕夏残阳落幕 提交于 2019-12-13 02:34:35

问题


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



来源:https://stackoverflow.com/questions/2926225/how-can-i-implement-a-volume-meter-for-a-song-currently-playing-iphone-os-3-1

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!