Switching between phone speaker and wired headset
I'm a total novice with programming for Android. I am trying to build a multimedia player app and I wanted to add option to switch between wired headset and speaker on button press - while I know this option is rather redundant, project requirements force me to implement such functionality.
I already tried all the suggested ideas from this thread:
Is there any way to force audio through the speakers when headphones are plugged in?
I added permission MODIFY_AUDIO_SETTINGS to manifest.
I do believe my mistake may be caused by not fully understanding some of the mechanics, I'm using, but I thought it won't hurt to ask for help here, here's my code for player activity in my app:
package com.example.ja.multimedia_player;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.MediaController;
import android.widget.Toast;
import android.widget.VideoView;
import android.view.View;
import android.view.View.OnClickListener;
import java.util.ArrayList;
import android.media.AudioManager;
public class PlayActivity extends Activity {
String title2;
ArrayList<String> mediaList;
String indeks;
Integer licznik;
String blokada;
Long blok;
Button button;
private AudioManager myAudioManager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_play);
Button musicOutputDevice = (Button)findViewById (R.id.device); // deklaracja przycisku do zmiany trybu odtwarzania
//listener na guziku do zmiany urządzenia
musicOutputDevice.setOnClickListener (new OnClickListener() {
public void onClick(View v) {
private AudioManager m_amAudioManager;
m_amAudioManager = (AudioManager)getSystemService(Context.AUDIO_SERVICE);
m_amAudioManager.setMode(AudioManager.MODE_IN_CALL);
m_amAudioManager.setSpeakerphoneOn(false);
/* AudioSystem audioSystem = new AudioSystem ();
if (myAudioManager.isWiredHeadsetOn())
{myAudioManager.setSpeakerphoneOn(true);}
else
{myAudioManager.setWiredHeadsetOn(true);}
*/
}
});
VideoView myVideoView = (VideoView)findViewById(R.id.videoview);
//pobranie listy utworów
mediaList = (ArrayList<String>) getIntent().getSerializableExtra("lista");
//pobranie aktualnego utworu
title2 = getIntent().getStringExtra("musicPath");
String viewSource =title2;
Intent mIntent = getIntent();
indeks = getIntent().getStringExtra("indeks");
licznik = Integer.parseInt(indeks);
blokada = getIntent().getStringExtra("blokada");
//blok = Long.parseLong(blokada);
Toast.makeText(PlayActivity.this,
blokada,
Toast.LENGTH_LONG).show();
myVideoView.setVideoURI(Uri.parse(viewSource));
myVideoView.setMediaController(new MediaController(this));
myVideoView.setOnCompletionListener(myVideoViewCompletionListener);
myVideoView.setOnPreparedListener(MyVideoViewPreparedListener);
myVideoView.setOnErrorListener(myVideoViewErrorListener);
myVideoView.requestFocus();
myVideoView.start();
}
// nasłuch końca utworu
MediaPlayer.OnCompletionListener myVideoViewCompletionListener
= new MediaPlayer.OnCompletionListener(){
@Override
public void onCompletion(MediaPlayer arg0) {
Toast.makeText(PlayActivity.this,
"Koniec",
Toast.LENGTH_LONG).show();
title2=mediaList.get(licznik + 1);
VideoView myVideoView = (VideoView)findViewById(R.id.videoview);
myVideoView.setVideoURI(Uri.parse(title2));
myVideoView.setOnCompletionListener(myVideoViewCompletionListener);
myVideoView.setOnPreparedListener(MyVideoViewPreparedListener);
myVideoView.setOnErrorListener(myVideoViewErrorListener);
myVideoView.requestFocus();
myVideoView.start();
licznik = licznik + 1;
}};

add a comment |
I'm a total novice with programming for Android. I am trying to build a multimedia player app and I wanted to add option to switch between wired headset and speaker on button press - while I know this option is rather redundant, project requirements force me to implement such functionality.
I already tried all the suggested ideas from this thread:
Is there any way to force audio through the speakers when headphones are plugged in?
I added permission MODIFY_AUDIO_SETTINGS to manifest.
I do believe my mistake may be caused by not fully understanding some of the mechanics, I'm using, but I thought it won't hurt to ask for help here, here's my code for player activity in my app:
package com.example.ja.multimedia_player;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.MediaController;
import android.widget.Toast;
import android.widget.VideoView;
import android.view.View;
import android.view.View.OnClickListener;
import java.util.ArrayList;
import android.media.AudioManager;
public class PlayActivity extends Activity {
String title2;
ArrayList<String> mediaList;
String indeks;
Integer licznik;
String blokada;
Long blok;
Button button;
private AudioManager myAudioManager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_play);
Button musicOutputDevice = (Button)findViewById (R.id.device); // deklaracja przycisku do zmiany trybu odtwarzania
//listener na guziku do zmiany urządzenia
musicOutputDevice.setOnClickListener (new OnClickListener() {
public void onClick(View v) {
private AudioManager m_amAudioManager;
m_amAudioManager = (AudioManager)getSystemService(Context.AUDIO_SERVICE);
m_amAudioManager.setMode(AudioManager.MODE_IN_CALL);
m_amAudioManager.setSpeakerphoneOn(false);
/* AudioSystem audioSystem = new AudioSystem ();
if (myAudioManager.isWiredHeadsetOn())
{myAudioManager.setSpeakerphoneOn(true);}
else
{myAudioManager.setWiredHeadsetOn(true);}
*/
}
});
VideoView myVideoView = (VideoView)findViewById(R.id.videoview);
//pobranie listy utworów
mediaList = (ArrayList<String>) getIntent().getSerializableExtra("lista");
//pobranie aktualnego utworu
title2 = getIntent().getStringExtra("musicPath");
String viewSource =title2;
Intent mIntent = getIntent();
indeks = getIntent().getStringExtra("indeks");
licznik = Integer.parseInt(indeks);
blokada = getIntent().getStringExtra("blokada");
//blok = Long.parseLong(blokada);
Toast.makeText(PlayActivity.this,
blokada,
Toast.LENGTH_LONG).show();
myVideoView.setVideoURI(Uri.parse(viewSource));
myVideoView.setMediaController(new MediaController(this));
myVideoView.setOnCompletionListener(myVideoViewCompletionListener);
myVideoView.setOnPreparedListener(MyVideoViewPreparedListener);
myVideoView.setOnErrorListener(myVideoViewErrorListener);
myVideoView.requestFocus();
myVideoView.start();
}
// nasłuch końca utworu
MediaPlayer.OnCompletionListener myVideoViewCompletionListener
= new MediaPlayer.OnCompletionListener(){
@Override
public void onCompletion(MediaPlayer arg0) {
Toast.makeText(PlayActivity.this,
"Koniec",
Toast.LENGTH_LONG).show();
title2=mediaList.get(licznik + 1);
VideoView myVideoView = (VideoView)findViewById(R.id.videoview);
myVideoView.setVideoURI(Uri.parse(title2));
myVideoView.setOnCompletionListener(myVideoViewCompletionListener);
myVideoView.setOnPreparedListener(MyVideoViewPreparedListener);
myVideoView.setOnErrorListener(myVideoViewErrorListener);
myVideoView.requestFocus();
myVideoView.start();
licznik = licznik + 1;
}};

add a comment |
I'm a total novice with programming for Android. I am trying to build a multimedia player app and I wanted to add option to switch between wired headset and speaker on button press - while I know this option is rather redundant, project requirements force me to implement such functionality.
I already tried all the suggested ideas from this thread:
Is there any way to force audio through the speakers when headphones are plugged in?
I added permission MODIFY_AUDIO_SETTINGS to manifest.
I do believe my mistake may be caused by not fully understanding some of the mechanics, I'm using, but I thought it won't hurt to ask for help here, here's my code for player activity in my app:
package com.example.ja.multimedia_player;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.MediaController;
import android.widget.Toast;
import android.widget.VideoView;
import android.view.View;
import android.view.View.OnClickListener;
import java.util.ArrayList;
import android.media.AudioManager;
public class PlayActivity extends Activity {
String title2;
ArrayList<String> mediaList;
String indeks;
Integer licznik;
String blokada;
Long blok;
Button button;
private AudioManager myAudioManager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_play);
Button musicOutputDevice = (Button)findViewById (R.id.device); // deklaracja przycisku do zmiany trybu odtwarzania
//listener na guziku do zmiany urządzenia
musicOutputDevice.setOnClickListener (new OnClickListener() {
public void onClick(View v) {
private AudioManager m_amAudioManager;
m_amAudioManager = (AudioManager)getSystemService(Context.AUDIO_SERVICE);
m_amAudioManager.setMode(AudioManager.MODE_IN_CALL);
m_amAudioManager.setSpeakerphoneOn(false);
/* AudioSystem audioSystem = new AudioSystem ();
if (myAudioManager.isWiredHeadsetOn())
{myAudioManager.setSpeakerphoneOn(true);}
else
{myAudioManager.setWiredHeadsetOn(true);}
*/
}
});
VideoView myVideoView = (VideoView)findViewById(R.id.videoview);
//pobranie listy utworów
mediaList = (ArrayList<String>) getIntent().getSerializableExtra("lista");
//pobranie aktualnego utworu
title2 = getIntent().getStringExtra("musicPath");
String viewSource =title2;
Intent mIntent = getIntent();
indeks = getIntent().getStringExtra("indeks");
licznik = Integer.parseInt(indeks);
blokada = getIntent().getStringExtra("blokada");
//blok = Long.parseLong(blokada);
Toast.makeText(PlayActivity.this,
blokada,
Toast.LENGTH_LONG).show();
myVideoView.setVideoURI(Uri.parse(viewSource));
myVideoView.setMediaController(new MediaController(this));
myVideoView.setOnCompletionListener(myVideoViewCompletionListener);
myVideoView.setOnPreparedListener(MyVideoViewPreparedListener);
myVideoView.setOnErrorListener(myVideoViewErrorListener);
myVideoView.requestFocus();
myVideoView.start();
}
// nasłuch końca utworu
MediaPlayer.OnCompletionListener myVideoViewCompletionListener
= new MediaPlayer.OnCompletionListener(){
@Override
public void onCompletion(MediaPlayer arg0) {
Toast.makeText(PlayActivity.this,
"Koniec",
Toast.LENGTH_LONG).show();
title2=mediaList.get(licznik + 1);
VideoView myVideoView = (VideoView)findViewById(R.id.videoview);
myVideoView.setVideoURI(Uri.parse(title2));
myVideoView.setOnCompletionListener(myVideoViewCompletionListener);
myVideoView.setOnPreparedListener(MyVideoViewPreparedListener);
myVideoView.setOnErrorListener(myVideoViewErrorListener);
myVideoView.requestFocus();
myVideoView.start();
licznik = licznik + 1;
}};

I'm a total novice with programming for Android. I am trying to build a multimedia player app and I wanted to add option to switch between wired headset and speaker on button press - while I know this option is rather redundant, project requirements force me to implement such functionality.
I already tried all the suggested ideas from this thread:
Is there any way to force audio through the speakers when headphones are plugged in?
I added permission MODIFY_AUDIO_SETTINGS to manifest.
I do believe my mistake may be caused by not fully understanding some of the mechanics, I'm using, but I thought it won't hurt to ask for help here, here's my code for player activity in my app:
package com.example.ja.multimedia_player;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.MediaController;
import android.widget.Toast;
import android.widget.VideoView;
import android.view.View;
import android.view.View.OnClickListener;
import java.util.ArrayList;
import android.media.AudioManager;
public class PlayActivity extends Activity {
String title2;
ArrayList<String> mediaList;
String indeks;
Integer licznik;
String blokada;
Long blok;
Button button;
private AudioManager myAudioManager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_play);
Button musicOutputDevice = (Button)findViewById (R.id.device); // deklaracja przycisku do zmiany trybu odtwarzania
//listener na guziku do zmiany urządzenia
musicOutputDevice.setOnClickListener (new OnClickListener() {
public void onClick(View v) {
private AudioManager m_amAudioManager;
m_amAudioManager = (AudioManager)getSystemService(Context.AUDIO_SERVICE);
m_amAudioManager.setMode(AudioManager.MODE_IN_CALL);
m_amAudioManager.setSpeakerphoneOn(false);
/* AudioSystem audioSystem = new AudioSystem ();
if (myAudioManager.isWiredHeadsetOn())
{myAudioManager.setSpeakerphoneOn(true);}
else
{myAudioManager.setWiredHeadsetOn(true);}
*/
}
});
VideoView myVideoView = (VideoView)findViewById(R.id.videoview);
//pobranie listy utworów
mediaList = (ArrayList<String>) getIntent().getSerializableExtra("lista");
//pobranie aktualnego utworu
title2 = getIntent().getStringExtra("musicPath");
String viewSource =title2;
Intent mIntent = getIntent();
indeks = getIntent().getStringExtra("indeks");
licznik = Integer.parseInt(indeks);
blokada = getIntent().getStringExtra("blokada");
//blok = Long.parseLong(blokada);
Toast.makeText(PlayActivity.this,
blokada,
Toast.LENGTH_LONG).show();
myVideoView.setVideoURI(Uri.parse(viewSource));
myVideoView.setMediaController(new MediaController(this));
myVideoView.setOnCompletionListener(myVideoViewCompletionListener);
myVideoView.setOnPreparedListener(MyVideoViewPreparedListener);
myVideoView.setOnErrorListener(myVideoViewErrorListener);
myVideoView.requestFocus();
myVideoView.start();
}
// nasłuch końca utworu
MediaPlayer.OnCompletionListener myVideoViewCompletionListener
= new MediaPlayer.OnCompletionListener(){
@Override
public void onCompletion(MediaPlayer arg0) {
Toast.makeText(PlayActivity.this,
"Koniec",
Toast.LENGTH_LONG).show();
title2=mediaList.get(licznik + 1);
VideoView myVideoView = (VideoView)findViewById(R.id.videoview);
myVideoView.setVideoURI(Uri.parse(title2));
myVideoView.setOnCompletionListener(myVideoViewCompletionListener);
myVideoView.setOnPreparedListener(MyVideoViewPreparedListener);
myVideoView.setOnErrorListener(myVideoViewErrorListener);
myVideoView.requestFocus();
myVideoView.start();
licznik = licznik + 1;
}};


edited Nov 21 '18 at 12:33


Fantômas
32.6k156389
32.6k156389
asked Nov 21 '18 at 12:15
PaladinRagePaladinRage
61
61
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
Ok I managed to crack this one on my own, but I will post the part that was wrong, so someone with similliar problem might use it.
I was trying to use an example for application that wasn't a media player, thus set(AudioManager.MODE_IN_CALL)
was not right for this.
Correct code for my onClickListener is:
public void onClick (View v) {
AudioManager myAudioManager;
myAudioManager = (AudioManager)getSystemService(Context.AUDIO_SERVICE);
myAudioManager.setMode(AudioManager.STREAM_MUSIC);
if (myAudioManager.isWiredHeadsetOn())
{musicOutputDevice.setText("Słuchawki");
myAudioManager.setSpeakerphoneOn(true);}
else
{musicOutputDevice.setText("Głośnik");
myAudioManager.setSpeakerphoneOn(false);
myAudioManager.setWiredHeadsetOn(true);}
Anyway, thank you guys for posting so many useful advices in other threads! This page is awesome! Hope I'll learn even more while being here :)
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53411830%2fswitching-between-phone-speaker-and-wired-headset%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
Ok I managed to crack this one on my own, but I will post the part that was wrong, so someone with similliar problem might use it.
I was trying to use an example for application that wasn't a media player, thus set(AudioManager.MODE_IN_CALL)
was not right for this.
Correct code for my onClickListener is:
public void onClick (View v) {
AudioManager myAudioManager;
myAudioManager = (AudioManager)getSystemService(Context.AUDIO_SERVICE);
myAudioManager.setMode(AudioManager.STREAM_MUSIC);
if (myAudioManager.isWiredHeadsetOn())
{musicOutputDevice.setText("Słuchawki");
myAudioManager.setSpeakerphoneOn(true);}
else
{musicOutputDevice.setText("Głośnik");
myAudioManager.setSpeakerphoneOn(false);
myAudioManager.setWiredHeadsetOn(true);}
Anyway, thank you guys for posting so many useful advices in other threads! This page is awesome! Hope I'll learn even more while being here :)
add a comment |
Ok I managed to crack this one on my own, but I will post the part that was wrong, so someone with similliar problem might use it.
I was trying to use an example for application that wasn't a media player, thus set(AudioManager.MODE_IN_CALL)
was not right for this.
Correct code for my onClickListener is:
public void onClick (View v) {
AudioManager myAudioManager;
myAudioManager = (AudioManager)getSystemService(Context.AUDIO_SERVICE);
myAudioManager.setMode(AudioManager.STREAM_MUSIC);
if (myAudioManager.isWiredHeadsetOn())
{musicOutputDevice.setText("Słuchawki");
myAudioManager.setSpeakerphoneOn(true);}
else
{musicOutputDevice.setText("Głośnik");
myAudioManager.setSpeakerphoneOn(false);
myAudioManager.setWiredHeadsetOn(true);}
Anyway, thank you guys for posting so many useful advices in other threads! This page is awesome! Hope I'll learn even more while being here :)
add a comment |
Ok I managed to crack this one on my own, but I will post the part that was wrong, so someone with similliar problem might use it.
I was trying to use an example for application that wasn't a media player, thus set(AudioManager.MODE_IN_CALL)
was not right for this.
Correct code for my onClickListener is:
public void onClick (View v) {
AudioManager myAudioManager;
myAudioManager = (AudioManager)getSystemService(Context.AUDIO_SERVICE);
myAudioManager.setMode(AudioManager.STREAM_MUSIC);
if (myAudioManager.isWiredHeadsetOn())
{musicOutputDevice.setText("Słuchawki");
myAudioManager.setSpeakerphoneOn(true);}
else
{musicOutputDevice.setText("Głośnik");
myAudioManager.setSpeakerphoneOn(false);
myAudioManager.setWiredHeadsetOn(true);}
Anyway, thank you guys for posting so many useful advices in other threads! This page is awesome! Hope I'll learn even more while being here :)
Ok I managed to crack this one on my own, but I will post the part that was wrong, so someone with similliar problem might use it.
I was trying to use an example for application that wasn't a media player, thus set(AudioManager.MODE_IN_CALL)
was not right for this.
Correct code for my onClickListener is:
public void onClick (View v) {
AudioManager myAudioManager;
myAudioManager = (AudioManager)getSystemService(Context.AUDIO_SERVICE);
myAudioManager.setMode(AudioManager.STREAM_MUSIC);
if (myAudioManager.isWiredHeadsetOn())
{musicOutputDevice.setText("Słuchawki");
myAudioManager.setSpeakerphoneOn(true);}
else
{musicOutputDevice.setText("Głośnik");
myAudioManager.setSpeakerphoneOn(false);
myAudioManager.setWiredHeadsetOn(true);}
Anyway, thank you guys for posting so many useful advices in other threads! This page is awesome! Hope I'll learn even more while being here :)
answered Nov 22 '18 at 4:03
PaladinRagePaladinRage
61
61
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53411830%2fswitching-between-phone-speaker-and-wired-headset%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown