Playing wav sound file with AVFoundation

12,711

Solution 1

Here is the solution;

I set AVAudioPlayer in header file..

@property (nonatomic, retain) AVAudioPlayer *theAudio;

i guess 'retain' is solved my problem. Because after i sending "play", it sends "release" to balance the alloc. When AVAudioPlayer is deallocated it stops playing audio.

Solution 2

I have had the same issue with Xcode 4.5. Making the AVAudioPlayer into a strongly typed property made it play.

So the following code needs added to the @interface:

@property (nonatomic, strong) AVAudioPlayer *theAudio;

The @implementation would then become:

- (IBAction) playSelectedAlarm:(id)sender {

    UIButton *button = (UIButton *)sender;

    int bTag = button.tag;

    NSString *fileName = [NSString stringWithFormat:@"%d23333", bTag];

    NSLog(@"%@", fileName);

    NSString *path = [[NSBundle mainBundle] pathForResource:fileName ofType:@"wav"];

    NSURL *fileURL = [[NSURL alloc] initFileURLWithPath: path];

    self.theAudio=[[AVAudioPlayer alloc] initWithContentsOfURL:fileURL error:NULL];

    self.theAudio.volume = 1.0;

    self.theAudio.delegate = self;

    [self.theAudio prepareToPlay];

    [self.theAudio play];

}
Share:
12,711
Quaso
Author by

Quaso

Updated on June 04, 2022

Comments

  • Quaso
    Quaso almost 2 years

    I am using AVFoundation to play wav files. But i could not make it play. Also no errors or warnings showed up.

    XCode is 4.2 and device is iOS 5.

    - (IBAction) playSelectedAlarm:(id)sender {
    
    UIButton *button = (UIButton *)sender;
    
    int bTag = button.tag;
    
    NSString *fileName = [NSString stringWithFormat:@"%d23333", bTag];
    
    NSLog(@"%@", fileName);
    
    NSString *path = [[NSBundle mainBundle] pathForResource:fileName ofType:@"wav"];
    
    NSURL *fileURL = [[NSURL alloc] initFileURLWithPath: path];
    
    AVAudioPlayer *theAudio=[[AVAudioPlayer alloc] initWithContentsOfURL:fileURL error:NULL];
    
    theAudio.volume = 1.0;
    
    theAudio.delegate = self;
    
    [theAudio prepareToPlay];
    
    [theAudio play];
    
    }
    
    • mostafa tourad
      mostafa tourad over 12 years
      Make sure you are not ignoring any possible errors (as you are within the initWithContentsOfUrl call). Use proper NSError-objects and print their results to the console.
    • slonkar
      slonkar over 12 years
      also check whether you forgot to add Audio Foundation framework.
    • Quaso
      Quaso over 12 years
      @SumitLonkar: Double checked :)
    • Quaso
      Quaso over 12 years
      @Till Still got no error. NSError *audioError = nil; AVAudioPlayer *theAudio=[[AVAudioPlayer alloc] initWithContentsOfURL:fileURL error:&audioError]; if(audioError != nil) { NSLog(@"An audio error occurred: \"%@\"", audioError); } else { [theAudio prepareToPlay]; [theAudio play]; }
    • mostafa tourad
      mostafa tourad over 12 years
      WAV is a format I would actually not use - try converting that file to AIFF or CAF and use the result. The WAV-Container format has gone through some slight irregularities sometimes introducing playback failures.
    • Quaso
      Quaso over 12 years
      Converted them to CAF but it didn't solve the problem.
  • Steve Liddle
    Steve Liddle over 11 years
    If you use a weak property (or a local variable) and you're using ARC, iOS will detect that there are no strong pointers to the AVAudioPlayer object and free it before you want the object removed. The strong pointer retains the object, preventing it from being removed until you explicitly nil out the property.
  • amisha.beladiya
    amisha.beladiya over 7 years
    if i upload audio file by select the MPMediaPickerController then play in background .Music thumbnail not show at home screen.how can solve this issue?