맞춤 DTMF 톤을 생성하고 iPhone에서 재생하고 싶습니다. 그렇게하기 위해서, 커스텀 톤 (ptr)을 가지는 메모리 버퍼를 작성해 할당했습니다. 이제 NSData 객체를 만들고 메모리 버퍼로 초기화하고 initWithData : error : instance 메서드를 사용하여 AVAudioPlayer에 전달하려고합니다.DTMF 톤 생성이 작동하지 않음 - 응용 프로그램이 충돌 함!
다음 코드를 작성했지만 응용 프로그램을 실행할 때 충돌이 발생합니다. 문서에
#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 방법의 설명을 읽
".. 새로운 개체에 대한 데이터를 포함하는 버퍼 바이트의 malloc으로 할당 된 메모리 블록을 가리켜 야"
그래서 이미 해봤지만 작동하지 않습니다.
모든 종류의 도움을 주실 수 있습니다! 사전에
감사합니다,
Sagiftw
네, 맞아. SIZE로 변경했습니다. 이제 응용 프로그램이로드되지만 "재생"버튼을 누르면 충돌합니다. – Sagiftw