i am trying to create a simple demo application that does the following: i have a button at MainPage.xaml (with Name="btnCamera") and an image control (with Name="photo") and when i press the button i want to start the camera task, capture a photo and display it on the image control. The problem is that my code works on the emulator but not on a real device. The device i have is updated to the latest update(7740). Do you have an explanation for that or any change to my code to make it work? That is my code:
public partial class MainPage : PhoneApplicationPage
{
CameraCaptureTask _cameraCapture;
public MainPage()
{
InitializeComponent()
_cameraCapture = new CameraCaptureTask();
_cameraCapture.Completed += new EventHandler(_cameraCapture_Completed);
}
private void btnCamera_Click(object sender, RoutedEventArgs e)
{
try
{
_cameraCapture.Show();
}
catch (Exception)
{
MessageBox.Show("Error occured");
}
}
void _cameraCapture_Completed(object sender, PhotoResult e)
{
if (e.TaskResult == TaskResult.OK)
{
BitmapImage image = new BitmapImage();
image.SetSource(e.ChosenPhoto);
photo.Source = image;
}
}
}
You need to make sure Zune is not running. The code looks fine and should work if you unplug the phone from the PC. If you want to debug whilst plugged into the PC, use WPConnect instead of Zune.
Related
I'm trying to implement a mediaplayer in Xamarin.Forms, and to make a full screen function I've added a button which toggles the device between landscape and portrait. This works fine, but for some reason my android device doesn't trigger the DeviceDisplay.MainDisplayInfoChanged event until I actually flip the phone to match the new orientation.
So the app DOES change orientation, but doesn't fire the event until I physically flip my phone. This is happening on a physical device.
On iOS this works fine, so I'm a little confused.
My shared code on the video player page:
private void VideoControlView_ToggleFullScreen(object sender, EventArgs e)
{
if (_isFullScreen)
DependencyService.Get<IOrientationService>().SetOrientationToPortrait();
else
DependencyService.Get<IOrientationService>().SetOrientationToLandscape();
}
private async void DeviceDisplay_MainDisplayInfoChanged(object sender, DisplayInfoChangedEventArgs e)
{
if (AutoRotateDisabled)
return;
await DisplayVideoFullScreen(e.DisplayInfo.Orientation == DisplayOrientation.Landscape);
}
And my service to change the orientation for android:
public class OrientationService : IOrientationService
{
public void SetOrientationToLandscape()
{
((Activity)Forms.Context).RequestedOrientation = ScreenOrientation.Landscape;
}
public void SetOrientationToPortrait()
{
((Activity)Forms.Context).RequestedOrientation = ScreenOrientation.Portrait;
}
}
And for iOS just in case:
public class OrientationService : IOrientationService
{
private const string ORIENTATION_KEY = "orientation";
public void SetOrientationToLandscape()
{
SetOrientation(UIInterfaceOrientation.LandscapeRight);
}
public void SetOrientationToPortrait()
{
SetOrientation(UIInterfaceOrientation.Portrait);
}
public void SetOrientation(UIInterfaceOrientation orientation)
{
UIDevice.CurrentDevice.SetValueForKey(new NSNumber((int)orientation), new NSString(ORIENTATION_KEY));
}
}
To recap:
When I trigger my ToggleFullScreen event, my android device corrently enters landscape mode, but it does NOT trigger MainDisplayInfoChanged.
I have tested this on two different physical android devices with the same issue.
Any help is appreciated, thanks!
I am trying to show error message "Cannot setup camera; currently being using" when there is already a process running the camera. I have the code that starts the preview using the MediaCapture and it works fine when running without another application using camera. I do get the exception
0x40080201: WinRT originate error (parameters: 0xC00D3704, 0x00000049, 0x10EFF1CC)
in my logs but my try catch block doesn't catch the error.
create_task(_mediaCapture->StartPreviewToCustomSinkAsync(encoding_profile, media_sink)).then([this, &hr](task<void>& info) {
try {
info.get();
} catch (Exception^ e) {
hr = e->HResult;
}
}).wait();
StartPreviewAsync method will throw a FileLoadException if another app has exclusive control of the capture device. We can catch this error to alert the user that the camera is currently being used by another application. A simple sample can be found at Use MediaCapture to start the preview stream.
However, this is a C# sample and FileLoadException is what used in .Net. For C++/CX, we can check the HRESULT value which should be 0x80070020. Following is the simple sample in C++/CX version.
MainPage.xaml
<Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
<CaptureElement Name="PreviewControl" Stretch="Uniform"/>
<Button Click="Button_Click">Click</Button>
</Grid>
MainPage.xaml.h
public ref class MainPage sealed
{
public:
MainPage();
private:
// Prevent the screen from sleeping while the camera is running
Windows::System::Display::DisplayRequest^ _displayRequest;
// MediaCapture
Platform::Agile<Windows::Media::Capture::MediaCapture^> _mediaCapture;
void Button_Click(Platform::Object^ sender, Windows::UI::Xaml::RoutedEventArgs^ e);
};
MainPage.xaml.cpp
MainPage::MainPage()
: _mediaCapture(nullptr)
, _displayRequest(ref new Windows::System::Display::DisplayRequest())
{
InitializeComponent();
}
void MainPage::Button_Click(Platform::Object^ sender, Windows::UI::Xaml::RoutedEventArgs^ e)
{
_mediaCapture = ref new Windows::Media::Capture::MediaCapture();
create_task(_mediaCapture->InitializeAsync())
.then([this](task<void> previousTask) {
try
{
previousTask.get();
_displayRequest->RequestActive();
Windows::Graphics::Display::DisplayInformation::AutoRotationPreferences = Windows::Graphics::Display::DisplayOrientations::Landscape;
PreviewControl->Source = _mediaCapture.Get();
create_task(_mediaCapture->StartPreviewAsync())
.then([this](task<void>& previousTask)
{
try
{
previousTask.get();
}
catch (Exception^ exception)
{
if (exception->HResult == 0x80070020)
{
auto messageDialog = ref new Windows::UI::Popups::MessageDialog("Cannot setup camera; currently being using.");
create_task(messageDialog->ShowAsync());
}
}
});
}
catch (AccessDeniedException^)
{
auto messageDialog = ref new Windows::UI::Popups::MessageDialog("The app was denied access to the camera.");
create_task(messageDialog->ShowAsync());
}
});
}
StartPreviewToCustomSinkAsync method is similar to StartPreviewAsync method and can also throw a FileLoadException. You should be able to handle it using the same way. For more info, please also see Handle changes in exclusive control.
I'm using this well documented solution to add a back button to our app. I'm setting things up like this when the App is initialized:
Windows.UI.Core.SystemNavigationManager.GetForCurrentView().AppViewBackButtonVisibility = AppViewBackButtonVisibility.Visible;
Windows.UI.Core.SystemNavigationManager.GetForCurrentView().BackRequested += CreateNewKeyView_BackRequested;
private void CreateNewKeyView_BackRequested(object sender, BackRequestedEventArgs e)
{
NavigationService.Instance.GoBack();
}
The back button is shown on the desktop app and works as expected, navigating our Frame back to previous pages.
However, on Windows Phone, the hardware button just exits the app. The various places that I found code like this all state that this should work for the mobile hardware button, but it simply isn't working for us.
You should set e.Handled = true in your CreateNewKeyView_BackRequested method.
Don't know how you code for your NavigationService, I just tested the following code, it works by my side:
private void CreateNewKeyView_BackRequested(object sender, BackRequestedEventArgs e)
{
Frame rootFrame = Window.Current.Content as Frame;
if (rootFrame != null)
{
if (rootFrame.CanGoBack)
{
e.Handled = true;
rootFrame.GoBack();
}
}
}
Or, for a phone, we use also special API for Hardware Buttons.
You can judge if the current using a phone Api is or not in the OnLaunched method:
if (Windows.Foundation.Metadata.ApiInformation.IsTypePresent("Windows.Phone.UI.Input.HardwareButtons"))
{
Windows.Phone.UI.Input.HardwareButtons.BackPressed += OnBackPressed;
}
then complete the OnBackPressed method:
public void OnBackPressed(object sender, Windows.Phone.UI.Input.BackPressedEventArgs e)
{
Frame rootFrame = Window.Current.Content as Frame;
if (rootFrame != null)
{
if (rootFrame.CanGoBack)
{
e.Handled = true;
rootFrame.GoBack();
}
}
}
To do this, you need at first add the Windows Mobile Extensions for the UWP references in your project.
Here is
private void CreateNewKeyView_BackRequested(object sender, BackRequestedEventArgs e) //event handle nya untuk backbutton
{
var frame = ((Frame)Window.Current.Content);
if (frame.CanGoBack)
{
frame.GoBack();
e.Handled = true;
}
}
In my windows phone 7 app I'm using the following code to navigate to a page
private void button2_Click(object sender, RoutedEventArgs e)
{
NavigationService.Navigate(new Uri("/Views/Game.xaml, UriKind.RelativeOrAbsolute));
}
In the OnNavigatedTo event of Game.xaml.cs I use the following code to play two wav files one after another. In order for me to prevent both audio play at the same time, I use while loop to wait until the first audio is finish before the second file is played. I believe this is what causing the first audio to play before the game page is displayed. Is there a better way of doing this?
protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
{
playAudio("intro.wav");
while (MySoundEffect.State == SoundState.Playing)
{
}
playAudio("firstQuestion.wav");
base.OnNavigatedTo(e);
}
The following is the code for playAudio.
protected void playAudio(string fileName)
{
Stream stream = TitleContainer.OpenStream("audio/" + fileName);
SoundEffect effect = SoundEffect.FromStream(stream);
MySoundEffect = effect.CreateInstance();
FrameworkDispatcher.Update();
MySoundEffect.Play();
}
MySoundEffect is a property of the Game class.
private SoundEffectInstance _MySoundEffect;
public SoundEffectInstance MySoundEffect
{
get { return _MySoundEffect; }
set { _MySoundEffect = value; }
}
Whenever user click the button, the sound
To answer your question in the title, you could try the same with the
Loaded
event handler instead of the OnNavigateTo. This way, your audio files will start playing
only after the page is loaded.
How can you tell whether the device is oriented vertically (portrait) or horizontally (landscape)?
Is there an API that simplifies this or do you have to make the determination "by hand" using the accelerometer?
I myself just have looked at windows 7 phones(through vs2010 express phone edition).
It seems to have in the code behind this
public MainPage()
{
InitializeComponent();
// seems to set the supported orientations that your program will support.
SupportedOrientations = SupportedPageOrientation.Portrait | SupportedPageOrientation.Landscape;
}
Then the actual form has
private void PhoneApplicationPage_OrientationChanging(object sender, OrientationChangedEventArgs e)
{
var test = e.Orientation;
}
So when the orientation changes it e.Orientation will tell you what orientation it is. Like for instance LandscapeRight.
Also you don't have to track this only via the event, you can ask for it directly from the PhoneApplicationPage instance:
private void Button_Click(object sender, RoutedEventArgs e)
{
MyCurrentOrientation.Text = this.Orientation.ToString();
}
You can also ask it through this.Orientation when your application starts so you know what the orientation is. Afther the start you can use the OrientationChanged event.
In your main:
OrientationChanged += new EventHandler<OrientationChangedEventArgs>(MainPage_OrientationChanged);
void MainPage_OrientationChanged(object sender, OrientationChangedEventArgs e)
{
Console.WriteLine(e.Orientation.ToString());
}