使用 DTMF 生成音调并使用 AVAudioPlayer 播放
我想生成自定义 DTMF 音并在 iPhone 上播放。 为此,我创建并分配了一个带有自定义音调 (ptr) 的内存缓冲区。 现在我想创建一个 NSData 对象,使用内存缓冲区进行初始化,并使用 initWithData:error: 实例方法将其传递给 AVAudioPlayer。
我编写了以下代码,但是当我按下“播放”按钮时,它崩溃了。
#import "AudioPlayerViewController.h"
#include <stdlib.h>
#include <math.h>
#define SIZE 10
#define LENGTH 65535
const int PLAYBACKFREQ = 44100;
const float PI2 = 3.14159265359f * 2;
const int freq1 = 697;
const int freq2 = 1209;
@implementation AudioPlayerViewController
@synthesize playButton, stopButton;
- (void)viewDidLoad {
[super viewDidLoad];
// Allocate space for an array with ten elements of type int.
int *ptr = malloc(SIZE * sizeof(int));
if (ptr == NULL) NSLog(@"Error: Memory buffer could not be allocated.");
else NSLog(@"Allocation succeeded.");
// The formula for the tone, the content of the buffer.
for(int i=0; i<SIZE; i++) ptr[i] = (sin(i*(PI2*(PLAYBACKFREQ/freq1))) + sin(i* (PI2*(PLAYBACKFREQ/freq2)))) * 16383;
NSData *myData = [[NSData alloc] initWithBytesNoCopy:ptr length:SIZE];
free(ptr);
ptr = NULL;
audioPlayer = [[AVAudioPlayer alloc] initWithData:myData error:&error];
audioPlayer.numberOfLoops = -1;
}
-(IBAction) playAudio: (id) sender {
if (audioPlayer == nil) NSLog([error description]);
else [audioPlayer play];
}
-(IBAction) stopAudio: (id) sender { [audioPlayer stop]; }
- (void)dealloc {
[audioPlayer release];
[myData release];
[super dealloc];
}
@end
在文档中,方法 initWithBytesNoCopy 的描述如下:
包含新对象数据的缓冲区。 bytes 必须指向用 malloc 分配的内存块。
所以我已经这样做了,但它不起作用。
任何形式的帮助将不胜感激!
I want to generate a custom DTMF tone and play it on the iPhone.
In order to do so, I have created and allocated a memory buffer with a custom tone (ptr).
Now I want to create a NSData object, initialized with the memory buffer, and pass it to AVAudioPlayer using initWithData:error: instance method.
I wrote the following code, but when I press the button "Play", it crashes.
#import "AudioPlayerViewController.h"
#include <stdlib.h>
#include <math.h>
#define SIZE 10
#define LENGTH 65535
const int PLAYBACKFREQ = 44100;
const float PI2 = 3.14159265359f * 2;
const int freq1 = 697;
const int freq2 = 1209;
@implementation AudioPlayerViewController
@synthesize playButton, stopButton;
- (void)viewDidLoad {
[super viewDidLoad];
// Allocate space for an array with ten elements of type int.
int *ptr = malloc(SIZE * sizeof(int));
if (ptr == NULL) NSLog(@"Error: Memory buffer could not be allocated.");
else NSLog(@"Allocation succeeded.");
// The formula for the tone, the content of the buffer.
for(int i=0; i<SIZE; i++) ptr[i] = (sin(i*(PI2*(PLAYBACKFREQ/freq1))) + sin(i* (PI2*(PLAYBACKFREQ/freq2)))) * 16383;
NSData *myData = [[NSData alloc] initWithBytesNoCopy:ptr length:SIZE];
free(ptr);
ptr = NULL;
audioPlayer = [[AVAudioPlayer alloc] initWithData:myData error:&error];
audioPlayer.numberOfLoops = -1;
}
-(IBAction) playAudio: (id) sender {
if (audioPlayer == nil) NSLog([error description]);
else [audioPlayer play];
}
-(IBAction) stopAudio: (id) sender { [audioPlayer stop]; }
- (void)dealloc {
[audioPlayer release];
[myData release];
[super dealloc];
}
@end
In the documentation, the description of method initWithBytesNoCopy reads:
A buffer containing data for the new object. bytes must point to a memory block allocated with malloc.
So I have already done this, but it doesn't work.
Any kind of help will be appreciated!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您创建一个 NSData 而不复制数据,然后释放数据,因此 NSData 现在有一个悬空指针。删除
free(ptr);
行并重试。 NSData 在使用完数据后会自行释放数据。You create an NSData without copying the data and then you free the data, so NSData now has a dangling pointer. Remove the
free(ptr);
line and try it again. NSData will free the data by itself when it is done with it.