101 Windows Phone 7 apps Reading Notes-local FM radio

Source: Internet
Author: User

Course Content

Ø radio Turner

Ø the networkinterface. interfacetype Property

Local FM radio ApplicationProgramProvides a specific interface to operate the built-in FM radio of the mobile phone. Unlike the built-in radio in music + videos hub, this app allows users to directly enter the frequency of the station they want to listen. In addition, the application shows the current signal strength, which is also a verification of our wireless environment. The purpose of this application is to display the fmradio class in the Microsoft. devices. Radio namespace using a simple and effective radio operation interface in the mobile phone. Although it provides very limited functions, it also has some advantages. For example, it can be automatically integrated with the history and playlist in "Music + video Hub.

This application needs to obtain the media library (id_cap_medialib) in the mobile phone ). Without this capability, although the application can still display some basic information about the radio, the program will throw an exception named "radiodisabledexception" when the radio is started.

The User Interface

As we can see from the application, the user interface of the application is a mixture of "tip calculator" and "Alarm Clock.

Figure 32.1 application Homepage

Note:

Except that the double-zero button is replaced by the power button, the buttons and related canvas styles in the page resource set are extremely similar to those used in Chapter 10th "tip calculator" application.

The custom seven-segment digital tube font used for display frequency is implemented in the frequencydisplay user control. It is almost the same as the timedisplay user control in Chapter 1 "Alarm Clock", but it should be a little simpler. This chapter does not cover this aspect, but we can view the projectSource code.

 

The Code-behind

Others mostCodeThere are many similarities with Chapter 10th "tip calculator. Apart from the code used to interact with fmradio and process power buttons, the biggest difference between them is that only one decimal point is reserved when the frequency value is displayed; the "tip calculator" uses two decimal places.

Using the static fmradio. instance attribute, we can get an instance of the fmradio class. The instance contains three read/write attributes to control radio operations on the mobile phone:

* Frequency indicates the double type of the current playing station frequency.

* Powermode, which can only be on or off.

* Currentregion, which can be the United States, Japan, or Europe. The latter means "all regions except the United States and Japan ".

Fmradio also contains a read-only signalstrength attribute of the double type, which is used to indicate the received signal strength (SNR ). The range of this attribute value is not specified yet, but from the test results, it is a value between 0 and 1, where 0 indicates no signal, 1 indicates the strongest signal.

Why doesn't the radio work when a mobile phone is connected to a PC through Zune?

The Zune software running on the desktop locks the media library, which makes the program that requires id_cap_medialib unable to work normally (Similarly, when the mobile phone is connected to Zune, we cannot use the marketplace application ). If we need to test a program with this function, we can use the "Windows Phone connect tool" tool, which is included in "Windows Phone Developer Tools, this allows the PC to establish a connection with the mobile phone without running Zune.

Due to the limitations of this function, we have to disable Zune or disconnect from the mobile phone. But the best way is to detect this situation in the program. When this happens, we know the cause of this phenomenon. The local FM radio application detects this situation in the startradio method. It assumes that the failure of the powermode setting is because the mobile phone is connected to Zune, which is a safe assumption.

Another way to detect this situation is to check networkinterface. interfacetype attribute value (in Microsoft. phone. net. in the networkinformation namespace) whether it is Ethernet. It only changes to Ethernet after it is connected to Zune. However, this is an unreliable attribute, because obtaining its value requires a long blocking process. If we decide to use this method, it is best to use it in the background thread.

Because the signal strength value of the radio is variable, but it does not have any radio-related events, the home page constructor uses a timer to refresh the signal strength value every two seconds. Although this loop method consumes a lot of power, the user generally does not run the application for a long time. That's because the radio keeps working after the application exits (it is important that the application stops running in the locked state ). The constructor initializes the FM frequency to the FM frequency value set last time.

The frequency attribute in fmradio is a global setting!

If we change the radio station, this will affect the built-in radio applications (Similarly, any other third-party applications that use the radio will also be affected ). On the one hand, this operation is very convenient, because when the application starts, you do not need to remember the last radio frequency (in fact, the local FM radio application does not store any data in the isolated bucket ). On the other hand, if we want the app to be isolated from the built-in radio of the mobile phone, we need to do some additional work.

Startradio and stopradio can change the value of the powermode attribute. If the received signal strength is 0, the startradio method also displays the same information as the built-in radio application, as shown in Figure 32.2. This application assumes that when this happens, the user's in-ear monitoring line is not inserted, because for the current mobile phone, the in-ear monitoring line acts as the receiving antenna of fmbroadcast.

Figure 32.2 The application cannot obtain any FM signal when the phone's in-ear monitoring line is not connected.

The powermode attribute of fmradio has a bug in Windows Phone 7.0!

On some mobile phones, after the powermode is set to off, the radio will stop for a short period of time, but then, even if the powermode is not set back, the radio is still working. This makes it impossible to implement a button to display and synchronize the radio status. On the contrary, the power buttons in the application are very weird. In this case, if the radio cannot work when the application is started due to Zune connection, this method can be used to start the radio.

When the radio is turned on, setting the frequency to an invalid value will cause an exception. The valid or invalid value is relative to the frequency of the local station. If this is implemented through the user logic, it is somewhat complicated. Therefore, this application adopts the simplest method, that is, when an exception occurs, the displayed frequency value turns red (of course, if the current topic color is red, then you cannot see the effect ).

After the user leaves the application (the application is still running), we can still control the radio by clicking the volume-up or volume-down button. The application will display an interface at the top of the screen, as shown in Figure 32.3. Interestingly, we can also search for the previous or next radio station through the back or forward button. Therefore, if we press the two buttons mentioned above during the local FM radio operation, the current broadcast station will be different from the displayed frequency value. Although this application can detect and verify this situation through its timer tick event handler, this is not a big problem.

Figure 32.3 In addition to changing the volume, the volume control interface can also enable or stop the radio, or even search for the previous or later radio station by receiving the signal strength.

Can we get the call number, name, or information of the current broadcast station?

The answer is no. Although the built-in radio application can display this information, we cannot do this from the APIS currently open to developers.

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.