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 […]


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!

Tiny break: 📬 Want to stay up to date with frontend and trends in web design? Subscribe and get our Collective newsletter twice a tweek.

Manoela Ilic

Manoela is the main tinkerer at Codrops. With a background in coding and passion for all things design, she creates web experiments and keeps frontend professionals informed about the latest trends.

Stay in the loop: Get your dose of frontend twice a week

👾 Hey! Looking for the latest in frontend? Twice a week, we'll deliver the freshest frontend news, website inspo, cool code demos, videos and UI animations right to your inbox.

Zero fluff, all quality, to make your Mondays and Thursdays more creative!

Feedback 1

Comments are closed.
  1. Do you plan to keep this site updated? I sure hope so… its great!