"101 Windows Phone 7 Apps" reading notes-Local FM Radio

"101 Windows Phone 7 Apps" reading notes-Local FM Radio

Course content

Ø Radio Turner

Ø The NetworkInterface.InterfaceType Property

    The Local FM Radio application provides a specific interface to operate the built-in FM radio of the phone. Unlike the radio built into the phone in the "Music + Videos hub", this application allows users to directly input the frequency of the radio station they want to listen to. Moreover, the application also displays the current received signal strength, which is also a verification of the wireless environment we are in. The purpose of this application is to show the simple and effective radio operation interface used in mobile phones, namely the FMRadio class in the Microsoft.Devices.Radio namespace. Although it provides very limited functions, it also has some advantages. For example, it can be automatically integrated with the history and playlists in the "Music + Video hub".

    The application needs to obtain the media library in the phone (that is, with ID_CAP_MEDIALIB capability). Without this capability, although the application can still display some basic information of the radio, when the radio is started, the program will throw an exception called "RadioDisabledException".

The User Interface

    As we can see from the screenshot of the application, the user interface of the application is a mixture of "Tip Calculator" and "Alarm Clock".

Figure 32.1 Application main page


➔ Except that the double-zero button is replaced by the power button, the buttons and related canvas styles in the page resource collection are very similar to those used in the "Tip Calculator" application in Chapter 10.

➔ The custom seven-segment digital tube font used to display the frequency is implemented in the FrequencyDisplay user control. It's almost the same as the TimeDisplay user control in Chapter 20 "Alarm Clock", but it's still slightly simpler. This chapter does not involve the introduction of this aspect, but we can view the project source code.

The Code-Behind

➔ Most of the code used here has many similarities with Chapter 10 "Tip Calculator". In addition to the different codes for interacting with the FMRadio class and processing the power button, the biggest difference between them is that when displaying the frequency value, only one decimal place is reserved; while the "Tip Calculator" uses two decimal places.

➔We can get an instance of the FMRadio class through the static FMRadio.Instance property. This example contains three readable and writable attributes to control the radio operation in the mobile phone:

* Frequency, which represents the double type value of the frequency of the currently playing radio station. * PowerMode 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 type double, which is used to indicate the received signal strength (RSSI). The range of the attribute value has not been specified, but from the results of my test, it is a value between 0 and 1, where 0 means no signal and 1 means the strongest signal.

Why doesn’t the radio work when the phone is connected to the PC via Zune? The Zune software running on the desktop will lock the media library, which makes programs that require ID_CAP_MEDIALIB capabilities unable to work normally (similarly, we cannot use the Marketplace application when the phone is connected to Zune). If we need to test a program with this function, we can use the "Windows Phone Connect Tool" tool, which is included in the "Windows Phone Developer Tools", so that the PC can be connected to the mobile phone without running Zune. .

Due to the limitation of this function, our users have to close Zune or disconnect from the phone. But the best way is to detect this situation in the program, when it happens, we know the reason for this phenomenon. The Local FM Radio application detects this situation in the StartRadio method. It assumes that the failure of the PowerMode setting is due to the connection of the phone to Zune, which is a safe assumption.

Another way to detect this situation is to check whether the NetworkInterface. InterfaceType property value (located in the Microsoft.Phone.Net.NetworkInformation namespace) is Ethernet. It only changes to Ethernet after establishing a connection with 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 a background thread.

➔Because the signal strength value of the radio is variable, but it does not have radio-related events, the constructor of the main page uses a timer to refresh the signal strength value every two seconds. Although this cycle method consumes more power, in general, users will not run the application for a long time. That's because after the application exits, the radio still keeps working (importantly, the application stops running when the screen is locked). The constructor also initializes the FM frequency to the FM frequency value set last time.

The Frequency property in FMRadio is a global setting! If we change the radio station, this will affect the built-in radio application (and 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, there is no need to remember the last radio frequency (in fact, the Local FM Radio application does not store any data in the isolated storage space). On the other hand, if we want the application to be isolated from the built-in radio of the phone, then we need to do some extra work. The two methods StartRadio and StopRadio can change the value of the PowerMode property. 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 headset cord is not plugged in, because for current mobile phones, the headset cord acts as a receiving antenna for FM broadcasting.

Figure 32.2 When there is no headset cable connected to the mobile phone, the application cannot obtain any FM signal.

The PowerMode property of FMRadio has a bug in Windows Phone 7.0! On some mobile phones, after setting PowerMode to Off, the radio will stop for a short time, but then, even if PowerMode is not set back, the radio is still working. This makes it impossible to realize a button that shows synchronization with the radio status. On the contrary, the power button in the application behaves very strangely. In this case, if the radio fails to work due to the connection to Zune when the application is started, then 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 local radio frequency. If this is implemented through user logic, it is somewhat complicated. Therefore, this application adopts the simplest method, which is to change the displayed frequency value to red when an abnormality occurs (of course, if the current theme color is red, the effect will not be visible).

➔ After the user leaves the application (while the application is still running), we can still control the radio by clicking the volume-up or volume-down button. The application will now pop up an interface at the top of the screen, as shown in Figure 32.3. Interestingly, we can also search for the previous/next station through the back or forward button, so if we press the above two buttons during the operation of Local FM Radio, the currently playing station will be displayed The frequency values ​​of are inconsistent. Although this application can detect and verify this situation through the Tick event handler of its timer, this is not a big problem.

Figure 32.3 In addition to changing the volume, the volume control interface can also be used to turn on or stop the radio. You can even search for the previous or next station based on the strength of the received signal.

Can we get the calling number, name, or "currently playing program" information of the currently playing radio station? The answer is no. Although the built-in radio application can display this information, judging from the APIs currently open to developers, we cannot do this.

Reference: https://cloud.tencent.com/developer/article/1017628 "101 Windows Phone 7 Apps" reading notes-Local FM Radio-Cloud + Community-Tencent Cloud