using slider change the volume in AVAudioPlayer in iphone sdk - iphone

I am using this code
NSURL *url = [NSURL fileURLWithPath:[NSString stringWithFormat:#"%#/audiofile.mp3", [[NSBundle mainBundle] resourcePath]]];
NSError *error;
player = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:&error];
player.numberOfLoops = -1;
if (player == nil)
NSLog(#"%#",[error description]);
else
[player play];
but i want to change the volume so how can i get the volume detail?

Set up your UISlider. Register it to receive the ValueChanged events in IB or programmatically. Set its min value to 0 and max to 100. And that should be it.
- (void)sliderValueChanged:(UISlider *)slider {
myPlayer.volume = slider.value / 100.0;
}

Related

How to do unlimited repetition by using AVAudioPlayer in ios

In my app i am using audio. I want to play audio for unlimited time on tapping button. Is it possible?
AVAudioPlayer has a numberOfLoops method. Set that to -1 for unlimited repetition.
Easiest way is to re-play the player when it reaches the end :
-(void)startPlayer {
NSURL *url = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:#"sound" ofType:#"mp3"]];
NSError *error;
player = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:&error];
player.delegate = self;
if (error) {
NSLog(#"Error in audioPlayer: %#", [error localizedDescription]);
} else {
[aPlayer play];
}
}
- (void)audioPlayerDidFinishPlaying:(AVAudioPlayer *)aPlayer successfully:(BOOL)flag {
aPlayer.currentTime = 0;
[aPlayer play];
}
Call [self startPlayer]; once, and it will loop forever !

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];

Can I stream a .mp3-link with the AV Foundation Framework on iOS?

I'm making an app for someone who has his own radio.
It's only a stream URL (ex. http://stream.domain.com/highquality.mp3)
I've made yet the code with the AV Foundation Framework to let it work with a simple .mp3-file.
This is my code:
-(IBAction)play {
if(clicked == 0) {
clicked = 1;
NSURL *url = [NSURL fileURLWithPath:[NSString stringWithFormat:#"%#/test.mp3", [[NSBundle mainBundle] resourcePath]]];
NSError *error;
audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:&error];
audioPlayer.numberOfLoops = 0;
[audioPlayer play];
[start setTitle:#"Stop" forState:UIControlStateNormal];
}
else {
[audioPlayer stop];
clicked = 0;
[start setTitle:#"Start" forState:UIControlStateNormal];
}
}
What should I edit?
Edit: The code above really works. I need to let it work with a button to play and stop it. I don't want to have a hole media player on my device. Simply and beautiful..
Edit 2: Some people don't find what I need: I need the edits to let it work with the .mp3 stream (as example a stream above).
You just have to pass a NSURL to your player object to make it play the stream from there
-(IBAction)play {
if(clicked == 0) {
clicked = 1;
NSURL *url = [NSURL URLWithString:#"http://www.linktoyourstream.com"];
NSError *error;
audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:&error];
audioPlayer.numberOfLoops = 0;
[audioPlayer play];
[start setTitle:#"Stop" forState:UIControlStateNormal];
}

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

iPhone SDK - Mute

I have the following to play m background music:
NSURL *url = [NSURL fileURLWithPath:[NSString stringWithFormat:#"%#/bgMusic.mp3", [[NSBundle mainBundle] resourcePath]]];
NSError *error;
bgMusic = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:&error];
bgMusic.numberOfLoops = -1;
bgMusic.volume = 0.1;
if (bgMusic == nil)
NSLog([error description]);
else
[bgMusic play];
But how I can I in any view, mute all sounds, not just this, any sound?
Thanks.
[[MPMusicPlayerController applicationMusicPlayer] setVolume:(use a value between 0.0 and 1.0)]
If your MPMoviePlayerController uses the application audio session, this works.