I am an absolute beginner in both objective-c and other environments so please be patient. Here is my problem: I need to play sound files so that each of them stops when the other is started, right now they overlap; here is a snippet of my code.
Thank you in advance and let me know if you need other details.
- (IBAction)soundfirst {
NSString *path =
[[NSBundle mainBundle] pathForResource:#"hi2" ofType:#"mp3"];
player1 = [[AVAudioPlayer alloc]
initWithContentsOfURL:[NSURL fileURLWithPath:path]
error:NULL];
player1.delegate = self;
[player1 play];
}
- (IBAction)soundsecond {
[player1 stop];
NSString *path =
[[NSBundle mainBundle] pathForResource:#"hi2" ofType:#"mp3"];
player2 = [[AVAudioPlayer alloc]
initWithContentsOfURL:[NSURL fileURLWithPath:path]
error:NULL];
player2.delegate = self;
[player2 play];
}
The best way to do something when you're stuck, is to look into the documentation.
In your code, you're creating an object of type AVAudioPlayer, and naming it player1.
AVAudioPlayer Documentation:
http://developer.apple.com/library/ios/#DOCUMENTATION/AVFoundation/Reference/AVAudioPlayerClassReference/Reference/Reference.html
look under the instance methods, and there's a method there called stop.
So in the first method, you can do something like
if(player2.playing){
[player2 stop];
}
[player1 play];
#import <AudioToolbox/AudioServices.h>
SystemSoundID buttonClickSoundID;
+ (void)playButtonClickSound {
if (buttonClickSoundID == 0) {
NSString *soundPath = [[NSBundle mainBundle] pathForResource:#"button_click" ofType:#"wav"];
CFURLRef soundURL = (CFURLRef)[NSURL fileURLWithPath:soundPath];
AudioServicesCreateSystemSoundID(soundURL, &buttonClickSoundID);
}
AudioServicesPlaySystemSound(buttonClickSoundID);
}
Related
I have read some threads about this topic, and currently have this:
- (void)viewDidLoad
{
[super viewDidLoad];
NSString *soundFilePath = [NSString stringWithFormat:#"%#/testsoundsr.aiff",
[[NSBundle mainBundle] resourcePath]];
NSURL *soundFileURL = [NSURL fileURLWithPath: soundFilePath];
AVAudioPlayer *player = [[AVAudioPlayer alloc] initWithContentsOfURL:soundFileURL
error:nil];
player.numberOfLoops = -1; //Infinite
[player play];
}
The sound is not played though, and I can only imagine that the problem is with the NSString definition which is used to reference the file
The file I'm trying to play is named 'testsoundsr.aiff' and I have dragged that into the project. Also I have added several frameworks to the project, including AudioToolBox.framework
---UPDATE---
I have now tried using an .mp3 file instead, but still getting no sound through.
Try player.delegate = self before [player play]
This code works in a test app, but not in the app in which I want the audio to play.
-(void) playTutorialAudio
{
_theAudioFile = [[NSBundle mainBundle] pathForResource:#"Welcome" ofType:#"mp3"];
NSLog(#"playTutorialAudio _theAudioFile %#",_theAudioFile);
NSURL *playTutorialURL = [NSURL fileURLWithPath:_theAudioFile ];
_audioPlayAlert = [[AVAudioPlayer alloc] initWithContentsOfURL:playTutorialURL error:NULL];
[_audioPlayAlert setDelegate:self];
[_audioPlayAlert prepareToPlay];
_audioPlayAlert.volume = 1.0;
[_audioPlayAlert play];
}
The code works with other audio files.
It will work, You need to change your implementation like this. Please notice your init method.
#property (nonatomic, retain) AVAudioPlayer *cellTapSound;
may be in viewDidLoad
cellTapSound = [[AVAudioPlayer alloc] initWithContentsOfURL:[[NSBundle mainBundle] URLForResource:#"ting" withExtension:#"wav"] error:nil];
[cellTapSound prepareToPlay];
Now may be on click of some button, you can give a shot like this :
if (cellTapSound.isPlaying) [cellTapSound setCurrentTime:0.0];
[cellTapSound play];
hope that helps.
I have would like to play audio on the startup of my app, but, first of all, the audio doesn't play! No matter how i code it. And secondly I would love to loop it if i can.
#implementation ViewController
-(void)awakeFromNib
{
SystemSoundID soundID;
NSString *soundFile1 = [[NSBundle mainBundle] pathForResource:#"ScaryMusic" ofType:#"mp3"];
AudioServicesCreateSystemSoundID((__bridge CFURLRef) [NSURL fileURLWithPath:soundFile1], &soundID);
AudioServicesPlaySystemSound(soundID);
NSLog(#"Sound Played");
}
Try this instead:
#import <AVFoundation/AVFoundation.h>
AVAudioPlayer *player;
NSURL *soundFileURL = [[NSBundle mainBundle] URLForResource:#"ScaryMusic" withExtension:#"mp3"];
if (soundFileURL != nil)
{
player = [[AVAudioPlayer alloc] initWithContentsOfURL:soundFileURL error:nil];
player.numberOfLoops = -1; //infinite
}
[player play];
This is better for mp3's and such. SystemSound is mostly reserved for short .wav type sounds.
If you look in the docs you'll find that mp3 is not among the formats supported by AudioToolbox
I am using AVAudioplayer to play 3 differents songs by pressing a button, using this following code:
-(IBAction)play
{
//Play sound1
NSString *pathToMusicFile1 = [[NSBundle mainBundle] pathForResource:#"Sample1" ofType:#"mp3"];
AVAudioPlayer* mySong1 = [[AVAudioPlayer alloc]initWithContentsOfURL:[NSURL fileURLWithPath:pathToMusicFile1] error:NULL];
NSLog(#"Song1 Loaded");
mySong1.numberOfLoops = 0;
mySong1.volume = 1.0;
[mySong1 play];
//Play sound2
NSString *pathToMusicFile2 = [[NSBundle mainBundle] pathForResource:#"Sample2" ofType:#"mp3"];
AVAudioPlayer* mySong2 = [[AVAudioPlayer alloc]initWithContentsOfURL:[NSURL fileURLWithPath:pathToMusicFile2] error:NULL];
NSLog(#"song2 Loaded");
mySong2.numberOfLoops = 0;
mySong2.volume = 1.0;
[mySong2 play];
//Play sound3
NSString *pathToMusicFile3 = [[NSBundle mainBundle] pathForResource:#"Sample3" ofType:#"mp3"];
AVAudioPlayer* mySong3 = [[AVAudioPlayer alloc]initWithContentsOfURL:[NSURL fileURLWithPath:pathToMusicFile3] error:NULL];
NSLog(#"song3 Loaded");
mySong3.numberOfLoops = 0;
mySong3.volume = 1.0;
[mySong3 play];
}
That work fine but now i would like to use 3 more buttons to mute off the sound1, sound2 and sound 3.
I tried that :
-(IBAction)sound1
{
mySong1.volume = 0.0;
}
-(IBAction)sound2
{
mySong2.volume = 0.0;
}
-(IBAction)sound3
{
mySong3.volume = 0.0;
}
But I am getting errors "mySong1 undeclared" ! How can I manage to declare and have the possibility to mute OFF one of the sound by pressing button ?
"mySong1 undeclared" is being presented because you are trying to access local pointers from one function in another function. Basically, delete your pointers (AVAudioPlayer *) in the .m and just leave mySong3 (or 2 or 1) = //blah blah. Then in the .h declare mysong1; 2; and 3; with pointers included.
I'm student working on a coding exercise and I am stumped!
I have two instances of AVAudioPlayer in my app - I can load the songs into each player using a direct path no problem.
What I'd like to be able to do is play multiple songs held within an array on each player.
Is this possible? and if so how would I go about doing this?
In my plist I have the key set to "One.mp3" as a string and the value as the path to the file... (was guessing at that part).
Thanks for any insight.
- (void)viewDidLoad {
[super viewDidLoad];
//multiple song array
NSString *soundsPath = [[NSBundle mainBundle] pathForResource:#"soundslist"
ofType:#"plist"];
soundsList = [[NSArray alloc] initWithContentsOfFile:soundsPath];
NSString* filename = [soundsList objectAtIndex:0];
NSString *path = [[NSBundle mainBundle] pathForResource:filename
ofType:#"mp3"];
AVAudioPlayer * newAudio=[[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path]
error:NULL];
self.audioPlayer = newAudio; // automatically retain audio and dealloc old file if new file is loaded
[newAudio release]; // release the audio safely
audioPlayer.delegate = self;
[audioPlayer prepareToPlay];
[audioPlayer setNumberOfLoops:0];
[audioPlayer play];
}
It sounds like you want to try to play multiple songs on a single player. This isn't possible as stated in the AVAudioPlayer Overview (4th bullet). It is possible to play multiple songs with multiple AVAudioPlayers. Something like this:
NSMutableArray *audioPlayers = [NSMutableArray arrayWithCapacity:[soundsList count]];
for (NSString *soundPath in soundsList) {
NSError *error = nil;
AVAudioPlayer *audioPlayer = [AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:&error];
[audioPlayers addObject:audioPlayer];
audioPlayer.delegate = self;
[audioPlayer prepareToPlay];
[audioPlayer setNumberOfLoops:0];
[audioPlayer play];
}
I would suggest that you hardcode the file path instead of reading it in with a plist. Once you get that working, then you can try reading it in with the plist. Something like this would work:
NSURL *soundURL = [[NSBundle mainBundle] URLForResource:#"One" withExtension:#"mp3"];
AVAudioPlayer *soundPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:soundURL error:&error]
This way, you remove one level of indirection and one point of failure.