Unable to start an audio player - iphone

I have an audio player and it won't play the sound, has someone any idea why?
And i have imported AVFoundationFramework.h
This is the code:
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
NSURL* musicFile = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:#"begin" ofType:#"mp3"]];
AVAudioPlayer *audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:musicFile error:nil];
[audioPlayer play];
audioPlayer.numberOfLoops=-1;
}

Add a delegate:
[audioPlayer setDelegate:self];
and make sure your volume is correctly set.

Related

IPhone, Audio MP3 or M4A audio file plays no sound

I'm trying to play a sound method within the object didSelectedIndex UITableViewController
The audio files are files with extension m4a, but I also tried with mp3 files and the result does not change.
Below I post the code that I have implemented in the method of TableViewController:
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
NSString *soundFilePath = [NSString stringWithFormat:#"%#/testaudio.m4a", [[NSBundle mainBundle] resourcePath]];
NSURL *soundFileURL = [NSURL fileURLWithPath:soundFilePath];
NSLog(#"soundFileURL %#",soundFileURL);
NSError *error;
AVAudioPlayer *player = [[AVAudioPlayer alloc] initWithContentsOfURL:soundFileURL error:&error];
if(!player){
NSLog(#"ERRORE: %#",error);
}
[player setDelegate:self];
[player prepareToPlay];
[player play];
}
I also tried directly on the device and it still does not play any sound.
I also added a class AVSession as follows:
AVAudioSession *session = [AVAudioSession sharedInstance];
[session setActive:YES error:nil];
The result did not change, does anyone know what the problem is?
Try this (tested):
Set a property for your AVAudioPlayer object in your .m:
#property(strong, nonatomic) AVAudioPlayer *player;
Then add this in your:
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *soundFilePath;
if ((soundFilePath = [[NSBundle mainBundle] pathForResource:#"testaudio" ofType:#"m4a"]))
{
NSURL *soundFileURL = [NSURL fileURLWithPath:soundFilePath];
NSError *error;
self.player = [[AVAudioPlayer alloc] initWithContentsOfURL:soundFileURL error:&error];
if(self.player)
{
[self.player setDelegate:self];
[self.player prepareToPlay];
[self.player play];
}
else
{
NSLog(#"ERRORE: %#", error);
}
}
}
You don't need AVAudioSession in your case.

iPhone App : Starting to play music very slow

Am working on an iPad/iPhone App & am playing a background music with this code snippet which I call in the Delegate's didFinishLaunchingWithOptions method:
-(void) playMusic {
NSLog(#"play music!");
NSString *sound_file;
if ((sound_file = [[NSBundle mainBundle] pathForResource:#"musicfile" ofType:#"mp3"])){
NSURL *url = [[NSURL alloc] initFileURLWithPath:sound_file];
self.audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:NULL];
self.audioPlayer.delegate = self;
[self.audioPlayer setNumberOfLoops:-1]; // Infinite loop
[self.audioPlayer prepareToPlay];
[self.audioPlayer setVolume:0.2];
[self.audioPlayer play];
} else {
NSLog(#"WARN : music file not found!");
}
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error:nil];
}
But I see that this method alone is taking 0.43 seconds on an average, which is a lot! Am I doing it the right way? Or is there a faster way to start the music play ? Please help.
My environment : iOS5, XCode 4.3, iPad 2.0 is what I tested this with now

how to stop sound on pressing a button

I am Using Following code to play sound
NSString *laughSoundPath = [[NSBundle mainBundle]pathForResource:#"laugh" ofType:#"wav"];
NSURL *laughURL = [[NSURL alloc]initFileURLWithPath:laughSoundPath];
laughTone = [[AVAudioPlayer alloc]initWithContentsOfURL:laughURL error:nil];
[laughTone prepareToPlay];
[laughTone setDelegate:self];
[laughURL release];
-(IBAction)play
{
[laughTone play];
}
i am Using following line to stop sound
-(IBAction)stop
{
[laughTone stop];
}
Problem is.. When i play sound again, its starting from the point where it was stopped.. i mean to say its working as pause.. help me out
-(IBAction)stop
{
[laughTone stop];
[laughTone playAtTime:0];
}
Try resetting the time and then playing, again put the code in your play method:
laughTone.currentTime = 0;
[laughTone play];
Try this:
NSString *Str = [[NSBundle mainBundle] pathForResource:#"Ahhh" ofType:#"wav"];
AVAudioPlayer *audioSound =[[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:ahhhPath] error:NULL];
-(IBAction)btnStopsound(id)sender
{
[audioSound stop];
audioSound.currentTime=0;
[audioSound play]; //restart the player
}

restarting a (audio) loop after it has finished

I want to play an audio loop by turning on a switch but i do not want the loop to end until the switch is turned off. When the loop ends i want it to simply start back at the beginning as to be continuos.
Try something like this. Note the -1 tells the music to repeat indefinitely:
Import this file at the top of your file:
#import <AVFoundation/AVFoundation.h>
Then to use it:
NSString *soundFilePath = [[NSBundle mainBundle] pathForResource:resourcePath ofType:#"caf"];
NSError *activationError = nil;
NSError *audioPlayerInitError = nil;
[[AVAudioSession sharedInstance] setActive: YES error:&activationError];
NSURL *newURL = [NSURL fileURLWithPath:soundFilePath];
AVAudioPlayer *newPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:newURL error:&audioPlayerInitError];
[newPlayer prepareToPlay];
[newPlayer setVolume:.8];
[newPlayer setNumberOfLoops:-1]; // -1 means play indefintely
[newPlayer setDelegate: self];
[newPlayer play];
[newPlayer release];
And then to stop it you would do:
if ([newPlayer isPlaying]) {
[newPlayer stop];
}

AVAudioPlayer Leak and Crash

I want to play multiple audio files (.WAV) using IBAction and AVAudioPlayer. Unfortunatelly the sound plays but if I play the sound many times, my application crashes. Can you help me?
Here's my code.
ViewController.h
#import <UIKit/UIKit.h>
#import <AVFoundation/AVAudioPlayer.h>
#interface ViewController : UIViewController <AVAudioPlayerDelegate>
{
NSString *Path;
}
- (IBAction)Sound1;
- (IBAction)Sound2;
- (IBAction)Sound3;
- (IBAction)Sound4;
#end
ViewController.m
#import <AVFoundation/AVAudioPlayer.h>
#import "ViewController.h"
#implementation ViewController
AVAudioPlayer *Media;
- (IBAction)Sound1
{
Path = [[NSBundle mainBundle] pathForResource:#"Sound1" ofType:#"wav"];
Media = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:Path] error:NULL];
[Media setDelegate:self];
[Media play];
}
- (IBAction)Sound2
{
Path = [[NSBundle mainBundle] pathForResource:#"Sound2" ofType:#"wav"];
Media = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:Path] error:NULL];
[Media setDelegate:self];
[Media play];
}
- (IBAction)Sound3
{
Path = [[NSBundle mainBundle] pathForResource:#"Sound3" ofType:#"wav"];
Media = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:Path] error:NULL];
[Media setDelegate:self];
[Media play];
}
- (IBAction)Sound4
{
Path = [[NSBundle mainBundle] pathForResource:#"Sound4" ofType:#"wav"];
Media = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:Path] error:NULL];
[Media setDelegate:self];
[Media play];
}
- (void)audioPlayerDidFinishPlaying:(AVAudioPlayer *)player successfully:(BOOL)flag
{
[player release];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
}
- (void)viewDidUnload
{
[super viewDidUnload];
}
- (void)dealloc
{
[Media Release];
[super dealloc];
}
#end
There are a couple things that look wrong in your code:
(1). There are no method Release, [Media Release] should be [Media release];
(2). If you play Sound2 while Sound1 is still playing, you leak Media instance:
Media = [[AVAudioPlayer alloc] initWithContentsOfURL:...
This allocates new player and overwrites old one without releasing it first;
(3). It is usually bad idea to release calling object in delegate;
(4). I'd also suggest to rename Media to media and Path to path.
So playing action should look like this:
- (IBAction)playSound1
{
path = [[NSBundle mainBundle] pathForResource:#"Sound1" ofType:#"wav"];
media = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL];
[media play];
[media release];
}