[WP 8.1 Development] solves the problem that calling a real camera will crash.

Source: Internet
Author: User
Tags silverlight

Whether you use Silverlight or the rt api for development, when you use mediacapture to take photos or record videos, it is fine to run on the simulator. However, once you put them on a real mobile phone, someone must have discovered it, and a careful friend must have discovered it-I don't know why, it often causes the phone to restart or crash.

Ah, by the way, let's talk about it. It's not terrible to crash, you don't need to reset it, you don't need to brush your machine, and you won't lose any information. You just need to press both the "volume reduction" and "Power Supply" keys at the same time, hold it down at the same time, don't let it go, wait for more than 10 seconds, it will shut down, and then you open the two keys, so that the phone will be soft and no data will be lost.

However, if the base station of your carrier (such as China Unicom) does not have the real-time update time function, the mobile phone cannot be updated after the soft start. You can adjust it manually. Some people say: isn't the automatic update time set? Why not? Note that the automatic update time on WP is not updated through network connection, but through the base station of the mobile operator. My China Mobile card will get the data of the base station, but the time is not accurate, it will be slow for half an hour.

Well, there are too many nonsense mentioned above. I'm worried someone will throw bricks. We must be patient with the good habits of doing things when we are coder. Otherwise, you are really not a good coder. Don't yell at your grandfather and mother-in-law as soon as you encounter any problems, and don't kick the table and hit the stool there, because you don't have that skill, the table won't hurt your feet.

The reason for calling the camera API to cause a crash is that the application occupies system resources, that is, your application does not release relevant resources in time. This usually occurs when debugging, because we usually end the application directly on vs during debugging, as a result, the code for clearing the resources is not executed, and the system resources are still occupied by you, so once the application is executed again, or running other camera applications will cause the system to wait infinitely, so it will crash.

 

To release a mediacapture object, you only need to call its dispose method. The key is where to call it.

To ensure security, you should release the app when you navigate to the Left page (processing the onnavigatingfrom method), or release it when the app is not running in the foreground, and re-instantiate the mediacapture object when you return to the app again. The advantage of doing so is to release resources in time, and after your application is sent to the background, you cannot guarantee that other programs will not call the camera. Otherwise, you will occupy the resources, no one else can use it.

 

Taking the Silverlight framework as an example, you can define some Members in the app class to initialize and clean up mediacapture.

Public sealed partial class app {mediacapture capture = NULL; /// <summary> /// obtain the mediacapture instance through this attribute // </Summary> internal mediacapture photocaptureforcurrent {get {return capture ;}} /// <summary> /// initialization /// </Summary> internal async task initailizecapture () {var Devs = await deviceinformation. findallasync (deviceclass. videocapture); deviceinformation BC = Devs. firstordefault (D => D. enclo Surelocation. Panel = windows. devices. enumeration. Panel. Back); If (BC! = NULL) {mediacaptureinitializationsettings = new mediacaptureinitializationsettings (); settings. audiodeviceid = ""; settings. videodeviceid = BC. ID; capture = new mediacapture (); await capture. initializeasync (settings) ;}/// <summary> /// clear /// </Summary> internal void clearupcapture () {If (capture! = NULL) {capture. Dispose (); capture = NULL ;}}}

After calling the dispose method, set the mediacapture variable to null reference, so that even if the cleanup method is called multiple times, no error occurs.

For the sake of security and insurance, cleaning methods may be repeatedly called. Because I will consider calling a page once when it leaves, once when the application is put into the background, and once when the application is closed.

Someone may ask, why? Because1. Leave the page; 2. The application is disabled (not on the frontend); 3. The application is disabled..

You cannot guarantee that these three events occur. Sometimes, 1 may occur, but 2 and 3 won't.

For example:

When I run the application, I suddenly press a START key and return to the Start Screen. At this point, the navigation will exit the page, and the deactived event of the application will occur, but the Close event of the application will not happen.

In short, it can be cleared once when these three events occur, which is guaranteed to be complete.

 

A. When you navigate to the Left page, clear it.

        protected override void OnNavigatingFrom ( NavigatingCancelEventArgs e )        {           ……            (App.Current as App).ClearupCapture();        }


B. Clean up when the application is sent to the background.

        private void Application_Deactivated ( object sender, DeactivatedEventArgs e )        {            ClearupCapture();        }

The deactivated event is returned.

C. When the application is disabled, clear it.

        private void Application_Closing ( object sender, ClosingEventArgs e )        {            ClearupCapture();        }

Processing the closing event.

Be cautious when calling a camera using the Runtime API, especially when debugging,Disable the application on the mobile phone first, and then end the application on..

I did not lie to you. After more than N tests in the past week, I did not crash. During the test, my poor Lumia 920 didn't know how many times it died. Remember, it's not hard to borrow a loan. System resources must be released.

 

Next, we will attach a test example I wrote. If you do not think this example is too spam, you can come and have fun.

Http://files.cnblogs.com/tcjiaan/AppCamera.zip

 

Another headache for me is that the hardware of different mobile phones is different, and the camera angle is often biased. 920 of cameras Rotate 90 degrees. This problem can be easily solved, it is difficult to do it, because the current RT library cannot be rotated.

[WP 8.1 Development] solves the problem that calling a real camera will crash.

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.