In my project i have a function that play music
-(void)playPlin {
AudioSessionSetActive(true);
// Set up audio session, to prevent iPhone from deep sleeping, while playing sounds
UInt32 category = kAudioSessionCategory_MediaPlayback;
AudioSessionSetProperty (
kAudioSessionProperty_AudioCategory,
sizeof (category),
&category
);
//UInt32 category = kAudioSessionCategory_MediaPlayback;
OSStatus result = AudioSessionSetProperty(kAudioSessionProperty_AudioCategory,
sizeof(category), &category);
if (result){
NSLog(#"ERROR SETTING AUDIO CATEGORY!\n");
}
result = AudioSessionSetActive(true);
if (result) {
NSLog(#"ERROR SETTING AUDIO SESSION ACTIVE!\n");
}
NSString *soundFilePath = [[NSBundle mainBundle] pathForResource:#"chimes" ofType:#"wav"];
NSData *sampleData = [[NSData alloc] initWithContentsOfFile:soundFilePath];
NSError *audioError = nil;
AVAudioPlayer *alarmAudioPlayer = [[AVAudioPlayer alloc] initWithData:sampleData error:&audioError];
[sampleData release];
[[AVAudioSession sharedInstance] setCategory: AVAudioSessionCategoryPlayback error: nil];
if(audioError != nil) {
NSLog(#"An audio error occurred: \"%#\"", audioError);
}
else {
[alarmAudioPlayer play];
} }
The file play ok but i have three issue:
1.how can I make that the audio file starts not from the beginning but from the number of second that i pass? for example, if i step value 300 audio file should start from the second 300 of it
2.How can i set volume of audio file?
3.How can i set loop property of it?
thank in advance
This is all pretty clearly laid out in the documentation.
1) Use the playAtTime: method call instead of play. Pass the time in seconds.
2) Set the volume property on the AVAudioPlayer. Note that this is the gain of the player, and will be modulated with the current system volume.
3) Set the numberOfLoops property. Assign a huge value like NSIntegerMax to keep looping indefinitely.
Related
How can I play more than one .m4a file consecutively?
Presently, my function starts playing one, using AudioServicesPlaySystemSound etc., uses AudioServicesAddSystemSoundCompletion to select a callback, and then returns. When the sound is done, the callback lets my software continue.
This is a LOT of coding whenever there is back to back sound.
Can my software start a .m4a playing, and SOMEHOW wait for it to complete, waiting waiting for its callback to set a flag? Can I use pthread software to do a wait loop with a multitasking pause?
Here's my code so far...
// Restore game_state to continue game after playing sound file.
int game_state_after_sound;
void play_sound_file_done (
SystemSoundID ssID,
void calling_class
)
{
printf("\n play_sound_file_done.");
// Sound is now done, so restore game_state and continue to manage_game from where left off:
game_state = game_state_after_sound;
[ (GeController)calling_class manage_game];
}
// Plays sound_file_m4a and returns after playing has completed.
-(void) play_sound_file: (NSString *) sound_file_m4a
{
printf("\n play_sound_file '%s' ", [sound_file_m4a UTF8String] );
NSString *soundPath = [[NSBundle mainBundle] pathForResource:sound_file_m4a ofType:#"m4a"];
SystemSoundID soundID;
AudioServicesCreateSystemSoundID((CFURLRef)[NSURL fileURLWithPath: soundPath], &soundID);
AudioServicesPlaySystemSound (soundID);
AudioServicesAddSystemSoundCompletion ( soundID, NULL, NULL, play_sound_file_done, (void*)self );
game_state_after_sound = game_state;
game_state = DELAY_WHILE_PLAYING_SOUND;
}
********************************** AVPlayer work-in-progress...
But this produces no sound.
#import <AVFoundation/AVFfoundation.h>
-(void) play_queued_sounds: (NSString *) sound_file_m4a
{
printf("\n queue_sound_file: '%s' ", [sound_file_m4a UTF8String] );
AVPlayerItem *sound_file = [[AVPlayerItem alloc] initWithURL:[NSURL URLWithString:[[NSBundle mainBundle] pathForResource:sound_file_m4a ofType:#"m4a"]]];
AVQueuePlayer *player = [AVQueuePlayer queuePlayerWithItems:[NSArray arrayWithObjects:sound_file, nil]];
[player play];
return;
}
This can be achieved quite easily in AVQueuePlayer. You don't have to handle completion or anything, you just give it the AVPlayerItems that you want to play in sequence and call play. Here's an example:
#import <AVFoundation/AVFfoundation.h>
//AVPlayerItem *item1 = [[AVPlayerItem alloc] initWithURL:[NSURL URLWithString:[[NSBundle mainBundle] pathForResource:#"myFileName" ofType:#"m4a"]]];
EDIT: Use this instead
AVPlayerItem *item1 = [[AVPlayerItem alloc] initWithURL:[NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:[[NSString alloc] initWithFormat:#"2"] ofType:#"mp3"]]];
AVQueuePlayer *player = [AVQueuePlayer queuePlayerWithItems:[NSArray arrayWithObjects:item1, nil]];
[player play];
Just as an addition, this can also be achieved with items from the users iPod library using a MPMusicPlayerController queued from the didPickMediaItems delegate method of MPMediaPickerController
- (void)mediaPicker:(MPMediaPickerController *)mediaPicker didPickMediaItems:(MPMediaItemCollection *)mediaItemCollection
{
MPMusicPlayerController *player = [MPMusicPlayerController applicationMusicPlayer];
[player setQueueWithItemCollection:mediaItemCollection];
[player play];
}
I could not get the AVQueuePlayer solution to work.
GREAT SOLUTION:
For each sound to play, spin up a thread that awaits then locks a mutex, then does AudioServicesPlaySystemSound. Just spin up a thread for each successive sound to play, and use the pthread mutex to delay each successive thread until the AudioServicesAddSystemSoundCompletion function unlocks the mutex. Works like a champ.
This is 21st century embedded programming: Throw threads at the problem!
I am developing a ebook reader. There are some audio files which needs to be played upon clicking on a icon. The audio just plays fine for the first time. But when I press the home button of the app and return into the audio player, the audio is being played twice. The number keeps increasing by 1 every time I press the home button and come back to the audio player. Using debugger I found that this loop is running multiple times. Can anyone explain why is it behaving like that?
-(void)StartPlayingFileWithNotification:(NSNotification *)notifcation{
// Load the array with the sample file
NSString *f = [[notifcation userInfo] valueForKey:#"audiopath"];
NSURL *fileURL = [[NSURL alloc] initFileURLWithPath: f];
if(fileURL == nil){
GLogError(#"%#",#"File Not Found");
return;
}else {
GLogInfo(#"%#",#"Got audio file");
}
if (self.player != nil) {
if (self.player.playing) {
[self.player stop];
}
[player release];
player = nil;
}
self.player = [[AVAudioPlayer alloc] initWithContentsOfURL:fileURL error:nil];
if (self.player)
{
fileName.text = [NSString stringWithFormat: #"%# (%d ch.)", [[player.url relativePath] lastPathComponent], player.numberOfChannels, nil];
NSLog(#"The audio file is %#",fileName.text);
NSLog(#"The file URL is %#",[fileURL description]);
[self updateViewForPlayerInfo:player];
player.numberOfLoops = 0;
player.delegate = self;
[player play];
[self updateViewForPlayerState:player];
}
[fileURL release];
}
Check your #synthesize, member var and #property declarations. Don't you have a typing mistake ? Use self.player everywhere, or just player, but try avoiding mixing both.
I am trying to play an in app audio with ipod the code I am using to play the audio file is:
Code:
[[AVAudioSession sharedInstance] setCategory: AVAudioSessionCategoryPlayback error: nil];
UInt32 doSetProperty = 1;
AudioSessionSetProperty (kAudioSessionProperty_OverrideCategoryMixWithOthers, sizeof(doSetProperty), &doSetProperty);
[[AVAudioSession sharedInstance] setActive: YES error: nil];
if(error)
{
NSLog(#"Some error happened");
}
NSString *path = [[NSBundle mainBundle] pathForResource:effect ofType:type];
myPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL];
myPlayer.delegate = self;
myPlayer.numberOfLoops = -2;
myPlayer.volume = 1.0f;
[myPlayer play];
and to play the ipod music I am using
Code:
player = [MPMusicPlayerController iPodMusicPlayer];
My ipod plays fine until the audio starts playing but once the audio stops I am not able to get back to the ipod to play. I am using
Code:
NSString *value = [[NSUserDefaults standardUserDefaults] stringForKey:#"sound"];
if([value compare:#"ON"] == NSOrderedSame && myPlayer != nil)
{
[myPlayer stop];
[myPlayer release];
myPlayer = nil;
}
NSError *error = nil;
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryAmbient error:&error];
if(error)
{
NSLog(#"Some error happened");
}
to stop the audio and then just call
Code:
[player play];
to play the ipod music but it does not play the music, it says the title to be null.
I would really appreciate if some one could help me with this.
Regards,
Ankur
I suggest moving the audio session setup (including the Ambient category) to your application delegate's didFinishLaunchingWithOptions and not using the Playback category at all. You can still play AVAudio sounds just fine with the Ambient setting.
If you want to kill iPod music whenever you play AV sounds, then you should only be using the Playback category. (I'm not exactly sure what you're trying to do.)
The error that you're getting when you try to resume the iPod music suggests that your MPMusicPlayerController object resets its song queue when it is stopped by the AVAudio sounds, and that you'll have to set up the MP player again. "Title is null" definitely sounds like a dangling pointer or the like in the MP player.
It's not possible to play iPod audio in AVAudioPlayer. You need to convert the library Asset link from ipod, using *AVURLAsset to an mp3 or wav and save it to the documents library. IOS.5 currently has a bug in the conversion (AudioExportSession) from ipod to mp3 file, so it's NOT POSSIBLE. Sorry.
You need to resume iPod player after myPlayer finish playing.
-(void) audioPlayerDidFinishPlaying:(AVAudioPlayer *)player successfully:(BOOL)flag {
[player play];
}
as topic... is it possible ?
Thanks
again, I have attached the code as follows, please check which step is wrong .thanks.
//#step
AudioSessionInitialize (NULL, NULL, NULL, NULL);
AudioSessionSetActive(true);
UInt32 sessionCategory = kAudioSessionCategory_MediaPlayback;
OSStatus error = AudioSessionSetProperty (kAudioSessionProperty_AudioCategory, sizeof(sessionCategory),&sessionCategory);
if (error)
printf("ERROR AudioSessionSetProperty ! %d\n", error);
//#step
NSString* filePath = #"AlarmClockBell.caf";
[Util restoreResourceFile:filePath];
filePath =[Util getFileFullPathFromSysDoc:filePath];
NSURL *soundFileURL = [NSURL fileURLWithPath:filePath];
NSError* error ;
AVAudioPlayer * audioPalyer = [[AVAudioPlayer alloc] initWithContentsOfURL: soundFileURL error: &error];
if (nil == audioPalyer)
{
AppTrace3(self, #"Faild to play", soundFileURL, error);
return FALSE;
}
[audioPalyer prepareToPlay];
[audioPalyer setVolume: 5 ];
[audioPalyer setDelegate: self];
audioPalyer.numberOfLoops = 10;
[audioPalyer play];
thanks...
If you look in the docs under Audio Session Categories, you'll find a number of modes that you can set to tell the system how your app plans to use audio. The default is AVAudioSessionCategorySoloAmbient which tracks the ring/silent switch and the screen lock.
To have your app ignore the ring/silent switch settings, you could try changing the category:
#import <AudioToolbox/AudioToolbox.h>
AudioSessionInitialize (NULL, NULL, NULL, NULL);
AudioSessionSetActive(true);
UInt32 sessionCategory = kAudioSessionCategory_MediaPlayback;
AudioSessionSetProperty (kAudioSessionProperty_AudioCategory,
sizeof(sessionCategory),&sessionCategory);
If you want to allow iPod audio to continue playing in the background, you'll also want to check kAudioSessionProperty_OverrideCategoryMixWithOthers.
As of iOS 6, there is an alternative method that's more compact than Ramin's answer.
#import <AVFoundation/AVFoundation.h>
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback
error:nil];
To allow background audio from other apps to continue playing, add the AVAudioSessionCategoryOptionMixWithOthers option:
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback
withOptions:AVAudioSessionCategoryOptionMixWithOthers
error:nil];
There's further details in Apple's AVAudioSession Class Reference.
It must be, there's been a couple games I've had which (even when its in mute mode) will play sound. Unfortunately this was discovered whilst attempting to play games covertly in class.
As to how to actually do it, I really don't have any idea.
Here is the code:
-(void)stop
{
NSLog(#"Disposing Sounds");
AudioServicesDisposeSystemSoundID (soundID);
//AudioServicesRemoveSystemSoundCompletion (soundID);
}
static void completionCallback (SystemSoundID mySSID, void* myself) {
NSLog(#"completion Callback");
}
- (void) playall: (id) sender {
[self stop];
AudioServicesAddSystemSoundCompletion (soundID,NULL,NULL,
completionCallback,
(void*) self);
OSStatus err = kAudioServicesNoError;
NSString *aiffPath = [[NSBundle mainBundle] pathForResource:#"slide1" ofType:#"m4a"];
NSURL *aiffURL = [NSURL fileURLWithPath:aiffPath];
err = AudioServicesCreateSystemSoundID((CFURLRef) aiffURL, &soundID);
AudioServicesPlaySystemSound (soundID);
NSLog(#"Done Playing");
}
Output:
Disposing Sounds
Done Playing
In actual no sound gets play at all and completion call back isn't called as well. Any idea what could be wrong here?
I want to stop any previous sound before playing current.
AFAIK, the only supported files are .caf, .aif, or .wav:
To play your own sounds, add the sound
file to your application bundle; the
sound file must adhere to the
following requirements:
Must be .caf, .aif, or .wav files.
The audio data in the file must be in PCM or IMA/ADPCM (IMA4) format.
The file's audio duration must be less than 30 seconds.
Audio & Video Coding How-To's
What does err contain? From that you should be able to infer the problem.
You can use AudioToolBox framework for this:
CFBundleRef mainbundle = CFBundleGetMainBundle();
CFURLRef soundFileURLRef = CFBundleCopyResourceURL(mainbundle, CFSTR("tap"), CFSTR("aif"), NULL);
AudioServicesCreateSystemSoundID(soundFileURLRef, &soundFileObject);
AudioServicesPlaySystemSound(kSystemSoundID_Vibrate);
AudioServicesPlaySystemSound(1100);