Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6902441
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T07:47:54+00:00 2026-05-27T07:47:54+00:00

I’ve a bit of code which was working fine with iOS 4.3. I had

  • 0

I’ve a bit of code which was working fine with iOS 4.3. I had a look on the Internet, I found others having the same problem without answer which worked for me. I think that I can record something but I cannot play it. Here is my code:

DetailViewController.h

#import <UIKit/UIKit.h>
#import <AVFoundation/AVFoundation.h>
#import <CoreAudio/CoreAudioTypes.h>
#import <AudioToolbox/AudioServices.h>

@interface DetailViewController : UIViewController <UISplitViewControllerDelegate, AVAudioRecorderDelegate> {

id detailItem;
UILabel *detailDescriptionLabel;

IBOutlet UIButton *btnStart;
IBOutlet UIButton *btnPlay;

//Variables setup for access in the class:
NSURL * recordedTmpFile;
AVAudioRecorder * recorder;
BOOL toggle;
}

// Needed properties
@property (nonatomic, retain) IBOutlet UIButton *btnStart;
@property (nonatomic, retain) IBOutlet UIButton *btnPlay;
@property (strong, nonatomic) id detailItem;
@property (strong, nonatomic) IBOutlet UILabel *detailDescriptionLabel;

-(IBAction) start_button_pressed;
-(IBAction) play_button_pressed;
@end

DetailViewController.m

- (void)viewDidLoad {
[super viewDidLoad];
toggle = YES;
btnPlay.hidden = YES;
NSError *error;

// Create the Audio Session
AVAudioSession *audioSession = [AVAudioSession sharedInstance];

// Set up the type of session
[audioSession setCategory:AVAudioSessionCategoryPlayAndRecord error:&error];

// Activate the session.
[audioSession setActive:YES error:&error];

[self configureView];    
}

-(IBAction) start_button_pressed{
if (toggle) {
    toggle = NO;
    [btnStart setTitle:@"Press to stop recording" forState:UIControlStateNormal];
    btnPlay.enabled = toggle;
    btnPlay.hidden = !toggle;
            NSError *error;

    NSMutableDictionary *recordSettings = [[NSMutableDictionary alloc] init];

    [recordSettings setValue:[NSNumber numberWithInt:kAudioFormatAppleIMA4] forKey:AVFormatIDKey];

    [recordSettings setValue:[NSNumber numberWithFloat:44100.0] forKey:AVSampleRateKey];

    [recordSettings setValue:[NSNumber numberWithInt:2] forKey:AVNumberOfChannelsKey];

    // Create a temporary files to save the recording. 
    recordedTmpFile = [NSURL fileURLWithPath:[NSTemporaryDirectory() stringByAppendingPathComponent:[NSString stringWithFormat: @"%.0f.%@", [NSDate timeIntervalSinceReferenceDate] * 1000.0, @"caf"]]];

    NSLog(@"The temporary file used is: %@", recordedTmpFile);

    recorder = [[AVAudioRecorder alloc] initWithURL:recordedTmpFile settings:recordSettings error:&error];

    [recorder setDelegate:self];

    [recorder prepareToRecord];
    [recorder record];
}
else {
    toggle = YES;
    [btnStart setTitle:@"Start recording" forState:UIControlStateNormal];
    btnPlay.hidden = !toggle;
    btnPlay.enabled = toggle;

    NSLog(@"Recording stopped and saved in file: %@", recordedTmpFile);
    [recorder stop];
}
}

-(IBAction) play_button_pressed{

NSError *error;
AVAudioPlayer * avPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:recordedTmpFile error:&error];

   if (!error)
   {
       [avPlayer prepareToPlay];
       [avPlayer play];

       NSLog(@"File is playing");
   }

}

- (void) audioPlayerDidFinishPlaying: (AVAudioPlayer *) player
                    successfully: (BOOL) flag {
     NSLog (@"audioPlayerDidFinishPlaying:successfully:");
}

- (void)audioRecorderDidFinishRecording:(AVAudioRecorder *) aRecorder successfully: (BOOL)flag
{
    NSLog (@"audioRecorderDidFinishRecording:successfully:");
}

Here is the of my program running:

2011-11-25 11:58:02.005 Bluetooth1[897:707] The temporary file used is: file://localhost/private/var/mobile/Applications/D81023F8-C53D-4AC4-B1F7-14D66EB4844A/tmp/343915082005.caf
2011-11-25 11:58:05.956 Bluetooth1[897:707] Recording stopped and saved in file: file://localhost/private/var/mobile/Applications/D81023F8-C53D-4AC4-B1F7-14D66EB4844A/tmp/343915082005.caf
2011-11-25 11:58:05.998 Bluetooth1[897:707] audioRecorderDidFinishRecording:successfully:
2011-11-25 11:58:11.785 Bluetooth1[897:707] File is playing

For some reason, the function audioPlayerDidFinishPlaying is never called. However it seems that something has been recorded. Right now I do not know which part is not working but I guess this has something to do with AVAudioPlayer.

[EDIT] It’s getting weirder and weirder. I wanted to make sure that something was recorded so I look for taking the duration of the record. Here is the new play function:

-(IBAction) play_button_pressed{

    NSError *error;
AVAudioPlayer * avPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL: recordedTmpFile error:&error];

    if (!error)
    {
        AVURLAsset* audioAsset = [AVURLAsset URLAssetWithURL:recordedTmpFile options:nil];
        CMTime audioDuration = audioAsset.duration;
        float audioDurationSeconds = CMTimeGetSeconds(audioDuration);

        [avPlayer prepareToPlay];
        [avPlayer play];

        NSString *something = [NSString stringWithFormat:@"%f",audioDurationSeconds]; 
        NSLog(@"File is playing: %@", something);
    }
    else
    {
         NSLog(@"Error playing.");
    }   
}

Now, the length of the record is recorded and it make sense (if I record for 10s it shows something around 10s). However, when I put these lines of code for the first time I forgot to do the conversion float to NSString. So it crashed… and the app play the sound… After different tests I can conclude that my app can record and play a sound but is as to crash to play the recorded sound. I’ve no idea what can be the problem. I found that AVPlayer is asynchronous, is their something to do with that? I’m completely lost…

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T07:47:55+00:00Added an answer on May 27, 2026 at 7:47 am

    OK, that is not really cool to answer you own questions. Moreover when the answer is not clean but it is working… In order to play what I have recorded I have used the following block of code:

        AVURLAsset* audioAsset = [AVURLAsset URLAssetWithURL:recordedTmpFile options:nil];
        CMTime audioDuration = audioAsset.duration;
        float audioDurationSeconds = CMTimeGetSeconds(audioDuration);
    
        [avPlayer prepareToPlay];
        [avPlayer play];
    
        // Block for audioDurationSeconds seconds
        [NSThread sleepForTimeInterval:audioDurationSeconds];
    

    I am calculating the length of the recorded file and I am waiting for this amount of time… it is dirty but it is doing the trick. Plus, if it launched in another thread it will not block the application.

    I anyone has something I would gladly take it!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am currently running into a problem where an element is coming back from
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
We're building an app, our first using Rails 3, and we're having to build
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I have a text area in my form which accepts all possible characters from
I'm having trouble keeping the paragraph square between the quote marks. In firefox the
I have this code to decode numeric html entities to the UTF8 equivalent character.
I'm working with an upstream system that sometimes sends me text destined for HTML/XML

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.