Objective c 播放音频iOS时绘制波形?
我想在播放音频时绘制波形 我查看了下面的链接 还检查了EZAudio和audiograph,这对我没有帮助 是否有类似的教程或示例项目。Objective c 播放音频iOS时绘制波形?,objective-c,ios7,avaudioplayer,waveform,Objective C,Ios7,Avaudioplayer,Waveform,我想在播放音频时绘制波形 我查看了下面的链接 还检查了EZAudio和audiograph,这对我没有帮助 是否有类似的教程或示例项目。-(void)startForFilePath:(NSString*)filePath - (void)startForFilePath:(NSString *)filePath { [self setNeedsDisplay]; _playTime = 0.0f; _audioFilePath = filePath; NSU
-(void)startForFilePath:(NSString*)filePath
- (void)startForFilePath:(NSString *)filePath
{
[self setNeedsDisplay];
_playTime = 0.0f;
_audioFilePath = filePath;
NSURL *url = [NSURL fileURLWithPath:_audioFilePath];
NSError *error = nil;
_player = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:&error];
if (_player == nil)
{
NSLog(@"player: %@ %ld %@", [error domain], (long)[error code], [[error userInfo] description]);
UIAlertView *alert =
[[UIAlertView alloc] initWithTitle: @""
message: [error localizedDescription]
delegate: nil
cancelButtonTitle:@"OK"
otherButtonTitles:nil];
[alert show];
return;
}
_player.numberOfLoops = 0;
[_player prepareToPlay];
_player.meteringEnabled = YES;
NSTimeInterval duration = _player.duration;
_waveUpdateFrequency = duration/(SOUND_METER_COUNT/2);
[_player play];
_timer = [NSTimer scheduledTimerWithTimeInterval:_waveUpdateFrequency target:self selector:@selector(updateMeters) userInfo:nil repeats:YES];
}
- (void)updateMeters
{
[_player updateMeters];
if ((![_player isPlaying]) || (_playTime >= 60.0f)) { //60s
[self stop];
return;
}
_playTime = _player.currentTime;
[self addSoundMeterItem:[_player averagePowerForChannel:0]];
}
#pragma mark - Sound meter operations
- (void)shiftSoundMeterLeft
{
for(int i = 0; i < SOUND_METER_COUNT - 1; i++) {
_soundMeters[i] = _soundMeters[i+1];
}
}
- (void)shiftSoundMeterRight
{
for(int i = SOUND_METER_COUNT - 1; i >= 0 ; i--) {
_soundMeters[i] = _soundMeters[i-1];
}
}
- (void)addSoundMeterItem:(int)lastValue
{
if (_isMeterRight) {
[self shiftSoundMeterRight];
[self shiftSoundMeterRight];
_soundMeters[0] = lastValue;
_soundMeters[1] = lastValue;
}
else {
[self shiftSoundMeterLeft];
[self shiftSoundMeterLeft];
_soundMeters[SOUND_METER_COUNT - 1] = lastValue;
_soundMeters[SOUND_METER_COUNT - 2] = lastValue;
}
[self setNeedsDisplay];
}
#pragma mark - Drawing operations
- (void)drawRect:(CGRect)rect
{
CGContextRef context = UIGraphicsGetCurrentContext();
// Draw sound meter wave
[_meterWaveColor set];
CGContextSetLineWidth(context, 1.0);
CGContextSetLineJoin(context, kCGLineJoinRound);
int baseLine = _frameRect.size.height/2;
int multiplier = 1;
int maxValueOfMeter = _frameRect.size.height/2 - 5;
for(CGFloat x = SOUND_METER_COUNT - 1; x >= 0; x--)
{
multiplier = ((int)x % 2) == 0 ? 1 : -1;
CGFloat y = baseLine + ((maxValueOfMeter * (MAX_LENGTH_OF_WAVE - abs(_soundMeters[(int)x]))) / MAX_LENGTH_OF_WAVE) * multiplier;
if(x == SOUND_METER_COUNT - 1) {
CGContextMoveToPoint(context, x * (_frameRect.size.width / SOUND_METER_COUNT), y);
//CGContextAddLineToPoint(context, x * (_frameRect.size.width / SOUND_METER_COUNT) + 1, y);
}
else {
CGContextAddLineToPoint(context, x * (_frameRect.size.width / SOUND_METER_COUNT), y);
//CGContextAddLineToPoint(context, x * (_frameRect.size.width / SOUND_METER_COUNT) + 1, y);
}
}
CGContextStrokePath(context);
}
{
[自我设置需要显示];
_播放时间=0.0f;
_audioFilePath=文件路径;
NSURL*url=[NSURL fileURLWithPath:_audioFilePath];
n错误*错误=nil;
_player=[[AVAudioPlayer alloc]initWithContentsOfURL:url错误:&error];
如果(_player==nil)
{
NSLog(@“player:%@%ld%@,[error domain],(long)[错误代码],[error userInfo]说明]);
UIAlertView*警报=
[[UIAlertView alloc]initWithTitle:@“”
消息:[错误本地化描述]
代表:无
取消按钮:@“确定”
其他按钮:无];
[警报显示];
返回;
}
_player.numberOfLoops=0;
[_playerpreparetoplay];
_player.meteringEnabled=是;
NSTimeInterval duration=\u player.duration;
_waveUpdateFrequency=持续时间/(声音、仪表、计数/2);
[玩家玩];
_计时器=[NSTimer scheduledTimerWithTimeInterval:\u waveUpdateFrequency目标:自选择器:@selector(updateMeters)用户信息:无重复:是];
}
-(void)updateMeters
{
[_播放器更新器];
如果(![_播放器显示])| |(_播放时间>=60.0f)){//60s
[自动停止];
返回;
}
_播放时间=_player.currentTime;
[self-addSoundMeterItem:[u播放器平均频道功率:0];
}
#布拉格马克-声级计操作
-(无效)shiftSoundMeterLeft
{
对于(int i=0;i<声音计计数-1;i++){
_声表[i]=_声表[i+1];
}
}
-(无效)移动SoundMeterRight
{
对于(int i=声音计计数-1;i>=0;i--){
_声音计[i]=_声音计[i-1];
}
}
-(void)addSoundMeterItem:(int)lastValue
{
如果(右){
[self-shiftSoundMeterRight];
[self-shiftSoundMeterRight];
_声音计[0]=最新值;
_声音计[1]=最新值;
}
否则{
[self-shiftSoundMeterLeft];
[self-shiftSoundMeterLeft];
_声音计[声音计计数-1]=最新值;
_声音计[声音计计数-2]=最新值;
}
[自我设置需要显示];
}
#pragma标记-绘图操作
-(void)drawRect:(CGRect)rect
{
CGContextRef context=UIGraphicsGetCurrentContext();
//绘制声波计波形
[_METERWAVECOLORSET];
CGContextSetLineWidth(上下文,1.0);
CGContextSetLineJoin(上下文,kCGLineJoinRound);
int基线=_frameRect.size.height/2;
整数乘数=1;
int maxValueOfMeter=_frameRect.size.height/2-5;
对于(CGFloat x=声音\仪表\计数-1;x>=0;x--)
{
乘数=((int)x%2)==0?1:-1;
CGFloat y=基线+((maxValueOfMeter*(声波的最大长度)abs((int)x]))/声波的最大长度)*乘数;
如果(x==声音\仪表\计数-1){
CGContextMoveToPoint(上下文,x*(\u frameRect.size.width/声音\u METER\u COUNT),y);
//CGContextAddLineToPoint(上下文,x*(\u frameRect.size.width/声音\u METER\u COUNT)+1,y);
}
否则{
CGContextAddLineToPoint(上下文,x*(\u frameRect.size.width/声音\u METER\u COUNT),y);
//CGContextAddLineToPoint(上下文,x*(\u frameRect.size.width/声音\u METER\u COUNT)+1,y);
}
}
CGContextStrokePath(上下文);
}
这可能与使用Avassetrader的问题无关。这个问题似乎与在播放资产时实时读取波形数据有关,而不是在不播放的情况下显示整个波形。我认为您错过了头文件。