WP8 Play sound when application is onbackground - windows-phone-8

Developing tools : Visual Studio 2012, target wp platform : wp8.
I am facing the above problem:
When my application is running on background and i am receiving a notification i vibrate the phone successful and i want to play a specific sound.
What i have tried so far:
Player = new MediaElement();
Player.AutoPlay = true;
Player.Volume = 5;
Player.Source = new Uri("/data/alert.mp3", UriKind.RelativeOrAbsolute);
Player.MediaOpened += (Object s, RoutedEventArgs args) =>
{
Player.Play();
};
I i am not getting any exception but no sound. Neither on foreground nor background .
Am i missing something?
I will appreciate any help.

You can't use the MediaElement API to play notification sounds when the app is in background. In order to launch a notification and play a custom sound, you need to use the ShellToast and use the Sound property via reflection.
Here is how play a custom notification sound from background (MSDN link) Using custom sounds in toasts on Windows Phone 8 Update 3
EDIT: When the app is running in foreground, you can use the same technique (ShellToast with Sound).

Even MediaPlayer class can help you play songs and media in the background when you are showing a toast or a small pop up dialog.
Check here : http://msdn.microsoft.com/en-us/library/microsoft.xna.framework.media.mediaplayer.play.aspx

Related

Windows Phone 8.1 Has No Sample Images?

I am testing PhotoChooserTask in my 8.1 app and I do not see any available sample images that come with the emulator? Is there a way to get sample images?
void photoChooserTask_Completed(object sender, PhotoResult e)
{
if (e.TaskResult == TaskResult.OK)
{
//MessageBox.Show(e.ChosenPhoto.Length.ToString());
//Code to display the photo on the page in an image control named myImage.
System.Windows.Media.Imaging.BitmapImage bmp = new System.Windows.Media.Imaging.BitmapImage();
bmp.SetSource(e.ChosenPhoto);
myImage.Source = bmp;
}
}
You don't need to download from the Internet. I found a great workaround for this.. In the new WP8.1 emulator you have the option to emulate a SD card. Its great. I have created a blog post and a YouTube video. Have a look. Hope it helps.
http://wpdevkvk.wordpress.com/2014/07/19/adding-your-own-photos-to-windows-phone-8-1-emulator/
In the Windows Phone 8.1 emulator there aren't any images available. Actually there are some images for selecting as the background for the start screen, but it is only available for start+theme app in settings page. You can't access them. But you have a workaround for that. Before you test your app you can open the camera app and take some photos. (Obviously it will take save some images of random squares but it will have to do) Then next time you run the app your PhotoChooserTask has some photos to select from.

Playing Soundbite in WP8 App

I'm trying to write a WP8 app that plays a short sound when a button is pressed, but I cannot seem to figure out how to play the sound. Here's a quick example of my code:
XAML
<Rectangle x:Name="Rect1" Grid.Row="0" Grid.Column="0" Tap="RectTapped" Fill="White" />
App.cs
private void RectTapped(object sender, System.Windows.Input.GestureEventArgs e)
{
MediaElement sound = new MediaElement();
sound.AutoPlay = false;
sound.Source = new Uri("Assets\\Sounds\\bark-1.wav", UriKind.Relative);
sound.Play();
}
When testing on my Nokie 820 device no sound plays. I can't understand why.
Is there something I'm doing wrong? The .wav is in my resources.
I've read that MediaElement shouldn't be used for this task. I've tried using the SoundEffect class in Xna.Framework.Audio; following the example from MSDN but that also fails because I couldn't use Content.Load as Load was not an available method of the Content class.
I've also looked at XAudio2, but as I do not know C++ I can't get my head around the examples.
You need to add MediaElement to your XAML tree
this.LayoutRoot.Children.Add(sound);
Instead of a Rectangle, use a Button. Also, MediaElement is good for playing short sounds in Silverlight applications. Make sure that the control is a part of your visual tree (add it in XAML). Then bind to the button Click event handler:
private void YourClickHandler(object sender, RoutedEventArgs e)
{
myMediaElement.Source = new Uri("/Assets/Sounds/bark-1.wav", UriKind.Relative);
myMediaElement.Play();
}
You should be using the XNA SoundEffect class instead of the MediaElement because, well, you're playing sound effects.
The documentation is bad for this area, but this is how you do it:
effect = SoundEffect.FromStream(stream);
FrameworkDispatcher.Update();
effect.Play();
so to play a soundeffect from your app package:
var stream = Application.GetResourceStream(filepath);
effect = SoundEffect.FromStream(stream);
FrameworkDispatcher.Update();
effect.Play();
The advantage over MediaElement is that the SoundEffect does not need to be in the visual tree (and also it doesn't screw up the background audio player). The disadvantage is that you have none of the (sometimes useful) events that you have on MediaElement.
For Windows Phone 8.1 Silverlight, take the first two answers (from #DenDelimarsky and #onmyway133) to play the sound.
Without adding MediaElement to XAML sound not played in my case.
Use below code to play sound:
private void YourClickHandler(object sender, RoutedEventArgs e)
{
MediaElement myMediaElement = new MediaElement();
myMediaElement.Source = new Uri("/MP3/AirHornTwoBlows.mp3", UriKind.Relative);
LayoutRoot.Children.Add(myMediaElement);
myMediaElement.Play();
}
Remove the line
sound.AutoPlay=false;
or change it to:
sound.AutoPlay = true;
it will work then.

Capture video without sound in Windows Store App

I want to write a Windows Store App that can capture video (without any sound) and take pictures. Imagine a digital camera: you can preview the picture on the screen of your device before pushing the button which takes the pic.
The problem I'm facing now is the fact that the Windows.Media.Capture namespace has only classes for objects that capture video with sound (CameraCaptureUI, MediaCapture). I'm not troubled by the objects' capabilities, but by the fact that I will have to include in the manifest of the app the Microphone capability and it does not make sense for the app to use it. I need a class that uses only the Webcam capability.
Any ideas?
I found the answer and I thought I should share it. I'm sorry for answering my own question, but here goes:
One can specify in the settings of the MediaCapture object, when initializing it, that it will use only the Video part:
var mediaCaptureMgr = new MediaCapture();
var captureSettings = new MediaCaptureInitializationSettings();
captureSettings.StreamingCaptureMode = StreamingCaptureMode.Video;
await mediaCaptureMgr.InitializeAsync(captureSettings);
RTFM!

AS3 AIR mobile urlloader progress bar

I have an app (for iOS and Android) which has the ability for users to select an image from the cameraroll or take an image using the cameraui. After selecting the image it is then uploaded to a server using urlloader and urlrequestwrapper class. This all works great but I need to display a percentage uploaded progress text/bar to the user but having real trouble finding a solution.
Thanks
Listen ProgressEvent.PROGRESS:
var loader = new URLLoader();
loader.addEventListener(ProgressEvent.PROGRESS,progressHandler);
function progressHandler(e:ProgressEvent):void {
trace(e.bytesLoaded/e.bytesTotal);
}
But notice, that your server has to support dispatching progress event for flash. Read the same problem here link

How to control mobile softkeys from Flash application embedded in HTML

I have a flash application running Flash 9 (CS3). Application is able to control the Softkeys when this flash application is loaded in the supported mobile device. But, the application doesn't have control when the same is embedded in HTML page and browsed via supported mobile device. Any ideas how to make this work?
Thanks
Keerthi
There is no special way to receive soft key events when embedded in HTML - if the browser/OS gives the events to Flash, then you can catch them like any other key event:
var myListener = new Object();
myListener.onKeyDown = function() {
var code = Key.getCode();
if (code==ExtendedKey.SOFT1) {
trace("I got a soft key event");
}
}
Key.addListener(myListener);
However, you'll find that most phones/browsers will not give you soft key events when your SWF is embedded in HTML. This isn't part of the Flash Lite spec - strictly speaking I believe they could give you those events if they wanted to, but most phones simply use those keys for browser functions, and consume them before they get to Flash.
Note that you can check at runtime whether or not softkeys are available:
trace(System.capabilities.hasMappableSoftKeys);
trace(System.capabilities.softKeyCount);
If you use a switch statement, you can have more than one keycode associated with an action, you make a desktop version for testing too. I have done it myself.