multi channel sound playback with MonoTouch - audio

I have a MonoTouch AVAudioPLayer that i want to play whenever an event occurs.
My code looks like this:
...
AVAudioPlayer player = AVAudioPlayer.FromUrl(url);
...
public void handleEvent(object sender, eventArgs e)
{
player.Play();
}
But if the event occurs again before the player is finished playing the sound, it will not fire again, waiting until the sound is complete before accepting another .Play() command.
How can get the sound to fire regardless of whether it is already playing?

AVAudioPlayer isn't a good choice for sound effects--it's meant more for making a music player. (It also pauses any music playing in the background like iTunes or Pandora)
Try using system sounds, see here: How can I play a sound on the iPhone using MonoTouch?

Related

How can I start playing the audio track of a AVPlayer in the background?

How do you play another item when the one playing is finished (think playlist...)
I want to be able to create a new AVPlayerLayer and attach a player to it and start it in the background (ie. audio only).
Thanks
I know I can keep the audio of a streaming video in the background by doing self?.playerLayer?.player = nil when leaving the foreground. That is not what I am asking about.
You can skip a playerLayer if you like. AVPlayerLayer is only a visual class that uses an AVPlayer.
player = [AVPlayer playerWithURL:theURL];
[player play];
or
[yourPlayerLayer.player play]
Bob's your auntie

How to launch a playlist in any music app

I have the following code which works for Google Play Music, partly for Poweramp, not at all for stock music player
public void playSelectedPlaylist(String plid) {
Intent intent = new Intent( MediaStore.INTENT_ACTION_MEDIA_PLAY_FROM_SEARCH);
intent.putExtra(MediaStore.Audio.Playlists.ENTRY_CONTENT_TYPE,
"android.intent.extra.playlist" );
intent.putExtra(MediaStore.EXTRA_MEDIA_FOCUS, "vnd.android.cursor.item/playlist");
intent.putExtra(SearchManager.QUERY, plid);
if (intent.resolveActivity(getPackageManager()) != null) {
startActivity(intent);
}else{
doToast("Sorry, no app was found to service this request", context);
}
}
The intent is INTENT_ACTION_MEDIA_PLAY_FROM_SEARCH and it finds all those apps that have this in their manifest.xml file. So far so good. However, some of these are not really suitable for mp3 playlists such as Youtube.
Question 1:
How can I return just music players. (the MUSIC_PLAYER activity has been deprecated so I need to use seomething else)
Question 2:
when executing the above code Poweramp does present the user with the appropriate playlist. When clicking this and select "Play" nothing happens other than Poweramp asking me if I want to play filtered or play all. Selecting either has no effect.
Finally the question, why does Poweramp not play the playlist?
Additional:
the reason why the stock music player does not show is because it does not have the INTENT_ACTION_MEDIA_PLAY_FROM_SEARCH. (I assume this will change in later versions)
Google Play Music has no problem and happily loads and starts to play the playlist
My Samsung has another music player which loads but seems unable to recognise it is a playlist I am passing and does not find anything although the playlist exists.
Anyone with suggestions, thoughts/suggestions I can try out?

Image capture in lwuit [duplicate]

I have tried using MediaComponent, but since it is now deprecated it wont be good moving forward. Also i was not able to get it to re-size to full screen on the form. I am trying to use VideoComponent to capture a screen shot in an S40 device. I cant find how to instantiate the VideoComponent properly for capturing an image and not playing a video.
You can use VideoComponent for capturing an image.
First, to instantiate the VideoComponent, you need to create a native peer:
VideoComponent videoComponent = VideoComponent.createVideoPeer("capture://video");
Player player = (Player) videoComponent.getNativePeer();
player.realize();
VideoControl videoControl = (VideoControl) player.getControl("VideoControl");
To capture the image, you have to start video component and use getSnapshot of Video Control:
videoComponent.start();
videoControl.getSnapshot(null);
If you want to resize the video component to full screen you can use:
videoComponent.setFullScreen(false);
Other posibility is:
videoComponent.setPreferredH(Display.getInstance().getDisplayHeight());
videoComponent.setPreferredW(Display.getInstance().getDisplayWidth());
Right now the VideoComponent is mostly designed for playback and doesn't really work well for capture. We will try to improve it in the near future to make it more flexible. Its relatively easy to migrate from MediaComponent to VideoComponent so it shouldn't be a problem.

playing audio with MPMoviePlayerViewController

Reference iOS TabController app...
Apple docs state that the AVAudioPlayer class does not provide support for streaming audio based on HTTP URL's. AVAudioPlayer plays only music embedded in the iApp with a file:// as a url.
Given that, I do use MPMoviePlayerViewController to play these web stored audio/mp3 files which it can handle.
This definitely works. I start out with a UIView with the lyrics for the song. At the very bottom of this UIView is a "Play" button. The user taps this button and the audio/mp3 plays; however, the audio/QuickTime (( Q )) graphic comes to the foreground and the lyrics disappear and will stay away until the user taps "Done". What I want to happen is that the audio/mp3 is played in the background, with the lyrics staying in front.
BTW, I really don't need for the "Done" button to be seen because the user can stop the audio by simply selecting another Tab.
Obviously this going to the background for the AV file makes sense because the MPMoviePlayerController object is designed to play video and the video ought to come to the front. It is consistent in that the audio also goes to the background .. except I need a way to defeat the audio/QuickTime (( Q )) graphic coming to the front.
I did insert code to determine if it was an audio file (mp3), versus a video file (mp4). So far so good ... and then if it was an audio file, within my actual playing segment, I have:
if ( NSClassFromString(#"MPMoviePlayerViewController") )
{
if (!isAudioFile)
{
[senderViewController
presentMoviePlayerViewControllerAnimated:moviePlayerViewController_];
}
}
[moviePlayerController_ play];
It actually works, that is, I actually hear the mp3 in the background, with the lyrics in front and the audio/QuickTime (( Q )) graphic does not show ...
BUT, what does happen is horrible, that is, the "Done" button shows over the lyrics I talked about with some sort of unknown ??? letters there.
The following answer is taken from an edit to the question:
if ( NSClassFromString(#"MPMoviePlayerViewController") )
{
if (!isAudioFile)
{
[senderViewController
presentMoviePlayerViewControllerAnimated:moviePlayerViewController_];
}
else
{
[moviePlayerController_ setControlStyle:MPMovieControlStyleNone];
}
}
FWIW, I really don't need a "Done" button because as soon as I go to another tab, either new music or a video, the music initially playing stops and the new AV file starts.

Problems with sound on a 6265i Nokia using J2ME and Netbeans 6.1

Currently, I have some basic code to play a simple tone whenever a button is pressed in the command item menu.
Using: Manager.playTone(note, duration, volume);
I also have a blackberry that I'm testing this same midlet on and the sound works fine. So, is this something specific to Nokia phones that aren't allowing me to play the sound?
I've made sure to build it using the correct CLDC and MIDP versions.
I've also tried the audio demos that are in the Netbeans IDE, and still no luck. It throws a "cannot create player" message.
http://discussion.forum.nokia.com/forum/showthread.php?t=91500
This thread on Forum Nokia seems to suggest that certain Nokia models have problems playing tones with the Manager.playTone() function, more specifically a MediaException is thrown, as you are having (MediaException is just the default exception if any problem occurs when trying to play a tone).
You can try sleeping the thread after calling Manager.playTone for greater than the length of the tone. There is a possibility that you get into a state where you are trying to play two or more tones at once and the phone might not allow more than one player to be created at a time.
If all else fails you can use the Nokia UI Sound class (com.nokia.mid.sound.Sound) to play the tone. It is deprecated and replaced with the call you are making, but it might be your only solution for this device. Just make your own playTone method and have it call the Nokia function for this device (and maybe other Nokia devices if need be) and the J2ME standard call on all other devices. You can accomplish this with the Netbeans ME Preprocessor.
http://www.theoreticlabs.com/dev/api/nokia-ui-1.1/com/nokia/mid/sound/Sound.html

Resources