count the number of loops and stop playing the audio - iphone

I have a small 4 second video file which will be played again and again.For that I have used
[player setRepeatMode:MPMovieRepeatModeOne];
but after the 1st loop I want the audio to be stopped and only video should be played.I tried setting [player setUseApplicationAudioSession:NO];
but its not working.
Any help?

There is no direct way to mute or adjust volume of MPMoviePlayerController. All you need to do is to control your device's volume.
Get MPMoviePlayerPlaybackDidFinishNotification and write Following code in specific check
[[MPMusicPlayerController applicationMusicPlayer] setVolume:0];

I don't think there is a ready to use option to count loops.
I would recommend to extend player and make your own method to play sound X times.
For that you can play this sound without repeat mode X times in a row. To detect that sound has finished playback check for MPMusicPlayerControllerPlaybackStateDidChangeNotification and beginGeneratingPlaybackNotifications. Based on that you play it again and again and decrease a count for how much times you want to play it.

Related

Is AVAudioPlayer able to loop large audio files without an audible gap?

I have large looping background music files of up to 10 minutes length. The sounds are looped perfectly and if the player introduces no delay you would not notice where the loop point is.
Can AVAudioPlayer play them without that the user will hear a gap caused by latency or other problems with looping?
You might be hearing a delay because your music is compressed (mp3 for example). AVAudioPlayer has to decode the mp3 as it streams. If you had the same music in an uncompressed format, it would take significantly more disk space, but would loop seamlessly.
As long as you don't stop/start it at the end and you set numberOfLoops to a negative integer, it should work.
I had problems trying to create seamless loops. I tried using the numberOfLoops property, with no success. I tried switching to uncompressed audio files, with no success. I even tried switching to an AVAudioQueuePlayer, queued with multiple instances of the same asset, with no success. In the end I solved the problem by creating two instances of AVAudioPlayer (player1 & player2) and switching between them. I began playback of player1 and used player2's prepareToPlay method, and the play(atTime time:) method setting the TimeInterval to player1.deviceCurrentTime - player1.currentTime + player1.duration.
I then used the delegate method audioPlayerDidFinishPlaying to prepare player1 to restart playback after player2 was done. To be honest, I don't know why this approach worked and simply setting numberOfLoops to -1 did not. But it may work for you also

Cocos Denshion: Play sound effect in sync with music

I am making a music game and when the user presses a note it will produce a sound. The sound naturally needs to play immediately when the user presses, so they can tell whether they are in time with the music. However, it feels as if the sound is lagging, especially when note presses become quicker.
My background .m4a music file is played with AVAudioPlayer. I chose to use this over Cocos Denshion as I have access to the currentTime property. I may be wrong, but I dont think I can access this with CocosDenshion.
I made a .wav file which is extremely short (less than a second). I preload my sound effect on init:
[[SimpleAudioEngine sharedEngine] preloadEffect:#"Assist.wav"];
Then to play the sound effect, in CCTouchesBegan I call:
[[SimpleAudioEngine sharedEngine] playEffect:#"Assist.wav"];
After that it calls my code to determine the users timing and awards points. Any idea why it might be lagging, or a better way to play sound effects in time with music?
EDIT: Ive tried a few things recently with no results. First I tried playing the sounds automatically as they came up to the appropriate time in the song. Still had the lag, so I dont think it is touch events being slow. I also tried 3 different sound libraries.
However, when I ran in the simulator, it seemed to not be laggy. Does anyone have an idea? Im clueless and its a major feature I cant really take out...
you should avoid this code:- [[SimpleAudioEngine sharedEngine] preloadEffect:#"Assist.wav"];
with the start of app you should load your framework SimpleAudioEngine by writing this code :-
//SimpleAudioEngine *palySound; made object in .h file.
palySound=[SimpleAudioEngine sharedEngine];
and whenever you want to play sound you can write: [palySound playEffect:#"Assist.wav"];
I am not sure what you're doing in your SoundEngine, but in my own experience, the best way to not get lag to play a sound is to assign an AVAudioPlayer for each sound file (unless you want to start messing around with AudioQueues).
Here it is an example:
Let's assume that you have an AVAudioPlayer *assistPlayer; in your current view controller.
In your viewDidLoad initialize it with your sound:
NSURL *wavURL = [[NSBundle mainBundle] URLForResource:#"Assist" withExtension:#"wav"];
assistPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:wavURL error:nil];
Then, in your IBAction where you want to play the file, just do:
[assistPlayer play];
You shouldn't get any lag.
Did you try Finch? It claims to play sounds with low latency, and it is also just a wrapper around OpenAL.
Other than that, I'm really not experienced with OpenAL, but can think of two possible reasons for your lag:
The main thread is too busy - Try to offload work from it to other
threads.
Perhaps OpenAL is defined with too large of a buffer, so the pipeline loads the entire sound into the buffer (or a big chunk of it), and only afterwards the playback starts.

iphone, my AVPlayer player is playing video really fast

My application is using AVPlayer to play video. There is a serious problem that my app currently experience. Sometimes, it is playing the video very fast although I checked to make sure that I either set the self.player.rate = 0.3 or self.player.rate = 1 or not set the rate at all. This only happens sometimes and it happens to all the videos I have.
Can anyone give me any hint on the current problem?
I finally know the answer. I called [player setRate:0.3] and [player play] at the same time and the second call override the first call, which sets the rate to be 0 (normal). The method setRate: already set the rate for the player and play the video already

Playing a a number of different sounds using AVAudioPlayer

I have a few single hit sounds(small sound effects) which I want to play when a button is pressed. Should I declare the player and then initialize the sound to the player in the IBAction of the button if I want to play different sounds on different buttons?
Or is there a method to call which you specify the sound as you play it?
the [audioPlayer play] statement plays what is already initialized or added to the player. How can I setup the play and call it at different places with some predefined sound?
Thanks
There's a bit of delay whenever you start playing a sound even if it's already loaded and all you have to do is hit [audioPlayer play]
If you need to initialize it before you play it i'd imagine the delay would be much more noticeable.
I think i'd make a separate audio player for each sound and populate an array with them. Keep an eye out for memory low messages and unload the objects if necessary but i don't think it will require much overhead.

iPhone (SDK 2.2): adusting playback volume while NOT actively playing music w/ AVFoundation?

So I have an app which plays many short sound clips. I need to know when the sounds are finished playing, and I need to use mp3s, so I'm using AVFoundation for the sound playback.
When a sound is actively playing, and the user uses the hardware volume buttons, the playback volume changes. Problem is, the app is NOT constantly playing sounds, and when it's not, and the hardware buttons are used, the RINGER volume gets adjusted instead.
How do I set it up so, as long as the app is running, the user can adjust the playback volume?
Thanks!
Turns out this can be accomplished by allocating an AVAudioPlayer with any valid sound file and calling the prepareToPlay method, without ever calling the play method.
Works perfectly.
Start an AudioSession and don't stop it when you're not playing sounds.
so you want to disable ringer playback volume as long as the app is running? therefore the hardware controls will only adjust the app playback sounds?
i dont think this is possible unless you are "always playing sound" for example, many games are always playing background music or what have you.
You might be able to accomplish this by constantly playing a 0 volume sound as long as your app is running. You could then play your sound clips over it.
How do I play multiple sounds simultaneously?