How to stop the sound - Cocoa Touch - iphone

I have a sound looped, its a rect button, once the user presses the button the sound loops and plays. I want it so once the user has pressed the same button again, the sound stops playing.
This is the code
- (IBAction)threeSound:(id)sender; {
NSString *path = [[NSBundle mainBundle] pathForResource:#"3" ofType:#"wav"];
if (threeAudio) [threeAudio release];
NSError *error = nil;
threeAudio = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:&error];
if (error)
NSLog(#"%#",[error localizedDescription]);
threeAudio.numberOfLoops = -1;
threeAudio.delegate = self;
[threeAudio play];
}
Thanks

Pretty straight forward...
- (IBAction)threeSound:(id)sender; {
if (threeAudio && threeAudio.playing) {
[threeAudio stop];
[threeAudio release];
threeAudio = nil;
return;
}
NSString *path = [[NSBundle mainBundle] pathForResource:#"3" ofType:#"wav"];
if (threeAudio) [threeAudio release];
NSError *error = nil;
threeAudio = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:&error];
if (error)
NSLog(#"%#",[error localizedDescription]);
threeAudio.numberOfLoops = -1;
threeAudio.delegate = self;
[threeAudio play];
}

You should separate those two processes
into this
`- (void)initSound {
NSString *path = [[NSBundle mainBundle] pathForResource:#"3" ofType:#"wav"];
NSError *error = nil;
threeAudio = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:&error];
if (error)
NSLog(#"%#",[error localizedDescription]);
threeAudio.numberOfLoops = -1;
threeAudio.delegate = self;
[threeAudio prepareToPlay];
[threeAudio play];
}
-(IBAction)threeSound:(id)sender:(UIButton *)sender{
if (sender.selected) {
[threeAudio pause];
} else {
threeAudio.currentTime = 0;
[threeAudio play];
}
sender.selected = !sender.selected;
}`
Note that initSound needs to be called at some point, if you want to start the sound with the button only.. then add
if (!threeAudio) {
[self initSound];
}
at the beggining of the IBAction
Thats my advice ;)

Related

Audio file not play from server

I am playing audio file from server but it is not playing if i play local file then it plays here is my code.
//NSURL *url = [NSURL fileURLWithPath:[NSString stringWithFormat:#"%#/audiofile.mp3", [[NSBundle mainBundle] resourcePath]]];
NSURL *url = [NSURL URLWithString:#"http://celeritas-solutions.com/pah_brd_v1/productivo/1.mp3"];
NSError *error;
audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:&error];
audioPlayer.numberOfLoops = -1;
if (audioPlayer == nil)
NSLog([error description]);
else
[audioPlayer play];
Try to use MPMoviePlayerController instead of AVAudioPlayer
NSURL *url = [NSURL URLWithString:#"http://celeritas-solutions.com/pah_brd_v1/productivo/1.mp3"];
mv = [[MPMoviePlayerController alloc] initWithContentURL:url];
mv.movieSourceType = MPMovieSourceTypeUnknown;
[self.view addSubview:mv.view];
[mv play];
Use this code to solve your problem...
NSData *_objectData = [NSData dataWithContentsOfURL:[NSURL URLWithString:http://celeritas-solutions.com/pah_brd_v1/productivo/1.mp3]];
NSError *error;
audioPlayer = [[AVAudioPlayer alloc] initWithData:_objectData error:&error];
audioPlayer.numberOfLoops = 0;
audioPlayer.volume = 1.0f;
[audioPlayer prepareToPlay];
if (audioPlayer == nil)
NSLog([error description]);
else
[audioPlayer play];

play mp3 from network

I have tried below three solutions but none of them worked. I am sure I am missing some key point but not clear what. (FYI, I am trying out below solutions on iPhone device, not on simulator.)
- (IBAction)actionAVPlayer:(id)sender {
AVPlayer* player = [[AVPlayer alloc] initWithURL:[NSURL URLWithString:urlTextField.text]];
[player play];
}
- (IBAction)actionMemoryBuffer1:(id)sender {
NSError *error;
NSData *_objectData = [NSData dataWithContentsOfURL:[NSURL URLWithString:urlTextField.text]];
AVAudioPlayer* audioPlayer = [[AVAudioPlayer alloc] initWithData:_objectData error:&error];
audioPlayer.numberOfLoops = -1;
audioPlayer.delegate = self;
audioPlayer.volume = 1.0f;
[audioPlayer prepareToPlay];
if (audioPlayer == nil)
NSLog(#"%#", [error description]);
else
[audioPlayer play];
}
- (IBAction)actionMemoryBuffer2:(id)sender {
NSURLRequest* request = [ NSURLRequest requestWithURL: [NSURL URLWithString:urlTextField.text] ];
NSError *requestError;
NSURLResponse *urlResponse = nil;
NSData *_objectData = [ NSURLConnection sendSynchronousRequest:request returningResponse: &urlResponse error: &requestError];
NSError *error;
AVAudioPlayer* audioPlayer = [[AVAudioPlayer alloc] initWithData:_objectData error:&error];
audioPlayer.numberOfLoops = -1;
audioPlayer.delegate = self;
audioPlayer.volume = 1.0f;
[audioPlayer prepareToPlay];
if (audioPlayer == nil)
NSLog(#"%#", [error description]);
else
[audioPlayer play];
}
Any help is much appreciated.
Try using MPMoviePlayerController. First you need to import the MediaPlayer framework. Then it's as easy as this:
MPMoviePlayerController *myAudioPlayer = [[MPMoviePlayerController alloc] initWithContentURL:[NSURL URLWithString:urlTextField.text]];
[myAudioPlayer play];

How to play a music file using URL link in iPhone?

I want to play a music file using URL Link in the iPhone. But when I use the below code I am getting error I am not getting where I am going wrong. Can anyone Correct me?
-(void)viewDidLoad
{
[super viewDidLoad];
NSString* resourcePath = #"http://192.167.1.104:8888/SHREYA.mp3"; //your url
NSData *_objectData = [NSData dataWithContentsOfURL:[NSURL URLWithString:resourcePath]];
NSError *error;
audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:_objectData error:&error];
audioPlayer.numberOfLoops = -1;
if (audioPlayer == nil)
NSLog([error description]);
else
[audioPlayer play];
}
This should work:
NSURL *url = [NSURL URLWithString:#"<#Live stream URL#>];
// You may find a test stream at <http://devimages.apple.com/iphone/samples/bipbop/bipbopall.m3u8>.
self.playerItem = [AVPlayerItem playerItemWithURL:url];
//(optional) [playerItem addObserver:self forKeyPath:#"status" options:0 context:&ItemStatusContext];
self.player = [AVPlayer playerWithPlayerItem:playerItem];
self.player = [AVPlayer playerWithURL:<#Live stream URL#>];
//(optional) [player addObserver:self forKeyPath:#"status" options:0 context:&PlayerStatusContext];
Use the following code to play the music:
[self.player play];
Try the following:
-(void)viewDidLoad
{
[super viewDidLoad];
NSString* resourcePath = #"your url"; //your url
NSData *_objectData = [NSData dataWithContentsOfURL:[NSURL URLWithString:resourcePath]];
NSError *error;
audioPlayer = [[AVAudioPlayer alloc] initWithData:_objectData error:&error];
audioPlayer.numberOfLoops = 1;
if (audioPlayer == nil)
NSLog([error description]);
else
[audioPlayer play];
Try my code:
NSURL *filepath = [NSURL fileURLWithPath:audioFilePath];
[yourMediaPlayer setContentURL:filepath];
//set player.
yourMediaPlayer.controlStyle = MPMovieControlStyleNone;
yourMediaPlayer.currentPlaybackTime = 0;
yourMediaPlayer.shouldAutoplay = FALSE;
[yourMediaPlayer play];
^-^,I use MPMoviePlayerController with above code.
Current Answer:
Now consider Why does AVAudioPlayer initWithContentsOfURL always fail with error code=-43 link. its solution is avplayer link
Earlier Answer
//You have to pass NSURL not NSData
NSString* resourcePath = #"http://192.167.1.104:8888/SHREYA.mp3"; //your url
NSURL *url = [NSURL alloc]initWithString:resourcePath];
audioPlayer = [[AVAudioPlayer alloc] initWithURL:url error:&error];
audioPlayer.delegate = self;
[url release];
[audioPlayer prepareToPlay];
[audioPlayer play];
I guess you are sending
audioPlayer.numberOfLoops = -1;
to receive NSInvalidArgumentException change it to
audioPlayer.numberOfLoops = 1;
and check apple's documentation here

Sound issue in device

NSString *musicPath = [[NSBundle mainBundle] pathForResource:#"ding" ofType:#"mp3"];
if (musicPath)
{
if(TapSoud)
{
[TapSoud release];
TapSoud = nil;
}
TapSoud = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:musicPath] error:nil];
}
[TapSoud setDelegate:self];
[TapSoud prepareToPlay];
[TapSoud play];
NSString *musicPath1 = [[NSBundle mainBundle] pathForResource:#"roadrunner" ofType:#"mp3"];
if (musicPath1)
{
if(MatchSoud)
{
[MatchSoud release];
MatchSoud = nil;
}
MatchSoud = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:musicPath1] error:nil];
}
[MatchSoud setDelegate:self];
[MatchSoud prepareToPlay];
NSString *musicPath2 = [[NSBundle mainBundle] pathForResource:#"Wrong Answer" ofType:#"wav"];
if (musicPath2)
{
if(WrongMatchSoud)
{
[WrongMatchSoud release];
WrongMatchSoud = nil;
}
WrongMatchSoud = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:musicPath2] error:nil];
}
[WrongMatchSoud setDelegate:self];
[WrongMatchSoud prepareToPlay];
This code works properly in the simulator, but generates no sound on the device. What is the problem here?
The most likely problem is that the audio files are missing either in the project or at resource copying stage. The simulator stores previously copied resources even if removed from the project until the application is removed completely from the simulator.
Make sure musicPath, musicPath1 and musicPath2 are not empty and valid.

How do I cycle through several sounds using one action?

Right now I have a single sound playing from one action, button press, accelerometer, etc..
I would like to know how I can cycle through several sounds (I'm using 3 sounds for my project) from a single action that the user initiates.
I am currently using the code shown below and it serves it's purpose for playing a single sound for each user action. I have not used NSArray in a project before, so if your including it please include any details.
NSURL *url = [NSURL fileURLWithPath: [NSString stringWithFormat:#"%#/Jet.wav", [[NSBundle mainBundle] resourcePath]]];
NSError *error;
audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:&error];
audioPlayer.numberOfLoops = 0;
if (audioPlayer == nil)
NSLog(#"%#", [error description]);
else
[audioPlayer play];
If you are only using 3 sounds then you can get away with just using a C array of NSString's but if you need a dynamic amount of sounds then you should change to using an NSArray
// .m
NSString *MySounds[3] = {
#"sound1.wav",
#"sound2.wav",
#"sound3.wav",
};
#implementation ...
Then in your method you need to add a little extra logic
- (void)playSound;
{
NSString *path = [NSString stringWithFormat:#"%#/%#", [[NSBundle mainBundle] resourcePath], [self nextSoundName]];
NSURL *url = [NSURL fileURLWithPath:path];
NSError *error;
audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:&error];
audioPlayer.numberOfLoops = 0;
if (audioPlayer == nil) {
NSLog(#"%#", [error description]);
} else {
[audioPlayer play];
}
}
- (NSString *)nextSoundName;
{
static NSInteger currentIndex = -1;
if (++currentIndex > 2) {
currentIndex = 0;
}
return MySounds[currentIndex];
}
-(IBAction)playSound:(UIButton *)sender{
NSURL *url = [NSURL fileURLWithPath: [NSString stringWithFormat:#"%#/%#", [[NSBundle mainBundle] resourcePath], [MySounds objectAtIndex:[sender tag]]]];
NSError *error;
audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:&error];
audioPlayer.numberOfLoops = 0;
if (audioPlayer == nil)
NSLog(#"%#", [error description]);
else
[audioPlayer play];
}
Here you can use single action buttons for all buttons. Just you need to do is set the tags to button. Use array as described by Paul