Stop background music

I have this app that has several view controllers. In the app delegate, I set it so that as soon as the app finishes launching, the background music starts. However, on another view controller, I have this button that plays this video. My problem is that when I play the movie, the background audio overlaps with the movie. My question is, how do I stop the music when I play the movie and play the music after the movie ends. Here is my app_delegate.h:

#import <UIKit/UIKit.h>
#import <AVFoundation/AVFoundation.h>

@interface App_Delegate : NSObject <UIApplicationDelegate> {

    UIWindow *window;
    UINavigationController *navigationController;
}

@property (nonatomic, retain) IBOutlet UIWindow *window;
@property (nonatomic, retain) IBOutlet UINavigationController *navigationController;

@end

Here is my App_Delegate.m

#import "App_Delegate.h"
#import "RootViewController.h"


@implementation App_Delegate

@synthesize window;
@synthesize navigationController;


#pragma mark -
#pragma mark Application lifecycle

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

    {NSString* soundFilePath = [[NSBundle mainBundle] pathForResource:@"beethoven_sym_5_i" ofType:@"mp3"];
        NSURL* soundFileURL = [NSURL fileURLWithPath:soundFilePath];
        AVAudioPlayer* player = [[AVAudioPlayer alloc] initWithContentsOfURL:soundFileURL error:nil];
        player.numberOfLoops=-1;
        [player play];
    }


    // Override point for customization after application launch.

    // Set the navigation controller as the window's root view controller and display.
    self.window.rootViewController = self.navigationController;
    [self.window makeKeyAndVisible];

    return YES;
}


- (void)dealloc {
    [navigationController release];
    [window release];
    [super dealloc];
}


@end

My MovieViewController.h:

#import <UIKit/UIKit.h>
#import <MediaPlayer/MediaPlayer.h>
#import <AVFoundation/AVFoundation.h>

@interface MovieViewController : UIViewController {

    IBOutlet UIScrollView *sesamescroller;
}

- (IBAction)playsesamemovie:(id)sender;


@end

Finally, my MovieViewController.m

#import "MovieViewController.h"


@interface MovieViewController ()

@end

@implementation MovieViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad


- (void)viewDidUnload


- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

- (IBAction)playsesamemovie:(id)sender {
    NSString *filepath   =   [[NSBundle mainBundle] pathForResource:@"How to make Sesame chicken" ofType:@"mp4"];
    NSURL    *fileURL    =   [NSURL fileURLWithPath:filepath];
    MPMoviePlayerController *moviePlayerController = [[MPMoviePlayerController alloc] initWithContentURL:fileURL];
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(moviePlaybackComplete:)
                                                 name:MPMoviePlayerPlaybackDidFinishNotification
                                               object:moviePlayerController];
    [self.view addSubview:moviePlayerController.view];
    moviePlayerController.fullscreen = YES;
    [moviePlayerController play];
}


- (void)moviePlaybackComplete:(NSNotification *)notification
{
    MPMoviePlayerController *moviePlayerController = [notification object];
    [[NSNotificationCenter defaultCenter] removeObserver:self
                                                    name:MPMoviePlayerPlaybackDidFinishNotification
                                                  object:moviePlayerController];
    [moviePlayerController.view removeFromSuperview];
    [moviePlayerController release];
}

- (void)dealloc {
    [sesamescroller release];
    [super dealloc];
}
@end

Answers


The code you show has a local variable pointing to the player object. To control the player, other code needs to be able to find it. Like this:

In App_Delegate.h:

@property (strong) AVAudioPlayer *player;

in App_Delegate.m: (where did this underbar come from? Most unconventional!)

self.player = [[AVAudioPlayer alloc] initWithContentsOfURL:soundFileURL error:nil];
self.player.numberOfLoops=-1;
[self.player play];

Then, wherever you want to control it:

[((App_Delegate *)([UIApplication sharedApplication].delegate)).player pause];
// ...
[((App_Delegate *)([UIApplication sharedApplication].delegate)).player play];

 set scalling mode to your player

for Paused :
 [moviePlayerController setScalingMode:MPMoviePlaybackStatePaused];

 for Stopped:
 [moviePlayerController setScalingMode:MPMoviePlaybackStateStopped];

If possible on iOS, you can use scriptable feature to send message to mute. I did this sometime back for Mac OS X where I used to control iTunes from another app.


First add start to songs in application delegate

.h application delegate

 #import <UIKit/UIKit.h>
 #import <AudioToolbox/AudioToolbox.h>
 #import <AVFoundation/AVFoundation.h>
 @class ViewController;
 @interface AppDelegate : UIResponder <UIApplicationDelegate,AVAudioPlayerDelegate>
{
    AVAudioPlayer *myAudioPlayer;
    NSDictionary *config;
   NSMutableArray *ARRAY;

}
-(void)stop;
@property(retain,nonatomic) NSDictionary *config;
@property (nonatomic, retain) AVAudioPlayer *myAudioPlayer;
@property (strong, nonatomic) UIWindow *window;

@property (strong, nonatomic) ViewController *viewController;

@end

.m application delegate

  @implementation AppDelegate
  {
      AVAudioPlayer*  audioPlayer;
  }
  @synthesize myAudioPlayer;
   - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions: (NSDictionary *)launchOptions
  {
        audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:&error];
        audioPlayer.delegate = self;
       [[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error:nil];
       [[AVAudioSession sharedInstance] setActive: YES error: nil];
       [[UIApplication sharedApplication] beginReceivingRemoteControlEvents];
      audioPlayer.numberOfLoops =  1;
      audioPlayer.delegate=self;
      [audioPlayer play];
 }
 -(void)stop
 {
     [audioPlayer stop];

 }
 -(void)star
 {
     [audioPlayer play];

 }

when use required start and stop background music in application

directly call this method -start and -stop

..it really work


Need Your Help

How to find the package name of default settings application?

android package settings taskmanager

I want to prevent launching of task manager and Settings applications in my application.For this,I tried to obtain currently running application and checked whether their package name is allowed o...

addAccount not called from AbstractAccountAuthenticator implementation from MainActivity

java android accountmanager

I am following a tutorial for adding a user account to the Android AccountManager.