Using the 8.1 MediaCapture classes for Windows Phone.
Have declared the capabilities for "Audio" and "Webcam", which 90% is what would be the cause of the exception.
Kicker is, it works perfectly in the WP emulator, but breaks on an actual device.
Exact exception is here :
I have added a mountain of checks to make sure we aren't re-initializing the already initialized camera or trying to read before the initializations.. etc (as I assumed the issue was being caused by) So it is very unlikely to be that.
private async Task InitializeCameraAsync()
{
if (_isInitialized)
{
Debug.WriteLine("Skipping unnecessary initialization");
return;
}
Debug.WriteLine("Initializing camera media capture...");
_deviceCapture = new MediaCapture();
await _deviceCapture.InitializeAsync(new MediaCaptureInitializationSettings
{
VideoDeviceId = _cameraInfoCollection[_currentVideoDevice].Id,
PhotoCaptureSource = PhotoCaptureSource.VideoPreview,
AudioDeviceId = _microphoneInfoCollection[_currentAudioDevice].Id
StreamingCaptureMode = StreamingCaptureMode.Video
});
Debug.WriteLine("Initialized camera media capture!");
// For code completion only, unlikely to be relevant
// Set up low-lag photo capture
if (IsNotUsingInstantCapture)
{
Debug.WriteLine("Preparing low-lag photo capture");
var imageEncoding = ImageEncodingProperties.CreateJpeg();
imageEncoding.Width = PhotoCaptureWidth;
imageEncoding.Height = PhotoCaptureHeight;
_lowLagPhotoCapture = await _deviceCapture.PrepareLowLagPhotoCaptureAsync(imageEncoding);
}
_isInitialized = true;
Debug.WriteLine("Initialized camera!");
}
_mediacapture is then being bound to the .source of a xaml CaptureElement to show a preview.
It was a temporary bug with the Windows api. It was fixed with the Windows Phone 8.1 update that was released the 24th of september 2014.
Related
I have a problem with UWP media capture initilization. My code is below,
private async Task StartPreviewAsync()
{
try
{
//set initilize settings
Settings oneSetting = null;
using (SQLite.Net.SQLiteConnection conn = new SQLite.Net.SQLiteConnection(new SQLite.Net.Platform.WinRT.SQLitePlatformWinRT(), sqlpath))
{
oneSetting = (from p in conn.Table<Settings>()
where p.id == 0
select p).FirstOrDefault();
}
if (oneSetting.camera != null)
{
var settings = new MediaCaptureInitializationSettings();
settings.StreamingCaptureMode = StreamingCaptureMode.Video;
settings.PhotoCaptureSource = PhotoCaptureSource.VideoPreview;
var devices = await Windows.Devices.Enumeration.DeviceInformation.FindAllAsync(Windows.Devices.Enumeration.DeviceClass.VideoCapture);
foreach (var device in devices)
{
if ((device.Id).Equals(oneSetting.cameraId))
{
settings.VideoDeviceId = device.Id;
break;
}
}
_mediaCapture = new MediaCapture();
await _mediaCapture.InitializeAsync(settings);
//MediaCapture m = new MediaCapture();
//await m.InitializeAsync();
var focusSettings = new FocusSettings();
focusSettings.AutoFocusRange = AutoFocusRange.FullRange;
focusSettings.Mode = FocusMode.Auto;
focusSettings.WaitForFocus = true;
focusSettings.DisableDriverFallback = false;
_mediaCapture.VideoDeviceController.FocusControl.Configure(focusSettings);
await _mediaCapture.VideoDeviceController.ExposureControl.SetAutoAsync(true);
_mediaCapture.SetPreviewRotation(VideoRotation.Clockwise90Degrees);
_mediaCapture.SetRecordRotation(VideoRotation.Clockwise90Degrees);
capturePreview.Source = _mediaCapture;
await _mediaCapture.StartPreviewAsync();
_isPreviewing = true;
_displayRequest.RequestActive();
DisplayInformation.AutoRotationPreferences = DisplayOrientations.Landscape;
}
}
catch (UnauthorizedAccessException)
{
// This will be thrown if the user denied access to the camera in privacy settings
System.Diagnostics.Debug.WriteLine("The app was denied access to the camera");
}
catch (Exception ex)
{
System.Diagnostics.Debug.WriteLine("MediaCapture initialization failed. {0}", ex.Message);
}
}
It returns MediaCapture initialization failed. {0} error. Recently, it runs well. But since this morning it gives the error. Is there anybody who takes the same error?
The full error message is that;
The specified device interface level or feature is not supported on this system.
: Media Capture initialization failed. {0}
The thread 0x1924 has exited with code 0 (0x0).
Same issue here.
After many hours of retries and googling i realize that it was related to the windows 10 anniversary update.
I found the solution here:
https://www.macecraft.com/fix-webcam-issues-windows-10-anniversary-update/
I added the EnableFrameServerMode key in the registry and magically the webcam came back working.
But since this morning it gives the error.
Did you recently update your device OS? On which device did you meet this problem and what is the OS version?
I personally think this is more like a device problem or drive issue. You can try to restart your device and see if this helps. Or you can start the built-in camera app and check if this official app runs well.
I'm writing this answer here because there are too many details need to be confirmed, please leave a comment here to tell us the detail information about your device and your test result based on my suggestion, so can we keep look into this issue.
I am using Geoposition and a Postition changed event to grab Coordinates of the device location.
private async void StartGpsMonitoring()
{
if (locator == null)
{
locator = new Geolocator();
}
if (locator.LocationStatus == PositionStatus.Disabled)
{
//throw new Exception();
MessageDialog noGpsDialog = new MessageDialog("Location services are disabled, please enable location services");
noGpsDialog.Commands.Add(new UICommand("Location Settings", new UICommandInvokedHandler(this.CommandInvokedHandler), 0));
noGpsDialog.Commands.Add(new UICommand("Cancel", new UICommandInvokedHandler(this.CommandInvokedHandler), 1));
await noGpsDialog.ShowAsync();
}
if (locator != null)
{
//locator.MovementThreshold = 3;
locator.ReportInterval = 1;
locator.DesiredAccuracy = PositionAccuracy.High;
locator.PositionChanged +=
new TypedEventHandler<Geolocator,
PositionChangedEventArgs>(locator_PositionChanged);
}
}
private async void locator_PositionChanged(Geolocator sender, PositionChangedEventArgs e)
{
string speed = string.Empty;
await dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>
{
Geoposition geoPosition = e.Position;
if (e.Position.Coordinate.Speed != null)
{
speed = e.Position.Coordinate.Speed.Value.ToString(); // always 5.8
}
geolocation = geoPosition.Coordinate.Point.Position.Latitude.ToString() + " " +
geoPosition.Coordinate.Point.Position.Longitude.ToString() + "Speed = " +
speed;
var textBlockStatus =
ControlHelper.FindChildControl<TextBlock>(JourneyTrackerSection, "TextBlockStatus") as TextBlock;
textBlockStatus.Text = geolocation;
});
}
I am also trying to get the speed value. But when using the emulator I am always getting 5.8 regardless of if I have speed limit/walking/biking set on the emulator, and still get 5.8 from a static position.
Can anybody shed some light as to why? Is it just the emulator? Would I get an accurate result if I used a real device?
Its hard to develop a location speed application where I have to run out side every time I want to debug/run it.
Any help much appreciated.
Thought I would post some details as to what I have managed to find out in case anyone comes across this in the future. Looks like this is because it is running on the emulator. Managed to come across some limited details about using the geopositioning code and some details about it being hardware specific. After I have managed to get hold of a windows phone for testing the emulator cannot do the speed. Works perfect on an actual device.
This is extremely annoying by Microsoft. means that every time I need to test my app I have to go out driving. Rendering the GPS emulator completely useless!
System Information
Windows 10 Technical Preview (build 9926)
Visual Studio Community 2013Attempting to debug on:
[AT&T] Lumia 635 (Windows 10 Technical Preview for phones build 9941 w/ Lumia Cyan)
[AT&T] Lumia 1520 (Windows Phone 8.1 with Lumia Denim and PfD)
[Unlocked] BLU Win Jr (Windows Phone 8.1 with PfD)
[Verizon] Lumia Icon (Windows Phone 8.1 with Lumia Denim and PfD)
I trying to get location services working in my app. Previously, I had Visual Studio throw the error. It was an ArgumentException with the message "Use of undefined keyword value 1 for event TaskScheduled in async". Googling didn't turn up any solutions.
Here is the code:
Geolocator Locator = new Geolocator();
Geoposition Position = await Locator.GetGeopositionAsync();
Geocoordinate Coordinate = Position.Coordinate;
When I could get the error to be thrown, the exception was thrown on the 2nd or 3rd line in the sample above.
I simplified the original code to try and fix it, but this is the original:
Geolocator Locator = new Geolocator();
Geocoordinate Coordinate = (await Locator.GetGeopositionAsync()).Position.Coordinate;
The entire app works when debugging, but crashes almost instantaneously otherwise.
This is a Windows 8.1 Universal project, focusing on the phone project.
Thanks in advance
EDIT: As requested, here is the full method:
private static bool CheckConnection()
{
ConnectionProfile connections = NetworkInformation.GetInternetConnectionProfile();
bool internet = connections != null && connections.GetNetworkConnectivityLevel() == NetworkConnectivityLevel.InternetAccess;
return internet;
}
public static async Task<double> GetTemperature(bool Force)
{
if (CheckConnection() || Force)
{
Geolocator Locator = new Geolocator();
await Task.Yield(); //Error occurs here
Geoposition Position = await Locator.GetGeopositionAsync();
Geocoordinate Coordinate = Position.Coordinate;
HttpClient Client = new HttpClient();
double Temperature;
Uri u = new Uri(string.Format("http://api.worldweatheronline.com/free/v1/weather.ashx?q={0},{1}&format=xml&num_of_days=1&date=today&cc=yes&key={2}",
Coordinate.Point.Position.Latitude,
Coordinate.Point.Position.Longitude,
"API KEY"),
UriKind.Absolute);
string Raw = await Client.GetStringAsync(u);
XElement main = XElement.Parse(Raw), current_condition, temp_c;
current_condition = main.Element("current_condition");
temp_c = current_condition.Element("temp_C");
Temperature = Convert.ToDouble(temp_c.Value);
switch (Memory.TempUnit)
{
case 0:
Temperature = Convertions.Temperature.CelsiusToFahrenheit(Temperature);
break;
case 2:
Temperature = Convertions.Temperature.CelsiusToKelvin(Temperature);
break;
}
return Temperature;
}
else
{
throw new InvalidOperationException("Cannot connect to the weather server.");
}
}
EDIT 2: I've asked for help on Twitter, and received a reply asking for a repro project. I recreated the major portion of the original app, but I could not get the error. However, errors may occur for you so here's the project.
EDIT 3: If it helps at all, here are the exception details:
System.ArgumentException occurred
_HResult=-2147024809
_message=Use of undefined keyword value 1 for event TaskScheduled.
HResult=-2147024809
IsTransient=false
Message=Use of undefined keyword value 1 for event TaskScheduled.
Source=mscorlib
StackTrace:
at System.Diagnostics.Tracing.ManifestBuilder.GetKeywords(UInt64 keywords, String eventName)
InnerException:
Having checked this and this, I believe this is a bug in .NET async/await infrastructure for WinRT. I couldn't repro it, but I encourage you to try the following workaround, see if it works for you.
Factor out all asynchronous awaitable calls from OnNavigatedTo into a separate async Task method, e.g. ContinueAsync:
async Task ContinueAsync()
{
Geolocator Locator = new Geolocator();
Geoposition Position = await Locator.GetGeopositionAsync();
Geocoordinate Coordinate = Position.Coordinate;
// ...
var messageDialog = new Windows.UI.Popups.MessageDialog("Hello");
await messageDialog.ShowAsync();
// ...
}
Remove async modifier from OnNavigatedTo and call ContinueAsync from OnNavigatedTo like this:
var scheduler = TaskScheduler.FromCurrentSynchronizationContext();
Task.Factory.StartNew(
() => ContinueAsync(),
CancellationToken.None, TaskCreationOptions.None, scheduler).
Unwrap().
ContinueWith(t =>
{
try
{
t.GetAwaiter().GetResult();
}
catch (Exception ex)
{
Debug.WriteLine(ex);
throw; // re-throw or handle somehow
}
},
CancellationToken.None,
TaskContinuationOptions.NotOnRanToCompletion,
scheduler);
Let us know if it helps :)
Updated, apparently, the bug is somewhere in the TPL logging provider, TplEtwProvider. You can see it's getting created if you add the below code. So far, I couldn't find a way to disable this event source (either directly or via Reflection):
internal class MyEventListener : EventListener
{
protected override void OnEventSourceCreated(EventSource eventSource)
{
base.OnEventSourceCreated(eventSource);
if (eventSource.Name == "System.Threading.Tasks.TplEventSource")
{
var enabled = eventSource.IsEnabled();
// trying to disable - unsupported command :(
System.Diagnostics.Tracing.EventSource.SendCommand(
eventSource, EventCommand.Disable, new System.Collections.Generic.Dictionary<string, string>());
}
}
}
// ...
public sealed partial class App : Application
{
static MyEventListener listener = new MyEventListener();
}
Geolocator seems to simply not work on WP8 when it should. (But for some strange reason works perfectly fine on 8.1)... I'm in optimal conditions - LTE, FiOS Wifi... No obstructions. No reason why it shouldn't work. However, it seems to just hang and never return the current location or even an exeption.
I have tried to disable the reverse geocode and it does not work. I've narrowed it down to being the actual service that gets the location (Geolocator or GeoPosition)
ID_CAP_LOCATION is enabled. Phone location services are enabled. I'm targeting WP8.
Code:
private async void getlocation()
{
try
{
Geolocator gl = new Geolocator(); gl.DesiredAccuracyInMeters = 50;
Geoposition geoposition = await gl.GetGeopositionAsync(maximumAge: TimeSpan.FromMinutes(5), timeout: TimeSpan.FromSeconds(60));
latitude = geoposition.Coordinate.Latitude.ToString(); longitude = geoposition.Coordinate.Longitude.ToString();
//Location to physical address
List<MapLocation> locations;
ReverseGeocodeQuery query = new ReverseGeocodeQuery();
query.GeoCoordinate = new GeoCoordinate(geoposition.Coordinate.Latitude, geoposition.Coordinate.Longitude);
//Set address to tecxtblock
query.QueryCompleted += (s, e) =>
{
locations = e.Result as List<MapLocation>;
address = locations[0].Information.Address.City.ToString();
ts.Center = new GeoCoordinate(geoposition.Coordinate.Latitude, geoposition.Coordinate.Longitude);
ts.ZoomLevel = 7;
location.Text = address.ToUpper(); //Location
//Successful, now get weather for current location
getforecast();
};
query.QueryAsync();
}
catch
{
MessageBox.Show("Location services appear to be turned off. To use Atmosphere, turn location services on.");
}
}
I've encountered issues on WP8 with background tracking where it would only update the the position when the app was bought to the foreground. Very confusing as I knew it was working on other devices without issue.
In the end I simply un-installed the app completely and re-installed, this solved the issue. Just in case you haven't tried it - just completely remove the app and re-install / re-deploy using Visual Studio. Issue might resolve itself.
I had the same problem. The same code works in 8.1 but does not in 8.0
I was able to make it work when i called the function from a button click instead of the constructor of the Page or in Page_loaded event.
I have an problem with muting the mic on an windows 7 machine. But all the code i have found dosen't run ore it's not doing anything the runned. Have is it done for an Windows 7 machine using C# code. I just need an on/off solution.
The DDL file works also with Win x64bit. But i thing that i creates an error another place.
mixers.Recording.Lines.GetMixerFirstLineByComponentType(
MIXERLINE_COMPONENTTYPE.SRC_MICROPHONE).Volume = 0;
if (!mediaElement1.CheckAccess()) mediaElement1.Dispatcher.Invoke(System.Windows.Threading.DispatcherPriority.Normal, (Action)delegate { mediaElement1.Play(); });
if (MessageBox.Show("Incoming Call from: " + string.Format(e.RemoteParticipant), "Video Chat Call", MessageBoxButton.YesNo) == MessageBoxResult.Yes)
{
mixers.Recording.Lines.GetMixerFirstLineByComponentType(
MIXERLINE_COMPONENTTYPE.SRC_MICROPHONE).Volume = 1;
if (!mediaElement1.CheckAccess()) mediaElement1.Dispatcher.Invoke(System.Windows.Threading.DispatcherPriority.Normal, (Action)delegate { mediaElement1.Stop(); });
_currentConversation.StartVideo();
}'
If error occurs at if (MessageBox.Show("Incoming Call from: " + string.Format(e.RemoteParticipant), "Video Chat Call", MessageBoxButton.YesNo) == MessageBoxResult.Yes) and says {"Arithmetic operation resulted in an overflow."}
http://www.computercabal.com/2010/11/mute-microphone-from-c-on-windows.html -- this gentleman appears to have had a similar problem, and he's provided the source code for a solution.
You can use Audio Switcher Api
https://www.nuget.org/packages/AudioSwitcher.AudioApi.CoreAudio/4.0.0-alpha5
Code is quite simple:
private async void btnMute_ButtonClick(object sender, EventArgs e)
{
var audioController = new CoreAudioController();
var devices = await audioController.GetDevicesAsync(DeviceType.Capture, DeviceState.Active);
var device = devices.FirstOrDefault(x => x.IsDefaultDevice);
if(device != null) {
await device.SetMuteAsync(!device.IsMuted);
}
}
this might help: Windows Mixer Control in C#
Good luck :).
EDIT: It can also mute certain devices if I'm right.