如何在不等待Android的情况下立即收听广播流?

时间:2015-11-05 09:22:12

标签: android audio streaming android-mediaplayer audio-streaming

我总是使用这种方法来读取流媒体广播电台的流程。

     play.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {

            if(play.getTag().equals("Start")){
                notificationBar("Click To Stop Radio",nom);
                play.setEnabled(false);
                startPlaying(image, nom, urlradio);
            }else{

                play.setTag("Start");
                stopPlaying(urlradio);
                play.setImageResource(R.drawable.play);
            }

        }
    });

    OnAudioFocusChangeListener focusChangeListener =
            new OnAudioFocusChangeListener() {
                public void onAudioFocusChange(int focusChange) {

                    switch (focusChange) {

                        case (AudioManager.AUDIOFOCUS_LOSS_TRANSIENT_CAN_DUCK) :
                            // Lower the volume while ducking.
                            player.setVolume(0.2f, 0.2f);
                            break;
                        case (AudioManager.AUDIOFOCUS_LOSS_TRANSIENT) :
                            player.pause();
                            break;

                        case (AudioManager.AUDIOFOCUS_LOSS) :
                            //  player.stop();

                            break;

                        case (AudioManager.AUDIOFOCUS_GAIN) :
                            // Return the volume to normal and resume if paused.
                            player.setVolume(1f, 1f);
                            player.start();

                            break;
                        default: break;
                    }
                }
            };
    // Request audio focus for playback
    int result = audioManager.requestAudioFocus(focusChangeListener,
            // Use the music stream.
            AudioManager.STREAM_MUSIC,
            // Request permanent focus.
            AudioManager.AUDIOFOCUS_GAIN);


    if (result == AudioManager.AUDIOFOCUS_REQUEST_GRANTED) {
        // other app had stopped playing song now , so u can do u stuff now .
    }
private void startPlaying(String image,String nom,String urlradio) {

    initializeMediaPlayer(urlradio);

    playSeekBar.setVisibility(View.VISIBLE);

    player.prepareAsync();

    player.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {

        public void onPrepared(MediaPlayer mp) {
            player.start();
            playSeekBar.setVisibility(View.INVISIBLE);
            play.setEnabled(true);
            play.setImageResource(R.drawable.stop);
            play.setTag("Stop");

        }
    });

}
private void stopPlaying(String urlradio) {
    if (player != null) {
        player.stop();
        player.release();
        player = null;
    }
    notificationClose(1);
    playSeekBar.setVisibility(View.INVISIBLE);
}

private void initializeMediaPlayer(String radio) {

    player = new MediaPlayer();
    player.setWakeMode(getApplicationContext(), PowerManager.PARTIAL_WAKE_LOCK);
    audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
    try {
        player.setDataSource(radio);
    } catch (IllegalArgumentException e) {
        e.printStackTrace();
    } catch (IllegalStateException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

    player.setOnBufferingUpdateListener(new MediaPlayer.OnBufferingUpdateListener() {

        public void onBufferingUpdate(MediaPlayer mp, int percent) {

            playSeekBar.setSecondaryProgress(percent);


            Log.i("Buffering", "" + percent);
        }
    });
}

但是在某些广播电台中我预计会有20到30秒的时间来收听,我在某些应用程序中看到了流量瞬间消失。

如何在不等待的情况下立即收听广播流?

0 个答案:

没有答案