Record audio voice message on cocos2d-x

Hey .
I’m new on cocos2d-x . i need to record a voice message in my project , do we have this functionality on cocos2d-x ? kindly guide me

i want this function too, can anybody give some suggest ?

Phenmod Zhu wrote:

i want this function too, can anybody give some suggest ?

hi…
I deleted my reply ’coz I was not clear about your question… … u want to record your voice? and replay from the device? or do something else?

Phenmod Zhu wrote:

i want this function too, can anybody give some suggest ?

anyway,

for recording the voice… from the mic of the device,

I tried it before… and I remember I had to do the native coding and called the methods from the mm file… and that was the only way to do it back then… v1.0… (no idea if there’s a new way to do it, though)

mike han wrote:

Phenmod Zhu wrote:
> i want this function too, can anybody give some suggest ?
>
anyway,
>
for recording the voice… from the mic of the device,
>
I tried it before… and I remember I had to do the native coding and called the methods from the mm file… and that was the only way to do it back then… v1.0… (no idea if there’s a new way to do it, though)

yes, I want to record user’s voice for sound analysis, from the mic of the device.
I tried your said method today, but cann’t do it right. I’m a green hand to cocos2d-x, without any project experience on this.
Can you give a detailed description about how to do?

This is my way below: (ios)
the problem is: I cann’t import <AVFoundation/AVFoundation.h> and <Foundation/Foundation.h> in RecordInCC2dx.h
what’s the right way?

RecordInCC2dx.h
#import <Foundation/Foundation.h> // here, this import cause compiling error in “NSObjCRuntime.h”(Unkown type name NSString, etc) “NSZone.h” “NSObject.h”
#import <AVFoundation/AVFoundation.h> //and here, error too
#import <CoreAudio/CoreAudioTypes.h>

class RecordInCC2dx
{
public:
…….
AVAudioPlayer player;
AVAudioRecorder
recorder;
AVAudioSession audioSession;
NSString
destinationString;
void initRecord();
void checkMic();
void startRecord();
void stopRecord();
void playRecord();
}

RecordInCC2dx.mm
void RecordInCC2dx::initRecord(){
audioSession = [[AVAudioSession sharedInstance] retain];
[audioSession setCategory:AVAudioSessionCategoryPlayAndRecord error: nil];
[audioSession setActive:YES error:nil];

NSURL destinationURL = ;
NSDictionary
settings = [NSDictionary dictionaryWithObjectsAndKeys:
[NSNumber numberWithFloat: 44100.0], AVSampleRateKey,
[NSNumber numberWithInt: kAudioFormatAppleLossless], AVFormatIDKey,
[NSNumber numberWithInt: 1], AVNumberOfChannelsKey,
[NSNumber numberWithInt: AVAudioQualityMax], AVEncoderAudioQualityKey,
nil];

NSError **error;
recorder = initWithURL:destinationURL settings:settings error:&error];
recorder.delegate = nil; //Question: in a C++ class , how to set recorder’s delegate?
}
void RecordInCC2dx::checkMic{
if {
NSLog;
}
else {
NSLog;
}
}
void RecordInCC2dx::startRecord{
if {
;
recorder.meteringEnabled = YES;
;
}
}
void RecordInCC2dx::stopRecord{
;
}
void RecordInCC2dx::playRecord{
NSLog;
player = initWithContentsOfURL:
error:NULL];
;
}
i want to use these methods in HellowordScene:
**HellowordScene.cpp*
void HelloWorld::initRecordCallback(CCObject* pSender)
{
recordInCC2dx = new RecordInCC2dx();
recordInCC2dx~~>initRecord;
recordInCC2dx~~>checkMic();
}
void HelloWorld::startRecordCallback(CCObject* pSender){
recordInCC2dx->startRecord();
}
……

mike han wrote:

Phenmod Zhu wrote:
> i want this function too, can anybody give some suggest ?
>
hi…
I deleted my reply ’coz I was not clear about your question… … u want to record your voice? and replay from the device? or do something else?

do you have a demo for me, thank you very very much.

Phenmod Zhu wrote:

mike han wrote:
> Phenmod Zhu wrote:
> > i want this function too, can anybody give some suggest ?
>
> hi…
> I deleted my reply ’coz I was not clear about your question… … u want to record your voice? and replay from the device? or do something else?
>
do you have a demo for me, thank you very very much.

hi…

the following steps are what I did before…

  1. wrote Objective-C class (ex @interface Recorder) for recording audio files (ex: -(void)startRecording;) and wrap them up with a static method (ex: +(void)useMeRecorder;) that inits the class (in Objective-C Class Files… h&m… not in mm or cpp files)

  2. create .mm & .h file to create methods that calls the static method from Objective-C Record class (ex: void startRecording() { [[Recorder useMeRecorder] startRecording]; } )

  3. finally, from cocos2d-x cpp files, call methods from .mm & .h files… (ex : startRecording(); )

well,

why don’t you try your lines in RecordInCC2dx.h & mm files above… in Objective-C class files… then, there will be no errors with <AVFoundation/AVFoundation.h> and <Foundation/Foundation.h>
and try step 2 and 3…

i hope it make sense to you…

I’ll try to get the demo for you later… I can’t find my old codes no more…

You can download a demo on this website: http://www.supersuraccoon-cocos2d.com/2011/06/05/voice-record-demo/

After that you might be wondering on how to link that with your C++ code and/or have one interface that manages more than one platform. This is usually called a Bridge in OOP. I would suggest the easy way which you can find by looking at the CCFileUtils class from Cocos2d. I got Android and iOS working that way.

Inouk BR wrote:

You can download a demo on this website: http://www.supersuraccoon-cocos2d.com/2011/06/05/voice-record-demo/
>
After that you might be wondering on how to link that with your C++ code and/or have one interface that manages more than one platform. This is usually called a Bridge in OOP. I would suggest the easy way which you can find by looking at the CCFileUtils class from Cocos2d. I got Android and iOS working that way.

thanks.
this demo is on cocoa2d, not cocoa2d-x.
In fact, I did learn this demo before try my demo on cocoa2d-x, but get error… And I don’t have time to study it.
I will try again according to mike han.
thanks for your advice about CCFileUtils.