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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T23:28:55+00:00 2026-06-02T23:28:55+00:00

I am trying to do something similar to what was requested in this question:

  • 0

I am trying to do something similar to what was requested in this question:
Playing audio with controls in iOS

I am using Storyboards with XCode 4.2. On my initial view page, I just have buttons that call other views, and no sounds are played there. These other views contain a UINavigationController with a BarButtonItem to allow you to go back. Other view pages will have buttons that allow one to play a sound. Each sound has its own play button, and there are resume, pause and stop buttons.

All sounds play fine, but if I hit the back button to the main page, the sound keeps playing. The desired behavior is for the sound to stop when navigating back to the main page via the back button.

Before storyboards, I could easily code a back button to stop the audio, but storyboards appear not that simple. It appears that I have to implement the method prepareForSegue. Okay, I can set the Segue identifier can be set in the Attributes Inspector, and referring back to the earlier post, I will use showPlayer.

But I was thinking I could code the prepareForSegue in one of my sound views, as seen in this cool example:
http://www.scott-sherwood.com/?p=219

-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{

if([[segue identifier] isEqualToString:@"showPlayer"]){
     SoundPageController *theAudio = (SoundPageController *)[segue ViewController];
     theAudio.delegate = self;
 }
 }

In the main page ViewController.m, I tried adding:

  @property (strong, nonatomic) AVAudioPlayer *theAudio;

And I tried adding something like:

  - (void)viewDidLoad {
[super viewDidLoad];

// this is the important part: if we already have something playing, stop it!
if (audioPlayer != nil)
{
    [audioPlayer stop];
}
// everything else should happen as normal.
audioPlayer = [[AVAudioPlayer alloc]
           initWithContentsOfURL:url
           error:&error];

[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error:nil];
[[AVAudioSession sharedInstance] setActive: YES error: nil];

I wasn’t able to get this to work, so I reverted the changes back to original. I will put my simplified code below, and if you have suggestions how to do it right, please let me know!

ViewController.h:

@interface ViewController : UIViewController {      
}

ViewController.m (pretty much default):

    #import "ViewController.h"

#import "AppDelegate.h"

@implementation ViewController

- (void)viewDidLoad
{

[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}

- (void)viewDidUnload
{

[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;

}

SoundPage1.h:
#import

#import <AVFoundation/AVAudioPlayer.h>

@interface occupy : UIViewController <AVAudioPlayerDelegate> {
AVAudioPlayer* theAudio;
}

//-(IBAction)PressBack:(id)sender; now the back button is linked up through storyboard
-(IBAction)pushButton1;
-(IBAction)pushButton2;
-(IBAction)play;
-(IBAction)stop;
-(IBAction)pause;
@end

SoundPage1.m

#import "SoundPage1.h"
#import "AppDelegate.h"
#import "ViewController.h"

@implementation SoundPage1

-(IBAction)pushButton {

NSString *path = [[NSBundle mainBundle] pathForResource:@"sound1" ofType:@"mp3"];
if(theAudio)[theAudio release];
theAudio = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path]    error:NULL];
theAudio.delegate = self;
theAudio.volume = 1.0;
[theAudio play];
 }

-(IBAction)pushButton2 {
NSString *path = [[NSBundle mainBundle] pathForResource:@"sound2" ofType:@"mp3"];
if(theAudio)[theAudio release];
theAudio = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL];
theAudio.delegate = self;
theAudio.volume = 1.0;
[theAudio play];
}

// Old code from XCode 3.x when creating a back button that could stop the audio was easy
//-(IBAction)PressBack:(id)sender{
//  [theAudio stop];
//  ViewController* myappname = [[ViewController alloc] initWithNibName:@"ViewController" bundle:nil];  
//  [self.navigationController pushViewController:myappname animated:NO];
//  }


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

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

Add in the code for play, stop and pause for the buttons.

I tried to format this, sorry in advance if it is hard to read.

Thanks for any suggestions!
Rob

  • 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-06-02T23:28:56+00:00Added an answer on June 2, 2026 at 11:28 pm

    You should stop the AVAudioPlayer in viewWillDisappear.

    - (void)viewWillDisappear:(BOOL)animated { 
        [super viewWillDisappear:animated]; 
        if(theAudio && theAudio.isPlaying) {
            [theAudio stop]; 
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This is what I'm trying to do something similar to the following request, but
I'm trying to write something similar to this (sorry if the sample is not
I am trying to do something very similar to this c# docs example :
I am trying to do something similar to what the user who asked this
I am trying to do something similar to this sample code from Phil Haack
Similar to this question , I am trying to perform simple authentication to a
Im trying to make something similar to twitpic.com's email submission feature. Their address schema
I'm trying to implement something similar to what http://www.ancestry.com has, but I'm not sure
I'm trying to build something similar to Facebook's privacy selection menu, except without the
Well,I am trying to do something similar what I did in Adobe Flex.Consuming a

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.