This project has moved and is read-only. For the latest updates, please go here.

Incompatibility with FMRadio

Jan 26, 2015 at 8:39 AM

I'm running the latest commit (1.4.5) and having trouble with the FM Radio. The steps to reproduce the bug are these:
  • Open the project named "BackgroundAudio.Sample.WP8"
  • Modify the "pauseButton_Click" to actually stop playing by setting to null the track instead of simply pausing (just to be sure!)
  • Add a button on the xaml that does the following:
if (FMRadio.Instance.PowerMode == RadioPowerMode.On)
    FMRadio.Instance.PowerMode = RadioPowerMode.Off;
    FMRadio.Instance.CurrentRegion = RadioRegion.Europe; //whatever you like
    FMRadio.Instance.Frequency = 102.5; //whatever you like
    FMRadio.Instance.PowerMode = RadioPowerMode.On;
Now start playing a stream and stop it after a while by pressing the pause button.
Let the library close everything gracefully (until you see MediaStreamFacadeBase.Dispose(True) ).
And then finally start up the FM Radio by tapping on the previously created button. The FM Radio will start but you'll see tons of System.Runtime.InteropServices.COMException exceptions being thrown. You will no longer be able to start the stream back, not until you restart the app :(
Jan 27, 2015 at 1:27 AM
Yikes. It looks like FM radio isn't really supported, although it looks like one can make it work.

As a test, have you tried this in something like HlsView to see if it might have something to do with background audio? (This way, at least the FM radio stuff and the phonesm code would be running in the same process.)
Jan 27, 2015 at 9:07 AM
Hi Henric,

actually the FM is fully supported, that first bit of information is outdated and was true until 8.0 GDR 2 (see this , this). Moreover, it is stated that
On Windows Phone 8 devices with a build of 8.0.10322 or greater, the run-time exception may not occur if you use the FMRadio API. However, the FMRadio API is not supported for apps that target Windows Phone OS 8.0.
Please note that 8.0.10322 is GDR2, confirming my thesis. I acknowledge that it could have been written better but what MS is basically trying to say is that WP8.0 vanilla (no gdr updates) does not support FM Radio but said feature has been enabled from GDR2 going forward.

I don't think it's a matter of execution thread (since it's a static class that runs independently of the app) but of course I'm totally going to try your suggestion. Please note that the FM radio was working right up until 2015 (I update regularly from the source) :)

Have a great day Henric and thank you again for your work.

Jan 28, 2015 at 5:07 AM
You're welcome.

They did add support for playing FM radio in Update 2, but is the API for doing so programmatically supported in apps that target WP8 or later? In that "Caution" box": the say that, "the run-time exception may not occur." While the page title is, "How to set up and tune the FM radio for Windows Phone 8," the first sentence says that it applies to apps targeting WP 7.1. From that, one might want to try targeting WP7.1 (BackgroundAudio.Sample.WP7). The phonesm stuff falls back to the TaskEx PCL TPL and who knows what else happens under the hood when WP8/8.1 gets a WP7.1 app, but it should still play background audio on a WP8 device. The "Development Considerations" section has a few other points, including one I'm not quite sure how to interpret: "Avoid setting up the FM Radio or synchronizing the UI thread while the app is running." At any rate, the other points suggests there may be some timing constraints or races to worry about. One might try putting in a delay after enabling the radio (per the, "Delay sending further commands to the FM Radio until at least one second after the FM Radio is enabled," bit). A background thread might work better for managing the radio, or it might not work at all (if it needs the UI thread).

I'm not saying it can't be made to work, just that one might expect some rough edges (assuming the store doesn't reject the app).

Do you know approximately when the FM radio stuff stopped working for you? Have there been any OS updates in your test environment since? How about any Visual Studio updates? Also, have you tried on different OS builds to see if the symptoms are different (I tend to use whatever happens to be on my phone, "Emulator 8.1 U1 WVGA 4 inch 512MB" and "Emulator 8.0 Update 3 WVGA 512MB")? Do old builds of your app work? How about builds that are from older source code in your current build environment?
Jan 28, 2015 at 8:25 AM
Thing is, with WP8.x MS has lifted the FM Radio requirement for new smartphone. That's why they warn you that it might throw that exception. Now it's been some time since the introduction of 8.0 so I'm having difficulties finding the official statements. However, in the Caution box you've linked they say that the FM Radio is supported only in 7.1 apps and if you use it in WP8 it will throw the exception if it's not supported, while on GDR2 + it could not throw it because the SW/FW support is there and the manufacturer decided to implement said feature.

I'm doing my testing on a 1520 DP (the FW is not denim btw, it's just cyan while the OS is 8.1 GDR1) and have a couple of testers with stock 8.1 cyan (925). The strange thing is that I had a fairly stable build using PhoneSM (1b3cb21dcd53, 19th Dec 14) where the FM solidly worked and then, out of the blue, with 2015 stopped working. I am going to do a rollback up to that build and test it again thoroughly. No OS updates in between.

I guess I'm just going to abandon the FM stuff since it's not even supported in Universal / WinRT runtime.

Feb 24, 2015 at 2:51 PM
Edited Feb 24, 2015 at 2:52 PM
Just for the records, I've been able to isolate the problem and fix it once and for all.
To whom it might concern, the problem was that I was calling BackgroundAudioPlayer from a different thread than the main one. I was calling the FMRadio from the same UI thread and the fact that I was stopping the BGA in one thread and starting the FMRadio on another thread caused the System.Runtime.InteropServices.COMException .

I also wrapped all the BGA's methods with safe calls (between try catch).

FMRadio is correctly working in WP8.1.1 Denim on a 1520, WP8.1 Cyan on a 925 and many others. Sadly still no love for the FM in the WinRT runtime ATM.
Marked as answer by mmischitelli on 2/24/2015 at 6:51 AM