如何在 Android 中进行实时音频流处理时处理电话

发布于 2024-12-21 02:10:50 字数 5984 浏览 0 评论 0原文

我正在做一个在线音频流,音频在模拟器和设备中都播放得很好。但问题是,当我同时打电话给我的设备时,流媒体也在播放。我需要在来电时暂停并播放音频。你能帮忙如何处理该广播吗?

    public class BhajanStream extends Activity {
protected static final String TAG = null;
/** Called when the activity is first created. */

final String rs_bhajan_uri = "Media URL";
MediaPlayer mediaPlayer;
AudioManager audioManager;
Button bhajan_play;
Button bhajan_stop;
ImageView loadanim, effectbhajan;
AnimationDrawable loadanimation, effectanimation;
ProgressDialog dialog;
MusicServicePhoneStateListener mPhoneListener;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.bhajan);
    bhajan_play = (Button) findViewById(R.id.btn_play);
    bhajan_stop = (Button) findViewById(R.id.btn_stop);
    bhajan_stop.setVisibility(View.GONE);

    loadanim = (ImageView) findViewById(R.id.loadeffectview);
    effectbhajan = (ImageView) findViewById(R.id.bhajan_effect);

    /*if (mediaPlayer != null) {
           mediaPlayer.stop();
           mediaPlayer= null;    
          }*/


    mediaPlayer = new MediaPlayer();
    mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
    // mediaPlayer.reset();

    bhajan_play.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            ConnectivityManager conMan = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
            NetworkInfo Info = conMan.getActiveNetworkInfo();
            if (Info == null) {
                Toast.makeText(BhajanStream.this, "POOR SIGNALS ",
                        Toast.LENGTH_LONG).show();
                // startActivity(new Intent(Settings.ACTION_WIFI_SETTINGS));
            }



                loadanim.setBackgroundResource(R.drawable.loader_1);
                loadanim.setBackgroundResource(R.anim.loadanim);
                loadanimation = (AnimationDrawable) loadanim
                        .getBackground();
                loadanimation.isVisible();
                effectbhajan.setBackgroundResource(R.drawable.effect_bhajan1);
                effectbhajan.setBackgroundResource(R.anim.bhajaneffect);
                effectanimation = (AnimationDrawable) effectbhajan
                        .getBackground();

                     bhajan_play.setBackgroundResource(R.drawable.bhajan_start);
                bhajan_play.setVisibility(View.GONE);
                bhajan_stop.setVisibility(View.VISIBLE);
                loadanim.setVisibility(View.VISIBLE);
                effectbhajan.setVisibility(View.VISIBLE);


            try {
                mediaPlayer.reset();
                mediaPlayer.setDataSource(rs_bhajan_uri);

            } catch (IllegalArgumentException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IllegalStateException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

            try {
                 mediaPlayer.prepareAsync();

            } catch (IllegalStateException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

            mediaPlayer.setOnPreparedListener(new OnPreparedListener() {
                @Override
                public void onPrepared(MediaPlayer mp) {
                    mediaPlayer.start();

                }
            });
        }

    });

    bhajan_stop.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            if (mediaPlayer.isPlaying()) {
                if (mediaPlayer != null) {
            bhajan_stop.setVisibility(View.GONE);
            bhajan_play.setVisibility(View.VISIBLE);

            mediaPlayer.stop();
            loadanimation.stop();
            effectanimation.stop();
            loadanim.setVisibility(View.GONE);
            effectbhajan.setVisibility(View.GONE);
                }}

        }
    });

}

protected void onPreExecute() {
    // UI work allowed here

    loadanimation.start();


}

@Override
public void onBackPressed() {
    // do something
    if (mediaPlayer.isPlaying()) {
        if (mediaPlayer != null) {

            mediaPlayer.stop();
            loadanimation.stop();
            effectanimation.stop();
            bhajan_stop.setVisibility(View.GONE);
            bhajan_play.setVisibility(View.VISIBLE);
            loadanim.setVisibility(View.GONE);
            effectbhajan.setVisibility(View.GONE);
        }
    } else{
        startActivity(new Intent(BhajanStream.this, SaiStreams.class));
        finish();
    }
    }

private class MusicServicePhoneStateListener extends PhoneStateListener {
    private boolean mResumeAfterCall = false;

    @Override
    public void onCallStateChanged(int state, String incoming_number) {
        switch (state) {
        case TelephonyManager.CALL_STATE_OFFHOOK:
        case TelephonyManager.CALL_STATE_RINGING:
            Log.i(TAG, "phone active, suspending music service");
            mResumeAfterCall = mediaPlayer.isPlaying();
            mediaPlayer.pause();
            break;
        case TelephonyManager.CALL_STATE_IDLE:
            Log.i(TAG, "phone inactive, resuming music service");
            if (mResumeAfterCall) {
               mediaPlayer.start();
            }
            break;
        default:
            break;
        }
    }
}
public void onCreate(){
    mPhoneListener = new MusicServicePhoneStateListener();
    ((TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE)).listen(mPhoneListener, PhoneStateListener.LISTEN_CALL_STATE);
}
public void onDestroy(){
    mPhoneListener = new MusicServicePhoneStateListener();
    ((TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE)).listen(mPhoneListener, 0);
}

}

I am doing an audio online streaming the audio is playing fine both in emulator and device. But the issue is when i make a call to my device simultaneously the streaming also playing. I need to pause and play the audio back while the call is coiming. Can u help how to handle that broadcasting.

    public class BhajanStream extends Activity {
protected static final String TAG = null;
/** Called when the activity is first created. */

final String rs_bhajan_uri = "Media URL";
MediaPlayer mediaPlayer;
AudioManager audioManager;
Button bhajan_play;
Button bhajan_stop;
ImageView loadanim, effectbhajan;
AnimationDrawable loadanimation, effectanimation;
ProgressDialog dialog;
MusicServicePhoneStateListener mPhoneListener;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.bhajan);
    bhajan_play = (Button) findViewById(R.id.btn_play);
    bhajan_stop = (Button) findViewById(R.id.btn_stop);
    bhajan_stop.setVisibility(View.GONE);

    loadanim = (ImageView) findViewById(R.id.loadeffectview);
    effectbhajan = (ImageView) findViewById(R.id.bhajan_effect);

    /*if (mediaPlayer != null) {
           mediaPlayer.stop();
           mediaPlayer= null;    
          }*/


    mediaPlayer = new MediaPlayer();
    mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
    // mediaPlayer.reset();

    bhajan_play.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            ConnectivityManager conMan = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
            NetworkInfo Info = conMan.getActiveNetworkInfo();
            if (Info == null) {
                Toast.makeText(BhajanStream.this, "POOR SIGNALS ",
                        Toast.LENGTH_LONG).show();
                // startActivity(new Intent(Settings.ACTION_WIFI_SETTINGS));
            }



                loadanim.setBackgroundResource(R.drawable.loader_1);
                loadanim.setBackgroundResource(R.anim.loadanim);
                loadanimation = (AnimationDrawable) loadanim
                        .getBackground();
                loadanimation.isVisible();
                effectbhajan.setBackgroundResource(R.drawable.effect_bhajan1);
                effectbhajan.setBackgroundResource(R.anim.bhajaneffect);
                effectanimation = (AnimationDrawable) effectbhajan
                        .getBackground();

                     bhajan_play.setBackgroundResource(R.drawable.bhajan_start);
                bhajan_play.setVisibility(View.GONE);
                bhajan_stop.setVisibility(View.VISIBLE);
                loadanim.setVisibility(View.VISIBLE);
                effectbhajan.setVisibility(View.VISIBLE);


            try {
                mediaPlayer.reset();
                mediaPlayer.setDataSource(rs_bhajan_uri);

            } catch (IllegalArgumentException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IllegalStateException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

            try {
                 mediaPlayer.prepareAsync();

            } catch (IllegalStateException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

            mediaPlayer.setOnPreparedListener(new OnPreparedListener() {
                @Override
                public void onPrepared(MediaPlayer mp) {
                    mediaPlayer.start();

                }
            });
        }

    });

    bhajan_stop.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            if (mediaPlayer.isPlaying()) {
                if (mediaPlayer != null) {
            bhajan_stop.setVisibility(View.GONE);
            bhajan_play.setVisibility(View.VISIBLE);

            mediaPlayer.stop();
            loadanimation.stop();
            effectanimation.stop();
            loadanim.setVisibility(View.GONE);
            effectbhajan.setVisibility(View.GONE);
                }}

        }
    });

}

protected void onPreExecute() {
    // UI work allowed here

    loadanimation.start();


}

@Override
public void onBackPressed() {
    // do something
    if (mediaPlayer.isPlaying()) {
        if (mediaPlayer != null) {

            mediaPlayer.stop();
            loadanimation.stop();
            effectanimation.stop();
            bhajan_stop.setVisibility(View.GONE);
            bhajan_play.setVisibility(View.VISIBLE);
            loadanim.setVisibility(View.GONE);
            effectbhajan.setVisibility(View.GONE);
        }
    } else{
        startActivity(new Intent(BhajanStream.this, SaiStreams.class));
        finish();
    }
    }

private class MusicServicePhoneStateListener extends PhoneStateListener {
    private boolean mResumeAfterCall = false;

    @Override
    public void onCallStateChanged(int state, String incoming_number) {
        switch (state) {
        case TelephonyManager.CALL_STATE_OFFHOOK:
        case TelephonyManager.CALL_STATE_RINGING:
            Log.i(TAG, "phone active, suspending music service");
            mResumeAfterCall = mediaPlayer.isPlaying();
            mediaPlayer.pause();
            break;
        case TelephonyManager.CALL_STATE_IDLE:
            Log.i(TAG, "phone inactive, resuming music service");
            if (mResumeAfterCall) {
               mediaPlayer.start();
            }
            break;
        default:
            break;
        }
    }
}
public void onCreate(){
    mPhoneListener = new MusicServicePhoneStateListener();
    ((TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE)).listen(mPhoneListener, PhoneStateListener.LISTEN_CALL_STATE);
}
public void onDestroy(){
    mPhoneListener = new MusicServicePhoneStateListener();
    ((TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE)).listen(mPhoneListener, 0);
}

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

牵你手 2024-12-28 02:10:50

在您的 Activity 中,您可以通过调用 TelephonyManager 类中的 public void Listen (PhoneStateListenerlistener, int events) 来注册电话状态监听器。请参阅此处 。此外,您还可以调用 Context.getSystemService(Context.TELEPHONY_SERVICE) 来获取 TelephonyManager 对象的实例。

In your activity,you can register a phone state listener by calling public void listen (PhoneStateListener listener, int events) in the TelephonyManager class. See here. Also,You can call Context.getSystemService(Context.TELEPHONY_SERVICE) to get an instance of the TelephonyManager object.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文