Windows Phone Universal App MediaCapture alternatives - c#

I have a big problem with Universal App's MediaCapture. In this link I found that the problem might be the update installed on the phone which goes in conflict with this class.
I tryed some phones and all phones with Windows phone 8.1 Update crushes on MediaCapture initializing. No errors, just the phone quits the app.
In this article they said that it is due to a bug which closes the camera stopping the application.
Now, my problem is to find an alternative to MediaCapture, because half phones I need are with update 1 and half with update2 and I can't develop the app only for half customers.
Does any of you knows an alternative class?
PS: phones where app crushes have this update: 8.10.14219.341
Thanks all and sorry for my not perfect english.

Have you looked into using CameraCaptureTask instead? It can be as simple as this:
CameraCaptureTask cameraCaptureTask;
cameraCaptureTask = new CameraCaptureTask();
cameraCaptureTask.Completed += new EventHandler<PhotoResult>(cameraCaptureTask_Completed);
cameraCaptureTask.Show();
And then this is what you can do when the user is done capturing:
void cameraCaptureTask_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;
}
}

Related

MediaPlayer Universal Windows App

I started to develop application using xamarin, and one of projects inside my solution is UWP.
I need to play sound there when someone clicked button, I'm using MediaPlayer to achieve my goal, and on windows 10 (desktop) it works fine, but on my Windows Mobile 10 (Lumia 930) it starts with long delay (about 1 second).
Below I provide my code to play audio source:
MediaPlayer _player = BackgroundMediaPlayer.Current;
_player.SetUriSource(new Uri(String.Format("ms-appx:///Assets/Sound/5s.wav", UriKind.Absolute)));
_player.Play();
My Question is:
Is there any other way to play audio in UWP than MediaPlayer?
If you don't have specific reason to use background audio, you can use just media element to play audio in foreground:
<!-- create element in XAML or in code -->
<MediaElement Name="mediaElement" ... />
// Code - set source or reference to stream
MediaElement mediaElement = new MediaElement();
mediaElement.Source = new Uri("msappx:///Media/sound.mp3");
I would also recommend to check with the list of supported codecs.
In more complex scenarios you may want to look at Audio Graph API.
I'm not sure if this is bad practice or not, but I am able to get instant playback if I pre-load the media.
Something like this example in pseudo-code (c# style):
class Foo
{
private MediaPlayer _player;
Foo() //constructor
{
_player = BackgroundMediaPlayer.Current;
_player.AutoPlay = false;
_player.SetUriSource(new Uri(String.Format("ms-appx:///Assets/Sound/5s.wav", UriKind.Absolute)));
}
void ButtonClicked(Object sender, EventArgs event)
{
_player.Play();
}
}

Windows phone Text Boundaries

I'm starting the development of a Windows Phone application and I can't find out how to get a rectangle selection on a textblock.
I'm looking for the exact same information as the getTextBounds(...) method gives in Android (getTextBounds)
Thanks for your help
Windows Phone 8 doesn't have any direct way to measure text. The closest you can get is to create the TextBlock and then check its ActualWidth.
TextBlock t = new TextBlock();
t.Text = "Lorum ipsum";
Debug.WriteLine("Text {0} {1},{2}", t.Text, t.ActualWidth, t.ActualHeight);
If you update to Windows Phone 8.1 then you can interop to DirectWrite to measure text, but DirectWrite is not available on Windows Phone 8.0. Win2D has measuring text on the backlog, but it's not there yet (and won't support WP8).
Did you by any chance, asking about focusing the Textbox:
//For your textbox
private void yourTextBox_GotFocus(object sender, RoutedEventArgs e)
{
(sender as TextBox).Text = string.Empty;
}
Reference
Or else try using the Control.Focus.

ShareStatusTask doubles Status on WP8.1 Facebook share

I have a simple C# WP8.0 application from where I launch, on a button click, the ShareStatusTask.
private void ButtonBase_OnClick(object sender, RoutedEventArgs e)
{
var task = new ShareStatusTask { Status = "Test message", };
task.Show();
}
When I choose to share on Facebook, Messaging, OneNote etc everything works fine on a WP8 device.
The same application running on a WP8.1 device doubles the Status text only on Facebook share.
I would like to have same behavior as on WP8. What am I missing here?
I ended up using ShareLinkTask instead of ShareStatusTask. Lucky me I needed anyway to add a link to the message.

Rendering a WriteableBitmap within Application_Deactivated event causes app to fail to re-activate after suspension

I am building a Windows Phone 8 version of an app that I already have for Windows Phone 7.1, which works perfectly.
Inside the Application_Deactivated event (in App.xaml.cs) I attempt to update the secondary tile of my app, if pinned to the start screen. Because it is a custom tile, I build it in code using a grid and by adding elements to it. So in the final step, I have something like (layoutRoot is of type Grid) :
layoutRoot.Measure(new Size(336, 336));
layoutRoot.Arrange(new Rect(0, 0, 336, 336));
layoutRoot.UpdateLayout();
WriteableBitmap bitmap = new WriteableBitmap(336, 336);
bitmap.Render(layoutRoot, null);
bitmap.Invalidate();
using (IsolatedStorageFile storage = IsolatedStorageFile.GetUserStoreForApplication())
{
using (Stream fileStream = storage.CreateFile("/Shared/ShellContent/BackBackgroundImage.png"))
{
bitmap.SaveJpeg(fileStream, 336, 336, 0, 100);
}
}
So then I can update the Tile very easily. The problem is that when the app is running and I tap the "Windows" button, the app is successfully suspended and the Tile updated, but when I hit "back" to make it active again, a default "loading" text is shown on screen and nothing will happen. However, I have noticed that by commenting out the line bitmap.Render(layoutRoot, null);, the app is re-activated successfully and it works fine, despite the fact that the Tile does not get updated upon suspension, as expected.
In the WP7.1 version of the app, this never happens, although the method for updating the Tile is the same. I really cannot figure out what is going on. Any comment/suggestion/advice will be appreciated.
Edit. Aplication_Deactivated code:
private void Application_Deactivated(object sender, DeactivatedEventArgs e)
{
UpdateLiveTile(); //Generate Tile content (as shown above) and update the Tile if it is pinned
ExportData(); //Writes data in a file
StartPeriodicAgent(); //Starts the periodic background agent that updates the live tile
}
I think that you should consider moving this code to OnNavigatedFrom event handler of the page(s). There can be side effects, because bitmap.Render require UI thread to render your xaml code.
Workaround:
RootFrame.Navigating += RootFrame_Navigating;
void RootFrame_Navigating(object sender, NavigatingCancelEventArgs e)
{
if(e.Uri.OriginalString=="app://external/")
{
// update the custom tiles here and the resume error is gone..
}
}
Source: http://social.technet.microsoft.com/wiki/contents/articles/22256.windows-phone-8-writeablebitmap-and-app-resume.aspx

Set source and play a single mp3 file on each button on windows phone 7

i'm trying to make an application for windows phone 7.1 for schoool. I want to play a sound for each pressed button on my application (just like istantfun button for android) but i have some problem. If i declare on my xaml all the 120 Media Elements my applcation will play only 3/4 sound randomly. I want to make something like this:
private void button1_Click(object sender, RoutedEventArgs e)
{
prova.Source = new Uri("/mp3/call.mp3", UriKind.Relative);
prova.Play();
}
where prova is a single MediaElement declared on the first page of xaml file.
How can i do? Thanks advice
first of all if you want play sound in multi-page app than media element is no good solution.
Besides of it's limitation (single sound at a time) and after considering amount of sound effects maybe you should try using XNA as described here: http://www.dotnetscraps.com/dotnetscraps/post/Play-multiple-sound-files-in-Silverlight-for-Windows-Phone-7.aspx
public void PlaySound(string soundFile)
{
using (var stream = TitleContainer.OpenStream(soundFile))
{
var effect = SoundEffect.FromStream(stream);
FrameworkDispatcher.Update();
effect.Play();
}
}
With this solution you don't need any XAML elements and thus you can play it app-wide.

Categories