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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T04:19:46+00:00 2026-06-13T04:19:46+00:00

Here is my code AppDelegate.m I use this code to start AVAudioPlayer. It works

  • 0

Here is my code

AppDelegate.m
I use this code to start AVAudioPlayer. It works fine.

#import "AppDelegate.h"

@implementation AppDelegate



- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{


    NSString* resourcePath = [[NSBundle mainBundle] resourcePath];
    resourcePath = [resourcePath stringByAppendingString:@"/Helloween.mp3"];
    NSLog(@"Path to play: %@", resourcePath);
    NSError* err;

    //Initialize our player pointing to the path to our resource
    backgroundMusic = [[AVAudioPlayer alloc] initWithContentsOfURL:
              [NSURL fileURLWithPath:resourcePath] error:&err];

    if( err ){
        //bail!
        NSLog(@"Failed with reason: %@", [err localizedDescription]);
    }
    else{
        //set our delegate and begin playback
        backgroundMusic.delegate = self;
        [backgroundMusic play];
        backgroundMusic.numberOfLoops = -1;
        backgroundMusic.currentTime = 0;
       backgroundMusic.volume = 1.0;
    }
    // Override point for customization after application launch.
    return YES;
}

In AppDelegate.h I add framework and AVAudioPlayer

#import <UIKit/UIKit.h>
#import <AVFoundation/AVFoundation.h>
@interface AppDelegate : UIResponder <UIApplicationDelegate> {
    AVAudioPlayer *backgroundMusic;
}

@property(nonatomic,retain) AVAudioPlayer *backgroundMusic;
@property (strong, nonatomic) UIWindow *window;

@end

In ViewController.m i also add framework AVFoundation and import AppDelegate.h

#import "ViewController.h"
#import <QuartzCore/QuartzCore.h>
#import <CoreData/CoreData.h>
#define fName @"data.plist"
#define kNames @"data.plist"
#import <AVFoundation/AVFoundation.h>
#import "AppDelegate.h"


@interface ViewController ()

@end
@implementation ViewController

@synthesize backgroundMusic;
-(IBAction)stopMusic:(id)sender {
   AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
      [appDelegate.backgroundMusic stop];

}

- (void)viewDidLoad
{
      [super viewDidLoad];

    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];

    //set the position of the button
    button.frame = CGRectMake(100, 100, 20, 20);
    button.backgroundColor = [UIColor whiteColor];
    [button addTarget:self action:@selector(stopMusic:) forControlEvents:UIControlEventTouchUpInside];

    //add the button to the view
    [self.view addSubview:button];

ViewController.h

#import <UIKit/UIKit.h>
#import <QuartzCore/QuartzCore.h>
#import <CoreData/CoreData.h>
#define kNames @"data.plist"
#import <AVFoundation/AVFoundation.h>


@interface ViewController : UIViewController <AVAudioPlayerDelegate>  {
 AVAudioPlayer *backgroundMusic;
}

I need stop the AudioPlayer with button.


Here is my code

AppDelegate.m
I use this code to start AVAudioPlayer.

#import "AppDelegate.h"

@implementation AppDelegate



- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{


    NSString* resourcePath = [[NSBundle mainBundle] resourcePath];
    resourcePath = [resourcePath stringByAppendingString:@"/Helloween.mp3"];
    NSLog(@"Path to play: %@", resourcePath);
    NSError* err;

    //Initialize our player pointing to the path to our resource
    backgroundMusic = [[AVAudioPlayer alloc] initWithContentsOfURL:
              [NSURL fileURLWithPath:resourcePath] error:&err];

    if( err ){
        //bail!
        NSLog(@"Failed with reason: %@", [err localizedDescription]);
    }
    else{
        //set our delegate and begin playback
        backgroundMusic.delegate = self;
        [backgroundMusic play];
        backgroundMusic.numberOfLoops = -1;
        backgroundMusic.currentTime = 0;
       backgroundMusic.volume = 1.0;
    }
    // Override point for customization after application launch.
    return YES;
}

AppDelegate.h

#import <UIKit/UIKit.h>
#import <AVFoundation/AVFoundation.h>
@interface AppDelegate : UIResponder <UIApplicationDelegate> {
    AVAudioPlayer *backgroundMusic;
}
-(IBAction)stopMus:(id)sender;
@property(nonatomic,retain) AVAudioPlayer *backgroundMusic;
@property (strong, nonatomic) UIWindow *window;

@end

ViewController.m

#import "ViewController.h"
#import <QuartzCore/QuartzCore.h>
#import <CoreData/CoreData.h>
#define fName @"data.plist"
#define kNames @"data.plist"
#import <AVFoundation/AVFoundation.h>
#import "AppDelegate.h"


@interface ViewController ()

@end
@implementation ViewController

@synthesize backgroundMusic;
-(IBAction)stopMusic:(id)sender {
   AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
      [appDelegate.backgroundMusic stop];

}

- (void)viewDidLoad
{
      [super viewDidLoad];

    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];

    //set the position of the button
    button.frame = CGRectMake(100, 100, 20, 20);
    button.backgroundColor = [UIColor whiteColor];
    [button addTarget:self action:@selector(stopMusic:) forControlEvents:UIControlEventTouchUpInside];

    //add the button to the view
    [self.view addSubview:button];

ViewController.h

#import <UIKit/UIKit.h>
#import <QuartzCore/QuartzCore.h>
#import <CoreData/CoreData.h>
#define kNames @"data.plist"
#import <AVFoundation/AVFoundation.h>


@interface ViewController : UIViewController <AVAudioPlayerDelegate>  {
 AVAudioPlayer *backgroundMusic;
}

I need stop the AudioPlayer with button.

  • 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-13T04:19:47+00:00Added an answer on June 13, 2026 at 4:19 am

    First, add UIButton to the UIView like this:

    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    
    //set the position of the button
    button.frame = CGRectMake(0, 0, 0, 0);
    
    [button addTarget:self action:@selector(yourMethod:) forControlEvents:UIControlEventTouchUpInside];
    
    //add the button to the view
    [self.view addSubview:button];
    

    Then,

    -(IBAction)yourMethod:(id)sender {
     [backgroundMusic stop];
    
    
    }
    

    After your done with the button, you can remove it via [button removeFromSuperView];
    I think, something like that should work 🙂 Please ask if your having problem with my answer.

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

Sidebar

Related Questions

My code here works fine to get the content length, and tells me how
I have an application that works fine in an older project (that doesn't use
i have this at my appdelegate: for 1st app: - (void)applicationDidFinishLaunching:(UIApplication *)application { [window
Ok the error is showing up somewhere in this here code if($error==false) { $query
I am going to start updating this to help those seeking to use this
I made a multiview app based on this Tutorial .Here is my code in
Here is my code stub for my app-delegate.m -- it never gets called. -
The script is on jsfiddle here : CODE What it does at the moment
Here is code my jqgrid editing through form. $(#DataEnergy).jqGrid('navGrid', '#pagergrid', {}, //options {editdata: {
Here my code, I need to insert into mysql database I have mysql,php,android 1)

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.