How To Play Looping Sound In Your iPhone App

Here is a very useful code snippet for playing your own sound file continuously in your iPhone application. The example uses a .caf file and is based on the AVAudioPlayer Class. You can find the class reference of Apple here: http://developer.apple.com/iPhone/library/documentation/AVFoundation/Reference/AVAudioPlayerClassReference/Reference/Reference.html Let’s assume that you have a view controller where you want to play some […]


Playing Sound
Here is a very useful code snippet for playing your own sound file continuously in your iPhone application. The example uses a .caf file and is based on the AVAudioPlayer Class. You can find the class reference of Apple here:
http://developer.apple.com/iPhone/library/documentation/AVFoundation/Reference/AVAudioPlayerClassReference/Reference/Reference.html

Let’s assume that you have a view controller where you want to play some sound. I will first start by the header file of your view controller and then move on to the methods in your class MyViewController.m.

In the header file MyViewController.h we will define the audio player first:

#import <UIKit/UIKit.h>

@interface MyViewController : UIViewController {
    AVAudioPlayer *player;
}

@property (nonatomic, retain) AVAudioPlayer *player;

@end

In MyViewController.m we want to load the sound file from our project and allocate the AVAudioPlayer. Further, we will want to define the looping behavior and the volume. This is how we integrate all that in MyViewController.m:

#import "MyViewController.h"

@interface MyViewController()
    -(void)playSound;
@end

@implementation MyViewController

@synthesize player;

- (void)viewDidLoad {
    [super viewDidLoad];
    NSString *soundFilePath = 
      [[NSBundle mainBundle] pathForResource: @"mySound" ofType: @"caf"];
    NSURL *fileURL = 
      [[NSURL alloc] initFileURLWithPath: soundFilePath];
    AVAudioPlayer *newPlayer = 
      [[AVAudioPlayer alloc] initWithContentsOfURL: fileURL error: nil];
    [fileURL release];
    self.player = newPlayer;
    [newPlayer release];
}	

- (void)viewWillAppear:(BOOL)animated{
    player.numberOfLoops = -1;
    player.currentTime = 0;
    player.volume = 1.0;
    [self playSound];
}

- (void)viewWillDisappear:(BOOL)animated{
    if (self.player.playing) {
        [self.player stop];
    }
}

- (void) playSound{
    [self.player play];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

- (void)viewDidUnload {
}

- (void)dealloc {
    [super dealloc];
    [player release];
}

@end

Setting numberOfLoops to a negative integer will make your sound loop until you call stop to terminate the playing of the sound. The currentTime is the moment from when you want to play the sound in seconds. So zero means that you want to start from the beginning.
The method playSound will call play to actually play the sound.

Check out the class reference for more options, like pausing your sound or playing more sounds simultaneously.
I hope it helped! Enjoy!





Previous:
Next:

Tagged with:

Mary Lou (Manoela Ilic) is a freelance web designer and developer with a passion for interaction design. She studied Cognitive Science and Computational Logic and has a weakness for the smell of freshly ground peppercorns.

View all contributions by

Website: http://tympanus.net/

Related Articles

Feedback 1

Comments are closed.
  1. 1

    Do you plan to keep this site updated? I sure hope so… its great!

Comments are closed.