iPhone SDK - Mute - iphone

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.

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

AVAudioPlayer is not working in iPhone 5

I am developing an iPhone application.In this app i am using AVAudioPlayer for playing audio.
This is my code for playing the sound.
NSString* resourcePath = [[NSBundle mainBundle] resourcePath];
resourcePath = [resourcePath stringByAppendingString:#"/sound.mp3"];
NSError* err;
player_ = [[AVAudioPlayer alloc] initWithContentsOfURL:
[NSURL fileURLWithPath:resourcePath] error:&err];
if( err )
{
}
else
{
[player_ prepareToPlay];
[player_ play];
int playingLoops = 0;
player_.numberOfLoops = playingLoops;
}
It is woking fine on iphone devices except iPhone5.While i run the app on iPhone 5 i get following error then app crashes.
2012-11-26 09:02:28.484 test[728:1dd03] <0xb0081000> Error '!obj' trying to fetch default input device's sample rate
2012-11-26 09:02:28.484 test[728:1dd03] <0xb0081000> Error getting audio input device sample rate: '!obj'
2012-11-26 09:02:28.494 test[728:1dd03] <0xb0081000> AQMEIOManager::FindIOUnit: error '!dev'
I was also facing same problem giving the same error. when try to solve this problem i notice that there is no problem in code. we need to update media player or install flash player and it's work fine.
You have to add AVAudioPlayerDelegate to your .h file and following code to your .m file
NSString* resourcePath =[[NSBundle mainBundle]pathForResource:#"sound" ofType:#"mp3"];
NSError* err;
AVAudioPlayer *audioplayer =[[AVAudioPlayer alloc]initWithContentsOfURL:[NSURL fileURLWithPath: resourcePath] error:&err];
audioplayer.delegate=self;
if( err )
{
NSLog(#"%#",err);
}
else
{
[audioplayer prepareToPlay];
[audioplayer play];
}
This code is working for me and accept the answer if it is working.
Anbu, try below code.At my end it's working fine.
NSURL *url = [NSURL fileURLWithPath:[NSString stringWithFormat:#"%#/audiofile.mp3", [[NSBundle mainBundle] resourcePath]]];
NSLog(#"File path is:->%#",[NSString stringWithFormat:#"%#/audiofile.mp3", [[NSBundle mainBundle] resourcePath]]);
NSFileManager* manager;
manager = [NSFileManager defaultManager];
if([manager fileExistsAtPath:[NSString stringWithFormat:#"%#/audiofile.mp3", [[NSBundle mainBundle] resourcePath]]])
{
printf("file Exists...\n\n");
}
else
printf("File not exist...\n\n");
NSError *er;
audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:&error];
audioPlayer.numberOfLoops = -1;
if (audioPlayer == nil)
NSLog([er description]);
else
[audioPlayer play];
In any concern, let me know. And, also please check your console for the full path and for file exist or not.

using slider change the volume in AVAudioPlayer in iphone sdk

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

Can MPMusicPlayerController can play music from local resource?

i wanna play music with MPMusicPlayerController.
MPMediaItem * mediaItem = [];
MPMediaItemCollection *songs;
NSArray * array = [NSArray arrayWithObjects:mediaItem, nil];
songs = [MPMediaItemCollection collectionWithItems:array];
[[MPMusicPlayerController iPodMusicPlayer] setQueueWithItemCollection:songs];
i don't know how to give mediaItem, and i have a mp3 file.
Help me. thank you!
No, the MPMusicPlayerController will only play music from the Media Library (That's why its located in the MP/MediaPlayer framework) You'll need to use the AVAudioPlayer or the AVPlayer class. A bit more work implementing that unfortunately.
Something along the lines of this should get you started:
NSURL *url = [NSURL fileURLWithPath:[NSString stringWithFormat:#"%#/audiofile.mp3", [[NSBundle mainBundle] resourcePath]]];
NSError *error;
audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:&error];
audioPlayer.numberOfLoops = -1;
if (audioPlayer == nil)
NSLog([error description]);
else
[audioPlayer play];

I can not play mp3 file using AVAudioPlayer

I want to play mp3 file placed in resources as code is given below,
NSString *st=#"alarm_1.mp3";
NSLog(st);
NSURL* musicFile = [NSURL fileURLWithPath:st];
AVAudioPlayer *click = [[AVAudioPlayer alloc] initWithContentsOfURL:musicFile error:nil];
//AVAudioPlayer *click1=[[AVAudioPlayer alloc]initWithContentsOfURL:[NSURL URLWithString:#"alarm_1.mp3"]];
[click prepareToPlay];
[click play];
[click release];
but unfortunately i cant hear the sound
please help
I don't think your file name is right. Try this syntax:
NSURL *clickURL = [NSURL fileURLWithPath:[NSString stringWithFormat:#"%#/alarm_1.mp3", [[NSBundle mainBundle] resourcePath]]];
AVAudioPlayer *audioPlayer = [[AVAudioPlayer alloc]initWithContentsOfURL:clickURL error:nil];
I had a similar problem due to ARC. Instead of defining the AVAudioPlayer in the same method you are using it, you should should have an instance variable somewhere else, such as the UIViewController. This way ARC doesn't auto release this object and audio can play.
NSURL *url = [NSURL fileURLWithPath:[NSString stringWithFormat:#"%#/alarm_1.mp3", [[NSBundle mainBundle] resourcePath]]];
NSError *error;
AVAudioPlayer *audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:&error];
audioPlayer.numberOfLoops = -1;
[audioPlayer play];
This is answer