AVAudioPlayer volume controle on different songs - ios

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.

Related

audio is not playing on button click

I want to play a audio on Button click. I have tried this code but audio is not playing:-
NSString *soundFilePath = [[NSBundle mainBundle] pathForResource:#"sound" ofType:#"mp3"];
NSURL *soundFileURL = [NSURL fileURLWithPath:soundFilePath];
AVAudioPlayer *audioPlayer = [[AVAudioPlayer alloc] initWithData:soundFileURL error:nil];
audioPlayer.delegate = self;
audioPlayer.volume=1.0;
[audioPlayer setNumberOfLoops:1];
[audioPlayer play];
you haven't created a player item
//declare in .h file
AVPlayerItem *playerItem;
//then in .m file
player = [AVPlayer playerWithPlayerItem:playerItem];
after that play it
[player play];
First, it appears you are using initWithData to access an audio file pointed to by a URL.
You should instead use initWithContentsOfU‌RL.
Another issue could be due to a common error in implementing the audio player.
If the AVAudioPlayer you are creating is inside a function, it will be released, or deallocated, at the end of the function. This is why the audio does not play.
To fix this problem, make the AVAudioPlayer a property or instance variable in your class so that it does not get immediately deallocated.
For example, a property is declared in your header as
#property(strong, nonatomic) AVAudioPlayer *audioPlayer;
and initialized in your implementation with
self.audioPlayer = [[AVAudioPlayer alloc] initWithData:soundFileURL error:nil];
If this does not solve your problem, then the issue is likely to do with the file pointed to by your URL.
NSString *soundFilePath = [NSString stringWithFormat:#"%#/test.mp3",
[[NSBundle mainBundle] resourcePath]];
NSURL *soundFileURL = [NSURL fileURLWithPath:soundFilePath];
AVAudioPlayer *player = [[AVAudioPlayer alloc] initWithContentsOfURL:soundFileURL error:nil];
player.numberOfLoops = -1; //Infinite
[player play];
print "soundFileURL" . check whether it is null(in case soundFileURL contains spaces it results/prints null ) and if it not a null value check the file name is spelled exactly correct or not(i.e., here your file name is "sound")
var asyncaAudioPlayer: AVAudioPlayer!
let audioFilePath = NSBundle.mainBundle().pathForResource(fileName, ofType: "mp3")
let audioFileUrl = NSURL.fileURLWithPath(audioFilePath!)
asyncaAudioPlayer = AVAudioPlayer(contentsOfURL: audioFileUrl, fileTypeHint: nil)
audioPlayerArray.append(asyncaAudioPlayer)
asyncaAudioPlayer.prepareToPlay()
asyncaAudioPlayer.play()
Your problem is you set NSURL for initWithData -> Wrong.
in case it sound play multi time, you should prepare your sound Data and keep it as strong ref.
and you should use CGD and global queue instead of main queue.
-(void)onTouchUpInside{
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_LOW, 0), ^{
AVAudioPlayer *audioPlayer = [[AVAudioPlayer alloc] initWithData:self.soundFileData error:nil];
audioPlayer.delegate = self;
audioPlayer.volume=1.0;
[audioPlayer setNumberOfLoops:1];
if ([audioPlayer prepareToPlay] && [audioPlayer play])
{
NSLog(#"play Success!");
}
else
{
NSLog(#"Failed to play the audio file.");
}
});
}

Cannot play audio in Xcode project

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.

stop first sound when second is playing

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);
}

AVAudioPlayer not playing sound on some devices

I am using AVAudioPlayer to play an audio file its working but on some device it does not play how do i solve my problem
if(_defaultSettings)
{
_soundFilePath = [[NSBundle mainBundle] pathForResource:#"ring" ofType:#"wav"];
NSLog(#"ringTone Path%#",_soundFilePath);
// /System/Library/Audio/UISounds/
_audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:_soundFilePath ]error:nil];
[_audioPlayer prepareToPlay];
[_audioPlayer play];
_audioPlayer.numberOfLoops = -1;
AudioServicesPlaySystemSound(1000);
}
- (void)playSystemSound {
NSLog(#"playSystemSound");
// Create the URL for the source audio file. The URLForResource:withExtension: method is
// new in iOS 4.0.
NSURL *tapSound = [[NSBundle mainBundle] URLForResource: #"short_low_high"
withExtension: #"wav"];
if (!self._soundFileURLRef) {
// Store the URL as a CFURLRef instance
self._soundFileURLRef = (CFURLRef) [tapSound retain];
// Create a system sound object representing the sound file.
AudioServicesCreateSystemSoundID (_soundFileURLRef, &_soundFileObject);
}
// Play the sound
AudioServicesPlaySystemSound (_soundFileObject);
// // to vibrate the device // AudioServicesPlaySystemSound (kSystemSoundID_Vibrate);
}

Pick songs from plist(NSArray) and play using AVAudioPlayer?

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.

Resources