在手机扬声器和有线耳机之间切换

时间:2018-11-21 12:15:04

标签: android

我是Android编程的新手。我正在尝试构建一个多媒体播放器应用程序,并且我想添加选项以在按下按钮时在有线耳机和扬声器之间切换-尽管我知道此选项相当多余,但项目要求迫使我实现了这种功能。

我已经从该线程尝试了所有建议的想法: Is there any way to force audio through the speakers when headphones are plugged in?

我添加了权限MODIFY_AUDIO_SETTINGS来显示。

我确实相信我的错误可能是由于我不完全理解所使用的某些机制而引起的,但是我认为在这里寻求帮助不会有什么坏处,这是我在应用中进行玩家活动的代码:< / p>

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;



        }};

1 个答案:

答案 0 :(得分:0)

好吧,我设法自己破解了这个,但是我将发布错误的部分,所以有类似问题的人可以使用它。

我正在尝试使用非媒体播放器的示例,因此set(AudioManager.MODE_IN_CALL)不合适。

我的onClickListener的正确代码是:

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);}

无论如何,谢谢你们在其他主题中发布了这么多有用的建议!这个页面很棒!希望我在这里能学到更多:)