In my app song play from ipod library through AVAudioPlayer,
problem is that when one song is play & select second song,
second song overlap,
i try to stop the song with call this method
- (IBAction)player_stop {
theAudio.delegate = nil;
[theAudio release];
theAudio = nil;
}
I try this method but not going in this condition
if (theAudio.isPlaying == YES){
[theAudio release];
theAudio = nil;
}
theAudio is Object of AVAudioplayer,
How can i do that?
what about
if (theAudio.isPlaying == YES){
[theAudio stop];
}
Related
I have been trying to learn AVAudioPlayer and it's behaviour.
So to start with I had written
AVAudioPlayer *player = [[AVAudioPlayer alloc] initWithContentsOfURL:soundFileURL error:nil];
player.numberOfLoops = -1;
[player play];
It did worked well as long as application did not enter background. So I searched again and found that I need to add entry in plist and also have written following code
NSError *activationError = nil;
if([[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error:&activationError] == NO)
NSLog(#"*** %# ***", activationError);
[[AVAudioSession sharedInstance] setActive: YES error: nil];
[[UIApplication sharedApplication] beginReceivingRemoteControlEvents];
Now my sample application plays sound in background as well. Then I encountered another problem. If I put sleep before all this processing, take my application in background during the sleep and start playing music from some other app, in this case my application is not able to play sound. It is probably because it does not get the audio session which some other music application has taken.
Questions
1. I understand that there is a way to detect if any other sound is playing, but I could not find how do I stop it. There are methods for stopping standard app sound like iPod, but is there any generic method to stop the sound from any other application.
2. [player play]; gives NO as result which is failure. How do I find what failure has caused. I have implemented audioPlayerDecodeErrorDidOccur:error: method, but it is never called.
Please share your knowledge on this. I have already gone through most of the stack overflow questions regarding the same, but did not find anything useful to this particular problem.
-(void)loadPlayer
{
NSURL *audioFileLocationURL = [NSURL URLWithString:[[NSBundle mainBundle] URLForResource:#"01-YedhiYedhi[www.AtoZmp3.in]" withExtension:#"mp3"]];
NSError *error;
audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:audioFileLocationURL error:&error];
[audioPlayer setNumberOfLoops:-1];
if (error)
{
NSLog(#"%#", [error localizedDescription]);
[[self volumeControl] setEnabled:NO];
[[self playPauseButton] setEnabled:NO];
[[self alertLabel] setText:#"Unable to load file"];
[[self alertLabel] setHidden:NO];
}
else
{
[[self alertLabel] setText:[NSString stringWithFormat:#"%# has loaded", str]];
[[self alertLabel] setHidden:NO];
//Make sure the system follows our playback status
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error:nil];
[[AVAudioSession sharedInstance] setActive: YES error: nil];
//Load the audio into memory
[audioPlayer prepareToPlay];
}
}
if (!self.audioPlayer.playing) {
[self playAudio];
} else if (self.audioPlayer.playing) {
[self pauseAudio];
}
}
- (void)playAudio {
[audioPlayer play];
}
I am using a AVAudio player to play a song,
I take a song from ipod library,
i play a first song but when select other song from ipod library that both song play simultaneously,
How to stop a first song?
I tried to stop a song
- (IBAction)player_stop {
[theAudio stop];
theAudio = Nil;
}
on button play method i write this code
- (IBAction)btn_play {
[self player_stop];
[theAudio play];
}
How i Do that?
You should tell the player, not the audio, to stop.
[self.player stop];
To start a new audio you can do as follows:
[self.player stop];
self.player = nil;
NSError *error;
self.player = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:&error];
[self.player play];
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
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!
I have 6 sounds on one view.
However I want it so that I can play more than one at once, So you tap sound 1 (sound 1 is playing) then sound 2 plays. while sound 1 is still playing.
But at the moment I press sound 1 (sound 1 plays), press sound 2 (sound 2 plays, but sound 1 stops)
Here is the code for the audio part.
- (IBAction)oneSound:(id)sender; {
NSString *path = [[NSBundle mainBundle] pathForResource:#"1" ofType:#"wav"];
if (theAudio) [theAudio release];
theAudio = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL];
theAudio.delegate = self;
[theAudio play];
volumeTimer = [NSTimer scheduledTimerWithTimeInterval:0.05 target:self selector:#selector(updateVolume) userInfo:nil repeats:YES];
}
- (IBAction)twoSound:(id)sender; {
NSString *path = [[NSBundle mainBundle] pathForResource:#"2" ofType:#"wav"];
if (theAudio) [theAudio release];
theAudio = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL];
theAudio.delegate = self;
[theAudio play];
volumeTimer = [NSTimer scheduledTimerWithTimeInterval:0.05 target:self selector:#selector(updateVolume) userInfo:nil repeats:YES];
}
There is some important code missing, but it looks as if theAudio is a global you are using to manage the playing of your sounds. Since you destroy it whenever a sound is played whatever is currently playing will stop in preparation for the next sound.
There are several ways to fix this, one being that each sound gets its own unique AVAudioPlayer instance.
don't release the player just because it exists. release when finished playing
You should implement this in the app delegate or a property of the app delegate so that the delegate reference remains valid if you pop the view.
You should not need to do this for each voice. Each voice will initial the player with a different filename or URL. You gain this by making sure that you will release when finished playing. The other thing to take care of is releasing players if the player doesn't finish because of an interruption.
#pragma mark -
#pragma mark Audio methods
-(void)playNote:(NSInteger)noteNumber {
NSString *soundFilePath =
[[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:#"%i", noteNumber ]
ofType: #"caf"];
NSURL *fileURL = [[NSURL alloc] initFileURLWithPath: soundFilePath];
AVAudioPlayer *playerToPrepare = [[AVAudioPlayer alloc] initWithContentsOfURL:fileURL
error:nil];
[fileURL release];
[playerToPrepare prepareToPlay];
[playerToPrepare setDelegate: self];
[playerToPrepare play];
}
#pragma mark -
#pragma mark AVAudioPlayer delegate methods
- (void) audioPlayerDidFinishPlaying: (AVAudioPlayer *) playerThatFinished
successfully: (BOOL) completed {
if (completed) {
[playerThatFinished release];
}
}
You can declare and use:
AVAudioPlayer *theAudio1; // for file 1.wav
AVAudioPlayer *theAudio2; // for file 2.wav
...
AVAudioPlayer *theAudio6; // etc.
instead of releasing and reusing just one AVAudioPlayer.