Windows Phone 8.1 Development solves the problem of calling a real camera to crash

Source: Internet
Author: User
Tags silverlight


Whether you're using Silverlight or an RT API to develop, it's fine to run on an emulator when you use Mediacapture or video, but once you've put it on a real phone, someone must have found out, and the attentive friend must have found out---don't know why, Will often cause the phone to reboot, or panic.



Ah, by the way, tell us that the crash is not terrible, do not need to reset, also do not brush machine, will not lose information, you just press "volume minus" + "power" two keys, to hold at the same time, do not let go, about more than 10 seconds later, will shut down, and then you release the two keys, so that the phone will be soft, and will not lose data.



However, if your operator (such as China Unicom) base station does not have real-time update function, then, the phone will not be allowed after the soft reboot, you can manually adjust. Some people say: Not set the automatic update time? How could it not be allowed? Note WP on the automatic update time is not used to update the network connection, but through mobile operators base station to update, my China Mobile card will get the base station data, but time is not allowed, will be slow for half an hour.



Well, it says too much nonsense, I'm afraid someone will throw bricks. We must have patience to do the good habit of coder. Otherwise you are really not good coder, do not have a problem right there scold grandpa dozens, also do not play table to hit stool, because you do not have that skill, the table does not kick bad your feet will be painful.



The reason for the crash caused by calling the camera API is: The application consumes the resources of the system, which means that your application does not release the related resources in a timely manner, generally this happens when debugging, because we usually end the application directly on VS when debugging, so that the code that cleans the resource is not executed, system resources are still occupied by you, so once you execute the application again or run another camera application, it will cause the system to wait indefinitely, so it freezes.



To release the Mediacapture object is actually very simple, just call its Dispose method, the key is where to invoke.



See more highlights of this column: http://www.bianceng.cnhttp://www.bianceng.cn/OS/extra/



For security insurance, you should either release (Process the Onnavigatingfrom method) when navigating away from the page, or release the application when it is not running in the foreground, and then instantiate the Mediacapture object again when you return to the application. The benefits of doing so can release resources in a timely manner, and after your application is sent to the background, you cannot guarantee that other programs will not call the camera, otherwise, the resources are always occupied by you, others can not use.



In the Silverlight framework, for example, you can define some members in the app class to initialize and clean the mediacapture.


public sealed partial class App
    {
        MediaCapture capture = null;
    
        /// <summary>
        /// Get the MediaCapture instance through this property
        /// </ summary>
        internal MediaCapture PhotoCaptureForCurrent
        {
            get {return capture;}
        }
    
        /// <summary>
        /// Initialize
        /// </ summary>
        internal async Task InitailizeCapture ()
        {
            var devs = await DeviceInformation.FindAllAsync (DeviceClass.VideoCapture);
            DeviceInformation bc = devs.FirstOrDefault (d => d.EnclosureLocation.Panel == Windows.Devices.Enumeration.Panel.Back);
            if (bc! = null)
            {
                MediaCaptureInitializationSettings settings = new MediaCaptureInitializationSettings ();
                settings.AudioDeviceId = "";
                settings.VideoDeviceId = bc.Id;
                capture = new MediaCapture ();
                await capture.InitializeAsync (settings);
            }
    
        }
    
        /// <summary>
        /// clean up
        /// </ summary>
        internal void ClearupCapture ()
        {
            if (capture! = null)
            {
                capture.Dispose ();
                capture = null;
            }
        }
    
    } 


After calling the Dispose method, set the mediacapture variable to a null reference, so that no error occurs even if the cleanup method is invoked more than once.



For security purposes, the cleanup method may be repeatedly invoked. Because I'll consider calling once when the page is gone, calling once when the application is in the background, and calling once when the application shuts down.



Some people ask, why do you do this? Since 1, leaving the page, 2, the application is deactivated (not in the foreground); 3. Application Shutdown .



There are three things you can't guarantee it will happen, sometimes, maybe 1, but 2 and 3 won't happen.





Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.