Audio Visualizer with AVQueuePlayer - objective-c

I'm trying to build a Audio Visualizer when playing from the AVQueuePlayer, I want something similar to this but AVQueuePlayer does not have some of the methods required like [audioPlayer updateMeters], [audioPlayer numberOfChannels] and [audioPlayer averagePowerForChannel:]. Could anyone help me with a workaround to this or link me to a guide to setting up one. I want to use the AVQueuePlayer because of the queue ability however if I can't setup something I would consider managing the queue myself and using AVAudioPlayer.
Thanks for your help.

AVQueuePlayer is a subclass of AVPlayer, not AVAudioPlayer, which is why it's "missing" those methods.
You can just use AVAudioPlayer for playback and a MPMediaPickerController for loading up a queue of songs. Take a look at the AddMusic sample code. It's a bit dated, but should get you going.

Related

Setup separate app volume controls

How can i control my app's output volume.
I've got an app that uses https://github.com/mattgallagher/AudioStreamer to stream mp3 files from the internet. The AudioStreamer class does not have a way to change output volume and I don't want to change system volume.
Many apps do this:
iTunes
Spotify
MPlayerX
Most Audio Players
Edit: If you're hear about AudioStreamer, I've since switched to Apple's AVPlayer, which i've found far simpler and superior. Easy volume adjustment too!
AudioStreamer and I'm guessing most OSX media players use the AudioToolbox framework. AudioToolbox uses a programming interface called AudioQueue to playback media files. Here is the way to adjust the volume using AudioQueue.
AudioQueueSetParameter(audioQueue, kAudioQueueParam_Volume, 0.5);
audioQueue is an AudioQueRef
kAudioQueueParam_Volume tells AudioQueueSetParameter() to change the Volume Parameter
0.5 is the volume from 0.0 - 1.0
More details on AudioQueue: https://developer.apple.com/library/mac/#documentation/MusicAudio/Reference/AudioQueueReference/Reference/reference.html
You can use AVAudioPlayer, it has an instance method setVolume: to set the output volume:
AVAudioPlayer * audioPlayer = ...
float volume = aVolumeValue / 100.f; // aVolumeValue can be 0~100
[audioPlayer setVolume:volume];
[audioPlayer play];
By the way, you can store the aVolumeValue into NSUserDefaults, and control it by UISlider object.

how to play two AVAudioPlayer using singleton object...?

I am beginner of iPhone. I want to play two sound one by one. one stop and then second play.only one button click event play two sound one by one and using singleton object. give any source code and suggestion which apply in my apps...
You can use the delegate method
- (void)audioPlayerDidFinishPlaying:(AVAudioPlayer *)player successfully:(BOOL)flag, which belongs to the protocol AVAudioPlayerDelegate.
You can read about it here.

Stop iTunes/Spotify Music when returning to App

Im having some trouble finding information on this. I would like to know how to stop the itunes/spotify or any other music players music upon returning to my application. They currently both play the same time.
Can anyone point me in the right direction?
Thanks
You can try this in your application: didFinishLaunchingWithOptions: method:
if ([[MPMusicPlayerController iPodMusicPlayer] playbackState] == MPMusicPlaybackStatePlaying) {
[[MPMusicPlayerController iPodMusicPlayer] stop];
}
Don't forget: You need to add MediaPlayer.framework to your target and in your AppDelegate.m import MediaPlayer/MediaPlayer.h.
Also take a look at the documentation.
EDIT
I found a better solution than mine in Apples Audio Session Programming Guide: Checking if Other Audio is Playing During App Launch
EDIT 2
Code sample …

AVQueuePlayer playing items simultaneously rather than in sequence

I am trying to put together a simple app to play a pre-roll video followed by some content video.
Currently, I'm trying to use the AVQueuePlayer class to get this done. Unfortunately, it seems to want to play the videos properly in sequence.
For example, the pre-roll plays by itself for a few seconds, then (prior to the pre-roll being complete) it starts to try and play the content video. For a few seconds, the player seems to be at war with itself and switches back and forth between the two videos (neither playing very well at all). Finally, when the pre-roll is finished, the content video then plays the rest of the way normally.
I've looked through the documentation for the AVQueuePlayer and I don't see anything obvious that I'm missing.
My code is pretty basic:
AVPlayerItem *preRollItem = [AVPlayerItem playerItemWithURL: preRollUrl];
AVPlayerItem *contentItem = [AVPlayerItem playerItemWithURL: contentUrl];
self.player = [AVQueuePlayer queuePlayerWithItems:[NSArray arrayWithObjects:preRollItem, contentItem, nil]];
[self.player play];
What is the trick to getting the videos to play in sequence.
Make sure you are actually testing on the device. From my experience the iOS5 simulator has big problems with AVQueuePlayer and does bizarre things.
I found the iOS4.3 simulator is doing a much better job when it comes to testing AVFoundation.

How do you know when a user chooses to play a video through AirPlay?

I have a custom video player set up with custom controls, and I utilize MPVolumeView to provide an airplay button. When a user chooses to use AirPlay, they interact with that Apple UI and there is no event (that I can find) that says "hey, the video is now playing over AirPlay".
The problem is that, if I close the player and reopen it, it loads the movie (load state changes to MPMovieLoadStatePlayable), I play it, and I immediately get a playback did finish notification with reason being MPMovieFinishReasonPlaybackEnded, and the video continues to try to play through AirPlay. I'm certain the movie stops and is deallocated whenever I close the player.
If anyone has any advice on how to handle this, knows some events to listen for, or has any ideas about this whatsoever, please let me know. Thanks!
The answer here turns out to be that, at least up to 4.3, there is no way to get an answer to this through code.
The problem in this case is how you dispose of the MPMoviePlayerController when you're finished with it. Even if the video plays through, before you finally release it, you have to call pause and then stop. Like this:
MPMoviePlayerController *mp = [[MPMoviePlayerController alloc] init];
// use the player. then when done with it:
[mp pause];
[mp stop];
[mp release];
If you don't do this then the next time you create a MPMoviePlayerController, certain properties are somehow ghosted in the framework. Playing a video progressively caused audio from the previous mp to play while the new mp did its initial buffering. Also, if the previous video was playing over airplay, the next video would get a notification that the video finished right after it starts and some other weirdness.
Long story short, dispose of your video players with the above sequence to avoid issues with later movie players.