can I stop the play back using my app - iphone

I am using AudioPlayer to stop all playbacks , as code is given
NSURL *url = [NSURL fileURLWithPath:[NSString stringWithFormat:#"%#/audiofile.mp3", [[NSBundle mainBundle] resourcePath]]];
NSError *error;
audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:&error];
audioPlayer.numberOfLoops = -1;
if (audioPlayer == nil)
NSLog([error description]);
else
[audioPlayer play];
But i can stop all play back using this but can not again play stopped play backs.How can I do this please help

You can easily play pause stop your AVAudioPlayer with these three methods:
- (void)pause
- (void)stop
- (BOOL)play //Returns YES on success, or NO on error.
If you pause, you can then play to resume from where you paused.
Hope this helps, i really don't see where your problem is!
in the code you gave, you are not pausing, you are just playing it with numberOfLoops negative.
You should have a method to start your music like this:
NSURL *url = [NSURL fileURLWithPath:[NSString stringWithFormat:#"%#/audiofile.mp3", [[NSBundle mainBundle] resourcePath]]];
NSError *error;
audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:&error];
audioPlayer.numberOfLoops = 0;//play once
if (audioPlayer == nil)
NSLog([error description]);
else
[audioPlayer prepareToPlay];
[audioPlayer play];
And another for pausing:
[audioPlayer pause];
And another for resuming:
[audioPlayer play];
To toggle iPod music when the app starts and exits override these two methods:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
MPMusicPlayerController *musicPlayer;
MPMusicPlaybackState playbackState = [musicPlayer playbackState];
if (playbackState == MPMusicPlaybackStatePlaying) { //simple verification
[musicPlayer pause];
}
}
- (void)applicationWillResignActive:(UIApplication *)application
if (playbackState == MPMusicPlaybackStateStopped || playbackState == MPMusicPlaybackStatePaused) {//simple verification
[musicPlayer play];
}
}
Hope it finally suits your needs!

Related

XCODE IOS memory leak for AVAudioPlayer

I use ARC.
Here is the method for starting the audio in my code:
.h file:
#property (retain,nonatomic)AVAudioPlayer *myAudio;
and .m files
-(void)myPlaySound:(NSString *)mySoundFile NumberOfLoops:(int)loopsCount ofType:(NSString *)fileType
{
if([myAudio isPlaying])
{
[myAudio stop];
}
NSURL* musicFile = [NSURL fileURLWithPath:[[NSBundle mainBundle]
pathForResource:mySoundFile
ofType:fileType]];
NSError *error;
myAudio = [[AVAudioPlayer alloc] initWithContentsOfURL:musicFile error:&error];
self.theAudio = myAudio;
myAudio.volume = audioVolume;
myAudio.numberOfLoops = loopsCount;
myAudio.currentTime = 0.0;
[myAudio prepareToPlay];
if([myAudio play] == NO)
{
NSLog(#"error");
}
}
Based on the button press, I play different audio files of different length and bit rate and types.
Its working, but I get memory leak issue.

AVAudioPlayer - Pause button

My previous code was
- (void) playaudio: (id) sender
{
NSString *filePath = [[NSBundle mainBundle] pathForResource:#"Theme"
ofType:#"mp3"];
NSURL *fileURL = [[NSURL alloc] initFileURLWithPath:filePath];
self.audioPlayer = [[AVAudioPlayer alloc]
initWithContentsOfURL:fileURL error:nil];
self.audioPlayer.currentTime = 0;
[self.audioPlayer play];
}
- (void)pause: (id)sender
{
[audioPlayer pause];
}
- (void)stop: (id)sender
{
[audioPlayer stop];
}
In the above code pause button was acting as stop button rather than pause where it was supposed to resume the audio file.
Now i have added simple statements to my code it is working to some extent but still not upto my expectations.
What happening now is when you play audio file and click on pause button nothing happens but when you click on the stop button it stops playing audio file and then when you press the pause button it resumes the audio file from where it was stopped by pressing stop button. Why is that when you press stop buttons only then pause button functions but not before that. I don't get this why?
Any ideas why this is happening
- (void)pause: (id)sender
{
[audioPlayer pause];
[audioPlayer prepareToPlay];
[audioPlayer play];
}
- (void) stop: (id) sender
{
[audioPlayer stop];
}
If anyone have any ideas that why this is happening. Will appreciate help.
Thanks in advance.
You shouldn't be recreating the audio file every time you play it. Here is how you could do this:
- (void) playaudio: (id) sender
{
if(self.audioPlayer == nil) {
NSString *filePath = [[NSBundle mainBundle] pathForResource:#"Theme"
ofType:#"mp3"];
NSURL *fileURL = [[NSURL alloc] initFileURLWithPath:filePath];
self.audioPlayer = [[AVAudioPlayer alloc]
initWithContentsOfURL:fileURL error:nil];
self.audioPlayer.currentTime = 0; //this could be outside the if if you want it to start over when they hit play
}
[self.audioPlayer play];
}
- (void)pause: (id)sender
{
if([audioPlayer isPlaying]){
[audioPlayer pause];
} else {
[audioPlayer play];
}
}
- (void)stop: (id)sender
{
[audioPlayer stop];
}

Handle audio interruptions AVAudioPlayer

I am playing "sound effects" in my app when buttons are clicked and such and I have been having some difficulties getting the delegates to work properly within the AVAudioPlayer class to keep the music playing after the sound. I have the framework imported and delegates set but I can not seem to get the AVAudioSession shared instance to work... I know I am missing something but can not seem to find a clear answer anywhere even within apples docs. Here is the code I am using trying to accomplish this.
#import "AVFoundation/AVAudioPlayer.h
#import "MediaPlayer/MediaPlayer.h"
#interface HomeViewController : UIViewController <AVAudioPlayerDelegate, MPMediaPlayback
> {
When a button is pushed
-(IBAction)About
{
NSString *path = [[NSBundle mainBundle] pathForResource:#"button" ofType:#"caf"];
AVAudioPlayer* theAudio = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL];
theAudio.delegate = self;
[theAudio play];
AboutViewController *about = [[AboutViewController alloc] init];
[self presentModalViewController:about animated:YES];
NSString *path1 = [[NSBundle mainBundle] pathForResource:#"move" ofType:#"caf"];
AVAudioPlayer* theAudio1 = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path1] error:NULL];
theAudio.delegate = self;
[theAudio1 play];
}
Delegates trying to either play my sound as a "ambient noise" or just resume play after my sound happens
-(void)audioPlayerDidFinishPlaying:(AVAudioPlayer *)player successfully:(BOOL)flag
{
}
-(void)audioPlayerDecodeErrorDidOccur:(AVAudioPlayer *)player error:(NSError *)error
{
}
-(void)audioPlayerBeginInterruption:(AVAudioPlayer *)player
{
}
-(void)audioPlayerEndInterruption:(AVAudioPlayer *)player withFlags:(NSUInteger)flags
{
if (flags)
{
AVAudioPlayer *audio = [[AVAudioPlayer alloc] init];
[audio setDelegate:self];
[audio play];
}
}
Now the last delegate from what I understand is the only delegate of use really in my case and what I have will not work since I need to be using the [AVAudioSession SharedInstance] like this but I can not seem to get the AVAudioSession to work!
-(void)audioPlayerEndInterruption:(AVAudioPlayer *)player withFlags:(NSUInteger)flags
{
if (flags == AVAudioSessionFlags_ResumePlay)
{
[player play];
}
}
Any help or suggestions would be greatly appreciated. I basically just need to know how to get the [AVAudio SharedInstance] so if anyone knows what class I need to import or framework I need to add to get this working it would be very much obliged!
Thanks

Play/Pause with the same button [AVAudioPlayer]

How can I play a sound with an IBAction by pressing on a UIbutton once and pause it by pressing the button again using AVAudioPlayer? Also I want to change the state of that UIButton when the sound is playing and when it's not.
Here's my code:
- (IBAction)Beat
{
if ([Media2 isPlaying])
{
[Media2 pause];
[Button17 setSelected:NO];
}
else
{
Path = [[NSBundle mainBundle] pathForResource:#"Beat" ofType:#"mp3"];
AVAudioPlayer *Media2 = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:Path] error:NULL];
[Media2 setDelegate:self];
[Media2 play];
[Button17 setSelected:YES];
}
}
Here's is simple method using BOOL Variable.
Set playing = NO in viewDidLoad.
-(void)PlayStop{
if (playing==NO) {
// Init audio with playback capability
[play setBackgroundImage:[UIImage imageNamed:#"hmpause.png"] forState:UIControlStateNormal];
AVAudioSession *audioSession = [AVAudioSession sharedInstance];
[audioSession setCategory:AVAudioSessionCategoryPlayback error:nil];
audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:______ error:&err];
[audioPlayer prepareToPlay];
audioPlayer.delegate=self;
[audioPlayer play];
playing=YES;
}
else if(playing==YES){
[play setBackgroundImage:[UIImage imageNamed:#"Audioplay.png"] forState:UIControlStateNormal];
[audioPlayer pause];
playing=NO;
}
}
Keep your audioPlayer instance ready to play using the below method.
/*
Prepares the audio file to play.
*/
-(void) initWithAudioPath:(NSString *) audioPath {
// Converts the sound's file path to an NSURL object
NSURL *audioPathURL = [[NSURL alloc] initFileURLWithPath:audioPath];
self.audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:audioPathURL error:nil];
audioPlayer.delegate = self;
[audioPlayer prepareToPlay];
[audioPathURL release];
}
-(void) pausePlaybackForPlayer:(AVAudioPlayer *) player {
[player pause];
}
-(void) startPlaybackForPlayer:(AVAudioPlayer *) player {
if (![player play]) {
NSLog(#"Could not play %#\n", player.url);
}
}
- (IBAction)Beat {
if (audioPlayer.playing == NO) {
// Audio player is not playing.
// Set the button title here to "stop"...
[self startPlaybackForPlayer:audioPlayer];
}else {
// Audio player is playing.
// Set the button title here to "play"...
[self pausePlaybackForPlayer:audioPlayer];
}
}

stopping an AVAudioPlayer

here is my code
NSString *soundFilePath = [[NSBundle mainBundle] pathForResource:#"95" ofType:#"wav"];
NSError *activationError = nil;
NSError *audioPlayerInitError = nil;
[[AVAudioSession sharedInstance] setActive: YES error:&activationError];
NSURL *newURL = [NSURL fileURLWithPath:soundFilePath];
musicPlayer1 = [[AVAudioPlayer alloc] initWithContentsOfURL:newURL error:&audioPlayerInitError];
if (musicPlayer1) {
[musicPlayer1 stop];
[musicPlayer1 release];
musicPlayer1 = nil;
}
else {
[musicPlayer1 prepareToPlay];
[musicPlayer1 setVolume:.8];
[musicPlayer1 setNumberOfLoops:-1]; // -1 means play indefintely
[musicPlayer1 setDelegate: self];
[musicPlayer1 play];
}
}
I am rying to start and stop the AVAudioPlay with the same button (musicPlayer1 is in the header). Now when i touch the button it does not play anything. Help please?
You're creating a new instance of AVAudioPlayer each time. You need to hold onto the reference to the object somewhere, and refer back to that. Add a field to your view controller class:
#private AVAudioPlayer *currentAudio;
And then in this method, make the following changes:
if (currentAudio) {
[currentAudio stop];
[currentAudio release];
currentAudio = nil;
} else {
// what you already have goes here
}