tags:

views:

104

answers:

0

Hi Guys I Got a prb in AvAudioRecorder that the sound was not recording and it was no playing and my code is in .m: #import "AudioRecorderViewController.h"

@implementation AudioRecorderViewController


 // The designated initializer.  Override if you create the controller programmatically and want to perform customization that is not appropriate for viewDidLoad.
- (id)init {
    if (self = [super init]) {
        // Custom initialization

    mview = [[UIView alloc]initWithFrame:[[UIScreen mainScreen]applicationFrame]];
    startRecording =[[UIButton alloc]initWithFrame:CGRectMake(100, 100, 140, 30)];
    startRecording.backgroundColor=[UIColor redColor];
    [startRecording  setTitle:@"Start Recording" forState:UIControlStateNormal];
    //[playButton setBackgroundImage:@"images-1.png" forState:UIControlStateNormal];
    //  myButton.backgroundImage = [UIImage imageNamed:@"anyButtonBackground.png"]; 
    [startRecording addTarget:self action:@selector(startRecordingAction) forControlEvents:UIControlEventTouchUpInside];
    [mview addSubview:startRecording];

    playRecordedItem =[[UIButton alloc]initWithFrame:CGRectMake(100, 200, 140, 30)];
    playRecordedItem.backgroundColor=[UIColor redColor];
    [playRecordedItem  setTitle:@"Play" forState:UIControlStateNormal];
    //[playButton setBackgroundImage:@"images-1.png" forState:UIControlStateNormal];
    //  myButton.backgroundImage = [UIImage imageNamed:@"anyButtonBackground.png"]; 
    [playRecordedItem addTarget:self action:@selector(playRecordedItemAction) forControlEvents:UIControlEventTouchUpInside];
    [mview addSubview:playRecordedItem];
    mview.backgroundColor = [UIColor blackColor];

    toggle =  YES;
    playRecordedItem.hidden = YES;

    AVAudioSession *audioSession = [AVAudioSession sharedInstance];
    [audioSession setCategory:AVAudioSessionCategoryPlayAndRecord error:&error];
    [audioSession setActive:YES error:&error];
    self.view = mview;
}
return self;
}


/*
// Implement loadView to create a view hierarchy programmatically, without using a nib.
- (void)loadView {
}

*/

// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
[super viewDidLoad];

toggle =  YES;
playRecordedItem.hidden = YES;

AVAudioSession *audioSession = [AVAudioSession sharedInstance];
[audioSession setCategory:AVAudioSessionCategoryPlayAndRecord error:&error];
[audioSession setActive:YES error:&error];
}

-(void)startRecordingAction
{

printf("\n Inside Record Action.......");

if(toggle)
{
    toggle = NO;
    //[actSpinner startAnimating];
    [startRecording setTitle:@"Stop Recording" forState: UIControlStateNormal ];    
    playRecordedItem.enabled = toggle;
    playRecordedItem.hidden = !toggle;
//Begin the recording session.
//Error handling removed. Please add to your own code.

//Setup the dictionary object with all the recording settings that this
//Recording sessoin will use
//Its not clear to me which of these are required and which are the bare minimum.

NSMutableDictionary* recordSetting = [[NSMutableDictionary alloc] init];
[recordSetting setValue :[NSNumber numberWithInt:kAudioFormatAppleIMA4] forKey:AVFormatIDKey];
[recordSetting setValue:[NSNumber numberWithFloat:44100.0] forKey:AVSampleRateKey];
[recordSetting setValue:[NSNumber numberWithInt: 2] forKey:AVNumberOfChannelsKey];

//Now that we have our settings we are going to instanciate an instance of our recorder instance.
//Generate a temp file for use by the recording.

recordedTmpFile = [NSURL fileURLWithPath:[NSTemporaryDirectory() stringByAppendingPathComponent: [NSString stringWithFormat: @"%.0f.%@", [NSDate timeIntervalSinceReferenceDate] * 1000.0, @"caf"]]];
NSLog(@"Using File called: %@",recordedTmpFile);
//Setup the recorder to use this file and record to it.
recorder = [[ AVAudioRecorder alloc] initWithURL:recordedTmpFile settings:recordSetting error:&error];
//Use the recorder to start the recording.
[recorder setDelegate:self];
//We call this to start the recording process and initialize
//the subsstems so that when we actually say "record" it starts right away.
[recorder prepareToRecord];
//Start the actual Recording
[recorder record];
}
else
{
    toggle = YES;
    //[actSpinner stopAnimating];
    [startRecording setTitle:@"Start Recording" forState:UIControlStateNormal ];
    playRecordedItem.enabled = toggle;
    playRecordedItem.hidden = !toggle;

    NSLog(@"Using File called: %@",recordedTmpFile);
    //Stop the recorder.
    [recorder stop];
}
}

-(void)playRecordedItemAction
{
        printf("\n Inside play Action........");
        //Setup the AVAudioPlayer to play the file that we just recorded.
        AVAudioPlayer * avPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:recordedTmpFile error:&error];
        [avPlayer prepareToPlay];
        [avPlayer play];
    }
/*

// Override to allow orientations other than the default portrait orientation. - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation { // Return YES for supported orientations return (interfaceOrientation == UIInterfaceOrientationPortrait); } */

- (void)didReceiveMemoryWarning {
// Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];

// Release any cached data, images, etc that aren't in use.
}

- (void)viewDidUnload {
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;

NSFileManager * fm = [NSFileManager defaultManager];
[fm removeItemAtPath:[recordedTmpFile path] error:&error];
//Call the dealloc on the remaining objects.
[recorder dealloc];
recorder = nil;
recordedTmpFile = nil;
}


- (void)dealloc {
    [super dealloc];
}


@end