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 4087506
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T18:51:50+00:00 2026-05-20T18:51:50+00:00

I am creating an application based on the Speak Here example app . I

  • 0

I am creating an application based on the Speak Here example app. I want the audio to play through the headphones if they are plugged in or otherwise default to the speakers.

I’ve used the bottom bit of code to make this happen and it works fine unless the headphones are unplugged during playback. At that point the playback ends, which is okay. The problem is when I hit play again the playback comes out all weird and the stop button stops working. It also starts playback from where it left off rather then resetting from the beginning as it normally does when you hit the stop button.

Likewise if the headphones are plugged in before the app opens this also results in strange behaviour.

Maybe I need to grab the ‘headphones unplugged’ event and get it to ‘press the stop button’? Because the way it is doing it now is not correct.

To put my question in a simple form: How do you properly setup core audio on the iPhone for playback through the speakers and headphones.

Any pieces of code that could solve this problem would be of great help. Thanks!

 OSStatus error = AudioSessionInitialize(NULL, NULL, NULL, NULL);

if (error) printf("ERROR INITIALIZING AUDIO SESSION! %d\n", error);
else 
{
    UInt32 category = kAudioSessionCategory_PlayAndRecord;  
    // UInt32 category = kAudioSessionCategory_MediaPlayback;   

    error = AudioSessionSetProperty(kAudioSessionProperty_AudioCategory, sizeof(category), &category);
    if (error) printf("couldn't set audio category!");


    // It is bugs when I unplug the headphones! 
    UInt32 doChangeDefaultRoute = 1;        
    AudioSessionSetProperty (kAudioSessionProperty_OverrideCategoryDefaultToSpeaker, sizeof (doChangeDefaultRoute), &doChangeDefaultRoute);




    error = AudioSessionAddPropertyListener(kAudioSessionProperty_AudioRouteChange, propListener, self);
    if (error) printf("ERROR ADDING AUDIO SESSION PROP LISTENER! %d\n", error);
    UInt32 inputAvailable = 0;
    UInt32 size = sizeof(inputAvailable);

    // we do not want to allow recording if input is not available
    error = AudioSessionGetProperty(kAudioSessionProperty_AudioInputAvailable, &size, &inputAvailable);
    if (error) printf("ERROR GETTING INPUT AVAILABILITY! %d\n", error);
    btn_record.enabled = (inputAvailable) ? YES : NO;

    // we also need to listen to see if input availability changes
    error = AudioSessionAddPropertyListener(kAudioSessionProperty_AudioInputAvailable, propListener, self);
    if (error) printf("ERROR ADDING AUDIO SESSION PROP LISTENER! %d\n", error);

    error = AudioSessionSetActive(true); 
    if (error) printf("AudioSessionSetActive (true) failed");
  • 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-20T18:51:50+00:00Added an answer on May 20, 2026 at 6:51 pm

    After further investigation I realize now that I essentially asked two questions here. I’ve solved one so far.

    Sometimes when the playback is interrupted it is ‘paused’ rather than ‘stopped’. I just eliminated any elements of the code that did this ‘pausing’ and replaced it with calls to ‘stop’ the audio and wind back it’s queue.

    As for the audio coming out of the speakers instead of the headphones, I’m still looking into that one.

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

Sidebar

Related Questions

I'm creating a simple Cocoa non-document based application. I want to show visually that
I am creating an application based on the Utility template. The main screen consists
Im creating a C# Application based on WinForms / KryptonForms, and as the application
I've an application that is based on .NET 2 runtime. I want to add
I'm creating a web application installer using WiX. I want to allow the user
I am creating an internal web based application that will not be the target
I'm creating a web based application (i.e. JavaScript with jQuery and lots of SVG)
I’m creating a tab based application. One of the tabs is test, and another
I'm creating an application that'll display a random picture based upon a defined letter
I did a simple view based application. what i did is creating view additional

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.