TECHNOLOGIES
FORUMS
JOBS
BOOKS
EVENTS
INTERVIEWS
Live
MORE
LEARN
Training
CAREER
MEMBERS
VIDEOS
NEWS
BLOGS
Sign Up
Login
No unread comment.
View All Comments
No unread message.
View All Messages
No unread notification.
View All Notifications
C# Corner
Post
An Article
A Blog
A News
A Video
An EBook
An Interview Question
Ask Question
Code For Check Headphone Jack is Connected form Windows Phone Device or Not
Kumar Bhimsen
Dec 24
2015
Code
2.7
k
0
0
facebook
twitter
linkedIn
Reddit
WhatsApp
Email
Bookmark
expand
HeadphonejackHeadpho
MainPage.Xaml
<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"
>
<Grid x:Name=
"LayoutRoot"
Background=
"LightGray"
>
<Grid.RowDefinitions>
<RowDefinition Height=
"Auto"
/>
<RowDefinition Height=
"*"
/> </Grid.RowDefinitions>
<MediaElement x:Name=
"playbackElement1"
Source=
"basictone.mp3"
AutoPlay=
"False"
></MediaElement>
<TextBlock Name=
"scanComp"
Foreground=
"BlueViolet"
TextAlignment=
"Center"
FontSize=
"30"
Text=
"..."
Height=
"95"
Margin=
"0,53,0,352"
></TextBlock>
<TextBlock Name=
"scanComp1"
Foreground=
"BlueViolet"
TextAlignment=
"Center"
FontSize=
"24"
Text=
"..."
Height=
"95"
></TextBlock>
<TextBlock Name=
"scanComp2"
Foreground=
"BlueViolet"
TextAlignment=
"Center"
FontSize=
"30"
Text=
"0"
Height=
"95"
Margin=
"0,386,0,19"
></TextBlock>
<Popup VerticalOffset=
"10"
x:Name=
"JackPopup"
IsOpen=
"False"
Margin=
"28,235,62,265"
>
<Border BorderThickness=
"2"
Width=
"387"
Height=
"254"
Margin=
"0,0,0,0"
>
<StackPanel Background=
"Gray"
VerticalAlignment=
"Center"
Height=
"216"
Margin=
"12,17,-12,17"
>
<StackPanel Margin=
"14,0,-4,0"
Orientation=
"Horizontal"
>
<TextBlock Text=
"Alert"
Margin=
"30,2,2,10"
HorizontalAlignment=
"Left"
FontSize=
"36"
Height=
"51"
Width=
"189"
/> </StackPanel>
<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"
/>
<StackPanel Orientation=
"Horizontal"
HorizontalAlignment=
"Right"
Margin=
"20,0,2,2"
Height=
"69"
>
<Button Content=
"Skip"
Click=
"SkipJackPopupClicked"
Width=
"150"
/>
<Button Content=
"Retry"
Click=
"RetryJackPopupClicked"
Width=
"150"
/> </StackPanel>
</StackPanel>
</Border>
</Popup>
<Popup VerticalOffset=
"10"
x:Name=
"EarpiecePopup"
IsOpen=
"False"
Margin=
"28,235,62,265"
>
<Border BorderThickness=
"2"
Width=
"387"
Height=
"254"
Margin=
"0,0,0,0"
>
<StackPanel Background=
"Gray"
VerticalAlignment=
"Center"
Height=
"216"
>
<StackPanel Margin=
"14,0,-4,0"
Orientation=
"Horizontal"
>
<TextBlock Text=
"Alert"
Margin=
"30,2,2,10"
HorizontalAlignment=
"Left"
FontSize=
"36"
Height=
"51"
Width=
"189"
/> </StackPanel>
<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"
/>
<StackPanel Orientation=
"Horizontal"
HorizontalAlignment=
"Right"
Margin=
"20,0,2,2"
Height=
"69"
>
<Button Content=
"Skip"
Click=
"SkipEarpiecePopupClicked"
Width=
"150"
/>
<Button Content=
"Retry"
Click=
"RetryEarpiecePopupClicked"
Width=
"150"
/> </StackPanel>
</StackPanel>
</Border>
</Popup>
<Popup VerticalOffset=
"10"
x:Name=
"EarpieceSoundPopup"
IsOpen=
"False"
Margin=
"28,235,62,265"
>
<Border BorderThickness=
"2"
Width=
"387"
Height=
"254"
Margin=
"0,0,0,0"
>
<StackPanel Background=
"Gray"
VerticalAlignment=
"Center"
Height=
"216"
>
<StackPanel Margin=
"14,0,-4,0"
Orientation=
"Horizontal"
>
<TextBlock Text=
"Earpiece Audio"
Margin=
"30,2,2,10"
HorizontalAlignment=
"Left"
FontSize=
"36"
Height=
"51"
Width=
"244"
/> </StackPanel>
<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"
/>
<StackPanel Orientation=
"Horizontal"
HorizontalAlignment=
"Right"
Margin=
"20,0,2,2"
Height=
"69"
>
<Button Content=
"Yes"
Click=
"YesPopupClicked"
Width=
"150"
/>
<Button Content=
"No"
Click=
"NoPopupClicked"
Width=
"150"
/> </StackPanel>
</StackPanel>
</Border>
</Popup>
<Popup VerticalOffset=
"10"
x:Name=
"LoudspeakerSoundPopup"
IsOpen=
"False"
Margin=
"28,235,62,265"
>
<Border BorderThickness=
"2"
Width=
"387"
Height=
"254"
Margin=
"0,0,0,0"
>
<StackPanel Background=
"Gray"
VerticalAlignment=
"Center"
Height=
"216"
>
<StackPanel Margin=
"14,0,-4,0"
Orientation=
"Horizontal"
>
<TextBlock Text=
"Speaker Audio"
Margin=
"30,2,2,10"
HorizontalAlignment=
"Left"
FontSize=
"36"
Height=
"51"
Width=
"259"
/> </StackPanel>
<TextBlock Text=
"Where you able to hear audio clearly?"
Margin=
"0,10,0,0"
TextWrapping=
"Wrap"
FontSize=
"22"
HorizontalAlignment=
"Right"
Width=
"332"
Height=
"67"
/>
<StackPanel Orientation=
"Horizontal"
HorizontalAlignment=
"Right"
Margin=
"20,0,2,2"
Height=
"69"
>
<Button Content=
"Yes"
Click=
"LoudspeakerYesPopupClicked"
Width=
"150"
/>
<Button Content=
"No"
Click=
"LoudspeakerNoPopupClicked"
Width=
"150"
/> </StackPanel>
</StackPanel>
</Border>
</Popup>
</Grid>
</phone:PhoneApplicationPage>
MainPage.Xaml.cs
namespace
TstApp {
public
partial
class
MainPage: PhoneApplicationPage {
DispatcherTimer headphoneTimer;
DispatcherTimer speakerTimer;
int
currentcount = 0;
public
MainPage() {
InitializeComponent();
AudioRoutingManager.GetDefault().AudioEndpointChanged += AudioEndpointChanged;
var roamingSettings = Windows.Storage.ApplicationData.Current.RoamingSettings;
roamingSettings.Values[
"cntearpiece"
] = 0;
roamingSettings.Values[
"cntJack"
] = 0;
headphoneTimer =
new
DispatcherTimer();
headphoneTimer.Interval =
new
TimeSpan(0, 0, 0, 1, 0);
headphoneTimer.Tick +=
new
EventHandler(headphoneTimer_Tick);
speakerTimer =
new
DispatcherTimer();
speakerTimer.Interval =
new
TimeSpan(0, 0, 0, 1, 0);
speakerTimer.Tick +=
new
EventHandler(speakerTimer_Tick);
checkHeadphoneJack();
}
private
void
headphoneTimer_Tick(
object
sender, EventArgs e) {
scanComp2.Text = currentcount--.ToString();
string
str = Convert.ToString(AudioRoutingManager.GetDefault().GetAudioEndpoint());
if
(str !=
"Speakerphone"
) {
headphoneTimer.Stop();
EarpiecePopup.IsOpen =
true
;
ScanHeadphneSpeakerStart();
}
if
(currentcount <= 0) {
var roamingSettings = Windows.Storage.ApplicationData.Current.RoamingSettings;
object
cntJack = roamingSettings.Values[
"cntJack"
];
if
(Convert.ToInt32(cntJack) == 1) {
headphoneTimer.Stop();
ScanHeadphneSpeakerStart();
}
else
{
headphoneTimer.Stop();
JackPopup.IsOpen =
true
;
}
}
}
private
void
speakerTimer_Tick(
object
sender, EventArgs e) {
scanComp2.Text = currentcount--.ToString();
string
str = Convert.ToString(AudioRoutingManager.GetDefault().GetAudioEndpoint());
if
(str !=
"Speakerphone"
) {
speakerTimer.Stop();
EarpiecePopup.IsOpen =
true
;
ScanHeadphneSpeakerStart();
}
if
(currentcount <= 0) {
var roamingSettings = Windows.Storage.ApplicationData.Current.RoamingSettings;
object
cntearpiece = roamingSettings.Values[
"cntearpiece"
];
if
(Convert.ToInt32(cntearpiece) == 1) {
speakerTimer.Stop();
ScanHeadphneSpeakerStart();
}
else
{
speakerTimer.Stop();
EarpiecePopup.IsOpen =
true
;
}
}
}
public
void
AudioEndpointChanged(AudioRoutingManager sender,
object
args) {
// ScanHeadphneSpeakerStart();
}
protected
void
checkHeadphoneJack() {
scanComp.Text =
"Scaning Headphone jack"
;
string
str = Convert.ToString(AudioRoutingManager.GetDefault().GetAudioEndpoint());
if
(str ==
"Speakerphone"
) {
scanComp1.Text =
"Please connect 3.5 mm jack."
;
currentcount = 10;
headphoneTimer.Start();
}
else
{
ScanHeadphneSpeakerStart();
}
}
protected
async
void
ScanHeadphneSpeakerStart() {
scanComp.Text =
"Scanning Earpiece"
;
string
str = Convert.ToString(AudioRoutingManager.GetDefault().GetAudioEndpoint());
var roamingSettings = Windows.Storage.ApplicationData.Current.RoamingSettings;
object
cntearpiece = roamingSettings.Values[
"cntearpiece"
];
if
(str ==
"Speakerphone"
&& Convert.ToInt32(cntearpiece) == 0) {
scanComp1.Text =
"Please attach headphone jack."
;
currentcount = 10;
speakerTimer.Start();
}
else
if
(str ==
"Speakerphone"
&& Convert.ToInt32(cntearpiece) == 1) {
loudSpeakerStart();
// Code for Earpiece slound fail.
}
else
{
playbackElement1.Stop();
playbackElement1.AutoPlay =
true
;
playbackElement1.Play();
await Task.Delay(TimeSpan.FromSeconds(1));
EarpieceSoundPopup.IsOpen =
true
;
}
}
protected
async
void
playEarpeceSound() {
playbackElement1.Stop();
playbackElement1.AutoPlay =
true
;
playbackElement1.Play();
await Task.Delay(TimeSpan.FromSeconds(1));
EarpieceSoundPopup.IsOpen =
true
;
}
protected
async
void
loudSpeakerStart() {
playbackElement1.Stop();
playbackElement1.AutoPlay =
false
;
await Task.Delay(TimeSpan.FromSeconds(5));
playbackElement1.AutoPlay =
true
;
playbackElement1.Play();
await Task.Delay(TimeSpan.FromSeconds(3));
LoudspeakerSoundPopup.IsOpen =
true
;
}
protected
void
SkipJackPopupClicked(
object
sender, RoutedEventArgs e) {
JackPopup.IsOpen =
false
;
headphoneTimer.Stop();
ScanHeadphneSpeakerStart();
}
protected
void
RetryJackPopupClicked(
object
sender, RoutedEventArgs e) {
JackPopup.IsOpen =
false
;
var roamingSettings = Windows.Storage.ApplicationData.Current.RoamingSettings;
roamingSettings.Values[
"cntJack"
] = 1;
currentcount = 10;
headphoneTimer.Start();
}
protected
void
YesPopupClicked(
object
sender, RoutedEventArgs e) {
EarpieceSoundPopup.IsOpen =
false
;
loudSpeakerStart();
}
protected
void
NoPopupClicked(
object
sender, RoutedEventArgs e) {
EarpieceSoundPopup.IsOpen =
false
;
loudSpeakerStart();
}
protected
void
SkipEarpiecePopupClicked(
object
sender, RoutedEventArgs e) {
EarpiecePopup.IsOpen =
false
;
speakerTimer.Stop();
ScanHeadphneSpeakerStart();
}
protected
void
RetryEarpiecePopupClicked(
object
sender, RoutedEventArgs e) {
EarpiecePopup.IsOpen =
false
;
var roamingSettings = Windows.Storage.ApplicationData.Current.RoamingSettings;
roamingSettings.Values[
"cntearpiece"
] = 1;
currentcount = 10;
speakerTimer.Start();
}
protected
void
LoudspeakerYesPopupClicked(
object
sender, RoutedEventArgs e) {
LoudspeakerSoundPopup.IsOpen =
false
;
NavigationService.Navigate(
new
Uri(
"/Page1.xaml"
, UriKind.Relative));
}
protected
void
LoudspeakerNoPopupClicked(
object
sender, RoutedEventArgs e) {
LoudspeakerSoundPopup.IsOpen =
false
;
NavigationService.Navigate(
new
Uri(
"/Page1.xaml"
, UriKind.Relative));
}
}
}
Play audio or mp3 sound in windows phone 8.1