How do you mute background music when you change view controllers? - objective-c

I'm trying to create an application that plays a sound in one view controller (background music) and mutes it in the other view controllers. I have the part of the code figured out for playing background music but I can't get the application to mute it in another window? How do you I fix this? Thank you so much! :)
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
NSString *music = [[NSBundle mainBundle]pathForResource:#"Waltz" ofType:#"wav"];
audioPlayer = [[AVAudioPlayer alloc]initWithContentsOfURL:[NSURL fileURLWithPath:music] error: NULL];
audioPlayer.delegate = self;
audioPlayer.numberOfLoops = -1;
[audioPlayer play];`

You can also use notifications to pause/restart the music. That way, a view controller that doesn't know anything about the music player could simply:
[[NSNotificationCenter defaultCenter] postNotificationName:#"STOP_MUSIC" object:nil];

Related

xcode 4.3 sound will not play at app launch

I have thoroughly looked through this forum and I have not found a solution to this issue. When my app launches during the splash screen I want a 5 sec sound clip to play then stop when the app is done launching and the home screen is shown.
In the AppDelegate.m
-(BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
NSURL *musicFile = [NSURL fileURLWithPath:[[NSBundle mainBundle]
pathForResource:#"bknewsroom2"
ofType:#"caf"]];
AVAudioPlayer *launchClip = [[AVAudioPlayer alloc] initWithContentsOfURL:musicFile error:nil];
[launchClip play];
Now I have a whole bunch of stuff loading within didFinishLaunchingWithOptions this is just one of many things. The sound won't launch in the simulator nor will it launch on my phone. So am I missing something? Again it is a 5 second clip that I want to play when the app launches for the first time. Any advice would be greatly appreciated.
I fixed this issue:
in the AppDelegate.m
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
//------ PLAY SOUND CLIP WHILE LOADING APP -----
NSLog(#"launch banner with clip");
NSURL *clip = [[NSBundle mainBundle] URLForResource: #"soundfile" withExtension:#"caf"];
self.startupPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:clip error:NULL];
[self.startupPlayer play];
Now when the application launches, the sound clip plays. It does hiccup once which I'm looking into, but the function works. Also, I put this at the top of didFinishLaunchingWithOptions if it was in the middle some place, it would work.

App audio volume

I need the user to control the device's media volume. I've seen many solutions using volume view or AVAudioPlayer, but I want to use the device volume buttons to set the app volume, like many apps have.
Thanks!
To use this functionality you need to set the audio session to playback. This is done with something like this:
AVAudioSession *session = [AVAudioSession sharedInstance];
[session setCategory:AVAudioSessionCategoryPlayback error:nil];
dumped in appdelegate
- (BOOL)application:(UIApplication *)application
didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
[[NSNotificationCenter defaultCenter]
addObserver:self
selector:#selector(volumeChanged:)
name:#"AVSystemController_SystemVolumeDidChangeNotification"
object:nil];
}
- (void)volumeChanged:(NSNotification *)notification
{
float volume =
[[[notification userInfo]
objectForKey:#"AVSystemController_AudioVolumeNotificationParameter"]
floatValue];
// Do stuff with volume
}

Trying to play video using MPMoviePlayerController, only get audio

I want to play a video in my iPhone app. I used this code:
- (void)viewDidLoad
{
[super viewDidLoad];
NSURL *movieUrl = [NSURL fileURLWithPath:
[[NSBundle mainBundle] pathForResource:#"Myvideo"
ofType:#"mp4"]];
//create a new instance of MPMoviePlayerController
MPMoviePlayerController* myMovie=[[MPMoviePlayerController alloc]
initWithContentURL:movieUrl];
//disable scaling of our movie
myMovie.scalingMode = MPMovieScalingModeNone;
//don't show any controls
// myMovie.movieControlMode = MPMovieControlModeHidden;
//you can specify at which time the movie should
//start playing (default is 0.0)
myMovie.initialPlaybackTime = 2.0;
//register a callback method which will be called
//after the movie finished
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(movieFinished:)
name:MPMoviePlayerPlaybackDidFinishNotification
object:myMovie];
//start the movie (asynchronous method)
[myMovie play];
// Do any additional setup after loading the view from its nib.
}
Only the sound works with this code. Can someone help me please?
You need to allocate a frame to your movie and add it to the view. See here.
So you can add:
[myMoview.view setFrame: self.view.bounds]; // player's frame must match parent's
[self.view addSubview:myMovie.view];
Apple Technical Q&A 1240:
MPMoviePlayerController plays movie audio but not video
Q: I'm able to successfully play movies using MPMoviePlayerController on iOS 3.1.3. When I run this same code on the iPad and on the iPhone with iOS 4 I can hear the movie audio, but the video is no longer displayed. What's going on?
A: Starting with iPhone iOS 3.2, calling the -play: method still starts playback of the movie but it does not ensure that the movie is visible. In order to display a movie, you must get the new view property from your MPMoviePlayerController object and add that view to your view hierarchy. Here's a code snippet:
Listing 1 How to add the MPMoviePlayerController view property to your view hierarchy to play a movie.
#import <UIKit/UIKit.h>
#import <MediaPlayer/MediaPlayer.h>
MPMoviePlayerController *player = [[MPMoviePlayerController alloc] initWithContentURL:movieURL];
[[player view] setFrame:[myView bounds]]; // size to fit parent view exactly
[myView addSubview:[player view]];
[player play];
See Important Porting Tip for Using the Media Player Framework and the MPMoviePlayerController Class Reference for more information.

iPod - Let the native music app to play sound in background

I am building an application for iPod.
When running it, the native iPod music app should continue play in background, but this is not happening.
I think it might have something to do with the sound that it is played by my app. For playing a short sound at a certain point, I am using AVAudioPlayer. The code looks like this:
NSString *path = [[NSBundle mainBundle] pathForResource:#"sound" ofType:#"mp3"];
NSLog(#"%#", path);
player = [[AVAudioPlayer alloc] initWithData:[NSData dataWithContentsOfFile:path] error:nil];
[player prepareToPlay];
[player play];
Could this interfere with the native music player on iPod?
And another thing. In my app, I have integrated AdWhirl and it appears to use AudioToolbox.
Thanks a lot,
George
You must set up your AVAudioSession to use the Ambient category. This tells the system that your sound should blend with any already playing sound. The best way to do this in iOS 4 is in the app delegate, like this:
- (void)applicationDidFinishLaunching:(UIApplication *)application {
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryAmbient error:nil];
// ...
}
- (void)applicationDidBecomeActive:(UIApplication *)application {
// reactivate audio session
[[AVAudioSession sharedInstance] setActive: YES error: nil];
}
Read up on Audio Sessions. If your app produces sound, you always want be conscious of and in control of your audio session.

MPMoviePlayerController: How can I make my video loop?

Thank you in advance for any help, I am a newbie and would appreciate any help here..
I have this code to play a movie and it works great. Can somebody PLEASE tell me how to make this movie loop and replay from the beginning non stop ( any code would help). Also I would like to know how to play 2 movies, one after the other, preferably with a fade or smooth transition. Thank you for any help
#import "MyAppViewController.h"
#implementation MyAppViewController
-(IBAction)button:(id)sender{
NSString *path = [[NSBundle mainBundle]
pathForResource:#"mymovie" ofType:#"mp4"];
player = [[MPMoviePlayerViewController alloc]
initWithContentURL:[NSURL fileURLWithPath:path]];
[self presentMoviePlayerViewControllerAnimated:player];
}
Set the repeatMode property of your MPMoviePlayerController to MPMovieRepeatModeOne
player = [[MPMoviePlayerViewController alloc]
initWithContentURL:[NSURL fileURLWithPath:path]];
player.moviePlayer.repeatMode = MPMovieRepeatModeOne;
MPMovieRepeatModeOne is nice but it doesn't loop the video very smoothly. Try this below (copied from another post) :
(I just got this working on my iPad 3 running iOS 5.1.1, base SDK iOS 5.1.)
When setting up the movie player, set the repeat mode to MPMovieRepeatModeNone then add the notification
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(moviePlayerDidFinish:)
name:MPMoviePlayerPlaybackDidFinishNotification
object:self.moviePlayer];
Then set up your selector to filter when the movie finishes playing:
- (void)moviePlayerDidFinish:(NSNotification *)note
{
if (note.object == self.moviePlayer) {
NSInteger reason = [[note.userInfo objectForKey:MPMoviePlayerPlaybackDidFinishReasonUserInfoKey] integerValue];
if (reason == MPMovieFinishReasonPlaybackEnded)
{
[self.moviePlayer play];
}
}
}
For the timer you can create an Int variable that has the value of your slider and then use a performSelector afterDelay:
int delayInt = 8; // Substitute the 8 for the value of your slider
[self performSelector:#selector(myMethod) withObject:nil afterDelay:delayInt];
And then in your "myMethod"
-(void) myMethod{
//the code to stop your player and remove view controller
}