How to play sound on "Ringer & Alert Volume" level - iphone

In my app I want to have an alarm/notification on "Ringer & Alert Volume" level.
In other words on the volume level which phone rings or alarms are used on (Preferences/Sound/Ringer and Alerts). It is different from the system volume level which changes with the volume buttons.
It should also play more than 30 sec.
I know it's possible because some clock apps can do that.
UPDATE:
OK I found an unideal solution. If use AudioServicesCreateSystemSoundID and load my own sound file (aif, wav, etc no mp3 or other compressed audio) than it works.
The only problem is that it has to be 30 sec or shorter, but I can can cut it into 30sec pieces and play them piece by piece.
It does not work when on silent switch on which is a problem for me.
Code:
SystemSoundID mySound;
AudioServicesCreateSystemSoundID(CFBundleCopyResourceURL(CFBundleGetMainBundle(), CFSTR("mySound"), CFSTR("aif"), NULL), &mySound);
AudioServicesPlaySystemSound(mySound);
I suspect that OpenAL should do that. Does anyone has a simple example which loads a aif/wav file and plays it on iPhone. I found some complicated examples, but all I need is just play and stop a sound (even if it's leaking that's fine).

I found it out myself. I have to use AudioServicesCreateSystemSoundID and load my own sound file (aif, wav, etc no mp3 or other compressed audio)
The only problem is that it has to be 30 sec or shorter, but I can can cut it into 30sec pieces and play them piece by piece.
It does not work when on silent switch on
Code:
SystemSoundID mySound;
AudioServicesCreateSystemSoundID(CFBundleCopyResourceURL(CFBundleGetMainBundle(), CFSTR("mySound"), CFSTR("aif"), NULL), &mySound);
AudioServicesPlaySystemSound(mySound);

You should look into AVAudioPlayer class. It will allow you to play sound files of any duration and you can control the volume output for the specified files you wish to play. I'm not in front of my mac otherwise I would provide code. Let me know if you need further instruction.

Related

iPhone short file to play

I am wondering if it is possible to play several little audio files on iPhone. I created a sound file of 25µs but I can't play it on Simulator or on the Device.
So I created several files 25µs, 50µs and 100µs but even if I use NSArray or NSMutableArray with ÀVQueuePlayer`I couldn't play it!
Do someone know the minimum time to play a sound or if what I am doing is possible?
Thank you for your answers
PS : My project is to communicate with other device on the audio
What makes you think it is not being played ? 25µs is 25 millionth of a second long !
I suggest you start playing a clip which is audible, then cut its length in half
and repeat until it becomes inaudible. Is your intention to have the other device listen ? you could get your play/listen loop working with audible clips then drill down.

Trying to play an audio file

I'm trying to play an audio file in a cocos2d application. Here is the line which tries to play the sound:
[[SimpleAudioEngine sharedEngine] playEffect:#"pig_squeal.wav"];
If I put a log near this line, the log appears, and I can play the sound with iTunes. But when the sound should be played, there is a message displayed:
AudioStreamBasicDescription: 2 ch, 44100 Hz, 'lpcm' (0x00000C2C) 8.24-bit little-endian signed integer, deinterleaved
What's the problem?
This was discussed in comments but I've amalgamated all the possibilities of why it may not work here.:
That's not an error message, just some information.
Is the sound definitely in your library and a part of the target?
You haven't changed the volume of the SimpleAudioEngine or the volume of your device isn't all the way down?
Click the sound in your library. Press Option+Command+1 to bring up the file editor. Scroll down to "Target Membership" and ensure the sound is checked for your target.
Try a different sound effect too? Try and narrow the problem down to "is it SimpleAudioEngine".
Also try and playBackgroundMusic for a sound (This was the solution in this case)
And try an mp3
There isn't a problem. It's a status message written to the log when you initialise the current OpenAL context using alcMakeContextCurrent. As far as I'm aware, you can't get rid of the message.
There is no problems with your code for the Playing of the Sound. Please check the Sound file has been added to the project also do check the format of the sound. It should play the Sound whenever you call the Play Effect. Please also try to PRELOAD the sound effect in the init method.
that message means the song was read correctly and should be playing. possible reasons you don't hear it?
volume turned down.
device malfunctioning
audio has silence in it
audio file is large and will take a long time to load.
sound has been redirect to come out of the headphones or the ear piece (even if not attached)
But the sound is loading and most likely playing.
If you are getting a crash while running in device from xcode pls try this(worked for me). Dissconnect the device from Xcode and run the app in the device. I dont know why it worked like that. But when I did this there was no crash.

how to use simpleaudioengine to play audio in a specified duration

i want to use SimpleAudioEngine to play a 2s mp3, but not so simple. I want to this audio plays 4s, that it, this mp3's duration should be 4s. Any good idea can solve this problem, Thank u very much
Edit the file to be 4 seconds using audacity audio editor. You can extend the file without changing the pitch of the audio. Then export the file as a separate mp3 file. Then just pay the file normally.

Access an mp3 in Cocos2D

I am playing a song in Cocos2D iphone using this line.
[[SimpleAudioEngine sharedEngine] playEffect:#"song.mp3"];
Suppose the mp3 is 1 min long, I want to play the song from 10 sec to 20 sec. Is it possible in Cocos2D iphone. Please help.
I don't think it is possible to "play the song from 10 sec to 20 sec" using the audio engine of cocos2d-iphone (which is named CocosDeshion).
SimpleAudioEngine is the main interface to CocosDeshion. Basically in cocos2d development we use audio files in two way - sound effect (short) and background music (long and usually loop). playEffect: is for the short ones, while playBackgroundMusic: and playBackgroundMusic:loop: is for the long ones. preloadBackgroundMusic: provides you the ability to cache the music in memory to avoid lag while playing, since they are often in compressed format.
As you can see in the documentation of SimpleAudioEngine (link above), there isn't such method for you to play a part of an audio file. Even the underlying class CDAudioManager doesn't support this.
I suggest to extract the 0:10 - 0:20 part as a separate audio file. But if you have a special reason to do this, you may want to use audio playback interfaces (such as AVAudioPlayer) in iOS SDK directly. Please refer to: http://developer.apple.com/library/ios/#documentation/AudioVideo/Conceptual/MultimediaPG/UsingAudio/UsingAudio.html

AVAudioPlayer lag when calling play

I have set up an AVAudioPlayer object in viewDidLoad, as per the Apple guidelines, calling prepareToPlay as the last line in viewDidLoad (i have tried in awakeFromNib also).
When i press my play button, there is a pause, as it would appear to load the file, then it plays.
In audioPlayerDidFinishPlaying, i reload the player with a different sound, and when clicking play for a second time, the file plays instantly.
What would cause the player to lag on the first play?
Thanks.
The delay is due to AVAudioPlayer being initialised. Please see this answer.
The audio system runs on several asynchronous software processes (audio units, OS drivers, etc.) and hardware systems (DMA, DACs, audio amp power supplies, etc.) that never really all completely finish initialization until some sound is actually played all the way out the speakers or earphones.
Here's one method to do that: Create a sound file containing a half second of silence. On app start up, while your app and view controller are still loading, use AVAudioPlayer to play this file of silence. Now when your view finishes loading, AVAudioPlayer should be ready to play subsequent non-silent sounds much faster, since some audio (silence) has already already gone all the way out to the speakers.
What kind of sound are you playing? Alerts, something longer? If alerts, I did go this way and it's much better with lags ...
create system sound with AudioServicesCreateSystemSoundID
play system sound with AudioServicesPlaySystemSound
dispose system sound with AudioServicesDisposeSystemSoundID
... you only need to store SystemSoundID for each sound you would like to play.