0

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.

Sagrian
  • 1,048
  • 2
  • 11
  • 29
  • please print your soundFileURL – Manu Feb 22 '13 at 06:34
  • @Manohar : used following code to get URL `NSString *soundFilePath = [[NSBundle mainBundle] pathForResource:@"sound" ofType:@"mp3"]; NSURL *soundFileURL = [NSURL fileURLWithPath:soundFilePath];` It did worked until application was in background and nothing else was playing. So I don't think URL is any problem here. – Sagrian Feb 22 '13 at 06:54
  • NSURL *soundFileURL = [NSURL URLWithString: soundFilePath]; try this – Manu Feb 22 '13 at 07:07
  • @Manohar : Nope ... same issue no change – Sagrian Feb 22 '13 at 07:25

1 Answers1

0
-(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];
}
Manu
  • 4,730
  • 2
  • 20
  • 45
  • What is alertLabel and why u need to use it? I do not want any user interaction in this. Also are `audioPlayer ` and `self. audioPlayer ` different? – Sagrian Feb 22 '13 at 10:03
  • Manohar your code seems to be incomplete. I do not know answer 2 my 2n d question. also `[self playAudio]` implementation is not given. Is it just `[player play]`. – Sagrian Feb 22 '13 at 13:28