Code For Check Headphone Jack is Connected form Windows Phone Device or Not

MainPage.Xaml

  1. <phone:PhoneApplicationPage x:Class="TstApp.MainPage" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone" xmlns:shell="clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:drawerLayout="using:DrawerLayout" mc:Ignorable="d" FontFamily="{StaticResource PhoneFontFamilyNormal}" FontSize="{StaticResource PhoneFontSizeNormal}" Foreground="{StaticResource PhoneForegroundBrush}" SupportedOrientations="Portrait" Orientation="Portrait" shell:SystemTray.IsVisible="True">  
  2.     <Grid x:Name="LayoutRoot" Background="LightGray">  
  3.         <Grid.RowDefinitions>  
  4.             <RowDefinition Height="Auto" />  
  5.             <RowDefinition Height="*" /> </Grid.RowDefinitions>  
  6.         <MediaElement x:Name="playbackElement1" Source="basictone.mp3" AutoPlay="False"></MediaElement>  
  7.         <TextBlock Name="scanComp" Foreground="BlueViolet" TextAlignment="Center" FontSize="30" Text="..." Height="95" Margin="0,53,0,352"></TextBlock>  
  8.         <TextBlock Name="scanComp1" Foreground="BlueViolet" TextAlignment="Center" FontSize="24" Text="..." Height="95"></TextBlock>  
  9.         <TextBlock Name="scanComp2" Foreground="BlueViolet" TextAlignment="Center" FontSize="30" Text="0" Height="95" Margin="0,386,0,19"></TextBlock>  
  10.         <Popup VerticalOffset="10" x:Name="JackPopup" IsOpen="False" Margin="28,235,62,265">  
  11.             <Border BorderThickness="2" Width="387" Height="254" Margin="0,0,0,0">  
  12.                 <StackPanel Background="Gray" VerticalAlignment="Center" Height="216" Margin="12,17,-12,17">  
  13.                     <StackPanel Margin="14,0,-4,0" Orientation="Horizontal">  
  14.                         <TextBlock Text="Alert" Margin="30,2,2,10" HorizontalAlignment="Left" FontSize="36" Height="51" Width="189" /> </StackPanel>  
  15.                     <TextBlock Text="Unable to detect headphone. Plese connect and click Retry." Margin="0,10,0,0" TextWrapping="Wrap" FontSize="22" HorizontalAlignment="Right" Width="332" Height="67" />  
  16.                     <StackPanel Orientation="Horizontal" HorizontalAlignment="Right" Margin="20,0,2,2" Height="69">  
  17.                         <Button Content="Skip" Click="SkipJackPopupClicked" Width="150" />  
  18.                         <Button Content="Retry" Click="RetryJackPopupClicked" Width="150" /> </StackPanel>  
  19.                 </StackPanel>  
  20.             </Border>  
  21.         </Popup>  
  22.         <Popup VerticalOffset="10" x:Name="EarpiecePopup" IsOpen="False" Margin="28,235,62,265">  
  23.             <Border BorderThickness="2" Width="387" Height="254" Margin="0,0,0,0">  
  24.                 <StackPanel Background="Gray" VerticalAlignment="Center" Height="216">  
  25.                     <StackPanel Margin="14,0,-4,0" Orientation="Horizontal">  
  26.                         <TextBlock Text="Alert" Margin="30,2,2,10" HorizontalAlignment="Left" FontSize="36" Height="51" Width="189" /> </StackPanel>  
  27.                     <TextBlock Text="Unable to detect headphone. Please connect and click Retry." Margin="0,10,0,0" TextWrapping="Wrap" FontSize="22" HorizontalAlignment="Right" Width="332" Height="67" />  
  28.                     <StackPanel Orientation="Horizontal" HorizontalAlignment="Right" Margin="20,0,2,2" Height="69">  
  29.                         <Button Content="Skip" Click="SkipEarpiecePopupClicked" Width="150" />  
  30.                         <Button Content="Retry" Click="RetryEarpiecePopupClicked" Width="150" /> </StackPanel>  
  31.                 </StackPanel>  
  32.             </Border>  
  33.         </Popup>  
  34.         <Popup VerticalOffset="10" x:Name="EarpieceSoundPopup" IsOpen="False" Margin="28,235,62,265">  
  35.             <Border BorderThickness="2" Width="387" Height="254" Margin="0,0,0,0">  
  36.                 <StackPanel Background="Gray" VerticalAlignment="Center" Height="216">  
  37.                     <StackPanel Margin="14,0,-4,0" Orientation="Horizontal">  
  38.                         <TextBlock Text="Earpiece Audio" Margin="30,2,2,10" HorizontalAlignment="Left" FontSize="36" Height="51" Width="244" /> </StackPanel>  
  39.                     <TextBlock Text="Where you able to hear audio clearly? Plese open the earpiece jack and click Yes/No." Margin="0,5,0,0" TextWrapping="Wrap" FontSize="22" HorizontalAlignment="Right" Width="332" Height="91" />  
  40.                     <StackPanel Orientation="Horizontal" HorizontalAlignment="Right" Margin="20,0,2,2" Height="69">  
  41.                         <Button Content="Yes" Click="YesPopupClicked" Width="150" />  
  42.                         <Button Content="No" Click="NoPopupClicked" Width="150" /> </StackPanel>  
  43.                 </StackPanel>  
  44.             </Border>  
  45.         </Popup>  
  46.         <Popup VerticalOffset="10" x:Name="LoudspeakerSoundPopup" IsOpen="False" Margin="28,235,62,265">  
  47.             <Border BorderThickness="2" Width="387" Height="254" Margin="0,0,0,0">  
  48.                 <StackPanel Background="Gray" VerticalAlignment="Center" Height="216">  
  49.                     <StackPanel Margin="14,0,-4,0" Orientation="Horizontal">  
  50.                         <TextBlock Text="Speaker Audio" Margin="30,2,2,10" HorizontalAlignment="Left" FontSize="36" Height="51" Width="259" /> </StackPanel>  
  51.                     <TextBlock Text="Where you able to hear audio clearly?" Margin="0,10,0,0" TextWrapping="Wrap" FontSize="22" HorizontalAlignment="Right" Width="332" Height="67" />  
  52.                     <StackPanel Orientation="Horizontal" HorizontalAlignment="Right" Margin="20,0,2,2" Height="69">  
  53.                         <Button Content="Yes" Click="LoudspeakerYesPopupClicked" Width="150" />  
  54.                         <Button Content="No" Click="LoudspeakerNoPopupClicked" Width="150" /> </StackPanel>  
  55.                 </StackPanel>  
  56.             </Border>  
  57.         </Popup>  
  58.     </Grid>  
  59. </phone:PhoneApplicationPage>  
MainPage.Xaml.cs

  1. namespace TstApp {  
  2.     public partial class MainPage: PhoneApplicationPage {  
  3.         DispatcherTimer headphoneTimer;  
  4.         DispatcherTimer speakerTimer;  
  5.         int currentcount = 0;  
  6.         public MainPage() {  
  7.             InitializeComponent();  
  8.             AudioRoutingManager.GetDefault().AudioEndpointChanged += AudioEndpointChanged;  
  9.             var roamingSettings = Windows.Storage.ApplicationData.Current.RoamingSettings;  
  10.             roamingSettings.Values["cntearpiece"] = 0;  
  11.             roamingSettings.Values["cntJack"] = 0;  
  12.             headphoneTimer = new DispatcherTimer();  
  13.             headphoneTimer.Interval = new TimeSpan(0, 0, 0, 1, 0);  
  14.             headphoneTimer.Tick += new EventHandler(headphoneTimer_Tick);  
  15.             speakerTimer = new DispatcherTimer();  
  16.             speakerTimer.Interval = new TimeSpan(0, 0, 0, 1, 0);  
  17.             speakerTimer.Tick += new EventHandler(speakerTimer_Tick);  
  18.             checkHeadphoneJack();  
  19.         }  
  20.         private void headphoneTimer_Tick(object sender, EventArgs e) {  
  21.             scanComp2.Text = currentcount--.ToString();  
  22.             string str = Convert.ToString(AudioRoutingManager.GetDefault().GetAudioEndpoint());  
  23.             if (str != "Speakerphone") {  
  24.                 headphoneTimer.Stop();  
  25.                 EarpiecePopup.IsOpen = true;  
  26.                 ScanHeadphneSpeakerStart();  
  27.             }  
  28.             if (currentcount <= 0) {  
  29.                 var roamingSettings = Windows.Storage.ApplicationData.Current.RoamingSettings;  
  30.                 object cntJack = roamingSettings.Values["cntJack"];  
  31.                 if (Convert.ToInt32(cntJack) == 1) {  
  32.                     headphoneTimer.Stop();  
  33.                     ScanHeadphneSpeakerStart();  
  34.                 } else {  
  35.                     headphoneTimer.Stop();  
  36.                     JackPopup.IsOpen = true;  
  37.                 }  
  38.             }  
  39.         }  
  40.         private void speakerTimer_Tick(object sender, EventArgs e) {  
  41.             scanComp2.Text = currentcount--.ToString();  
  42.             string str = Convert.ToString(AudioRoutingManager.GetDefault().GetAudioEndpoint());  
  43.             if (str != "Speakerphone") {  
  44.                 speakerTimer.Stop();  
  45.                 EarpiecePopup.IsOpen = true;  
  46.                 ScanHeadphneSpeakerStart();  
  47.             }  
  48.             if (currentcount <= 0) {  
  49.                 var roamingSettings = Windows.Storage.ApplicationData.Current.RoamingSettings;  
  50.                 object cntearpiece = roamingSettings.Values["cntearpiece"];  
  51.                 if (Convert.ToInt32(cntearpiece) == 1) {  
  52.                     speakerTimer.Stop();  
  53.                     ScanHeadphneSpeakerStart();  
  54.                 } else {  
  55.                     speakerTimer.Stop();  
  56.                     EarpiecePopup.IsOpen = true;  
  57.                 }  
  58.             }  
  59.         }  
  60.         public void AudioEndpointChanged(AudioRoutingManager sender, object args) {  
  61.             // ScanHeadphneSpeakerStart();  
  62.         }  
  63.         protected void checkHeadphoneJack() {  
  64.             scanComp.Text = "Scaning Headphone jack";  
  65.             string str = Convert.ToString(AudioRoutingManager.GetDefault().GetAudioEndpoint());  
  66.             if (str == "Speakerphone") {  
  67.                 scanComp1.Text = "Please connect 3.5 mm jack.";  
  68.                 currentcount = 10;  
  69.                 headphoneTimer.Start();  
  70.             } else {  
  71.                 ScanHeadphneSpeakerStart();  
  72.             }  
  73.         }  
  74.         protected async void ScanHeadphneSpeakerStart() {  
  75.             scanComp.Text = "Scanning Earpiece";  
  76.             string str = Convert.ToString(AudioRoutingManager.GetDefault().GetAudioEndpoint());  
  77.             var roamingSettings = Windows.Storage.ApplicationData.Current.RoamingSettings;  
  78.             object cntearpiece = roamingSettings.Values["cntearpiece"];  
  79.             if (str == "Speakerphone" && Convert.ToInt32(cntearpiece) == 0) {  
  80.                 scanComp1.Text = "Please attach headphone jack.";  
  81.                 currentcount = 10;  
  82.                 speakerTimer.Start();  
  83.             } else if (str == "Speakerphone" && Convert.ToInt32(cntearpiece) == 1) {  
  84.                 loudSpeakerStart();  
  85.                 // Code for Earpiece slound fail.  
  86.             } else {  
  87.                 playbackElement1.Stop();  
  88.                 playbackElement1.AutoPlay = true;  
  89.                 playbackElement1.Play();  
  90.                 await Task.Delay(TimeSpan.FromSeconds(1));  
  91.                 EarpieceSoundPopup.IsOpen = true;  
  92.             }  
  93.         }  
  94.         protected async void playEarpeceSound() {  
  95.             playbackElement1.Stop();  
  96.             playbackElement1.AutoPlay = true;  
  97.             playbackElement1.Play();  
  98.             await Task.Delay(TimeSpan.FromSeconds(1));  
  99.             EarpieceSoundPopup.IsOpen = true;  
  100.         }  
  101.         protected async void loudSpeakerStart() {  
  102.             playbackElement1.Stop();  
  103.             playbackElement1.AutoPlay = false;  
  104.             await Task.Delay(TimeSpan.FromSeconds(5));  
  105.             playbackElement1.AutoPlay = true;  
  106.             playbackElement1.Play();  
  107.             await Task.Delay(TimeSpan.FromSeconds(3));  
  108.             LoudspeakerSoundPopup.IsOpen = true;  
  109.         }  
  110.         protected void SkipJackPopupClicked(object sender, RoutedEventArgs e) {  
  111.             JackPopup.IsOpen = false;  
  112.             headphoneTimer.Stop();  
  113.             ScanHeadphneSpeakerStart();  
  114.         }  
  115.         protected void RetryJackPopupClicked(object sender, RoutedEventArgs e) {  
  116.             JackPopup.IsOpen = false;  
  117.             var roamingSettings = Windows.Storage.ApplicationData.Current.RoamingSettings;  
  118.             roamingSettings.Values["cntJack"] = 1;  
  119.             currentcount = 10;  
  120.             headphoneTimer.Start();  
  121.         }  
  122.         protected void YesPopupClicked(object sender, RoutedEventArgs e) {  
  123.             EarpieceSoundPopup.IsOpen = false;  
  124.             loudSpeakerStart();  
  125.         }  
  126.         protected void NoPopupClicked(object sender, RoutedEventArgs e) {  
  127.             EarpieceSoundPopup.IsOpen = false;  
  128.             loudSpeakerStart();  
  129.         }  
  130.         protected void SkipEarpiecePopupClicked(object sender, RoutedEventArgs e) {  
  131.             EarpiecePopup.IsOpen = false;  
  132.             speakerTimer.Stop();  
  133.             ScanHeadphneSpeakerStart();  
  134.         }  
  135.         protected void RetryEarpiecePopupClicked(object sender, RoutedEventArgs e) {  
  136.             EarpiecePopup.IsOpen = false;  
  137.             var roamingSettings = Windows.Storage.ApplicationData.Current.RoamingSettings;  
  138.             roamingSettings.Values["cntearpiece"] = 1;  
  139.             currentcount = 10;  
  140.             speakerTimer.Start();  
  141.         }  
  142.         protected void LoudspeakerYesPopupClicked(object sender, RoutedEventArgs e) {  
  143.             LoudspeakerSoundPopup.IsOpen = false;  
  144.             NavigationService.Navigate(new Uri("/Page1.xaml", UriKind.Relative));  
  145.         }  
  146.         protected void LoudspeakerNoPopupClicked(object sender, RoutedEventArgs e) {  
  147.             LoudspeakerSoundPopup.IsOpen = false;  
  148.             NavigationService.Navigate(new Uri("/Page1.xaml", UriKind.Relative));  
  149.         }  
  150.     }  
  151. }