0

I have a soundtrack I would like to play throughout my app even when users change view controllers and such. I have a loop from a soundtrack that I want to repeat once it finishes. I am currently playing sounds through:

NSString *soundFile = [[NSBundle mainBundle]
                       pathForResource:@"Click03" ofType:@"wav"];
AVAudioPlayer  *audioPlayer = [[AVAudioPlayer alloc]
               initWithContentsOfURL:[NSURL fileURLWithPath: soundFile]
               error:nil];
[audioPlayer start];
 // or [audioPlayer stop];

I am not quite sure if this the best way of playing sounds. Can someone explain to me how to do what I asked above? Thanks in advance!

Kevin
  • 53,822
  • 15
  • 101
  • 132
Alex G
  • 2,299
  • 5
  • 37
  • 54

2 Answers2

1

Basically you are trying to play an background audio..

Check the following links this would help you

Play music in the background using AVAudioplayer

http://www.sagorin.org/2011/11/29/ios-playing-audio-in-background-audio/

For repeating audio check this link

How do I repeat a AVAudioPlayer?

Community
  • 1
  • 1
Dilip Rajkumar
  • 7,006
  • 6
  • 60
  • 76
0

Write code to play sound in Appdelegate.

-(void)PlaySound:(NSString*)strSoundFileName 
{
    fileURL=[[NSURL alloc] initFileURLWithPath:strSoundFileName];
    audioPlayer=[[AVAudioPlayer alloc] initWithContentsOfURL:fileURL error:nil];
    audioPlayer.delegate=self;
    [audioPlayer prepareToPlay];

    audioPlayer.currentTime=0;
    [audioPlayer play];
    audioPlayer.numberOfLoops=-1;
}
Dilip Rajkumar
  • 7,006
  • 6
  • 60
  • 76
user1113101
  • 955
  • 1
  • 5
  • 12