无法在Android应用中通过IP地址播放广播

时间:2016-04-16 10:43:32

标签: java android android-studio radio

我正在开发一个Android收音机应用程序,我将通过IP播放无线电,这是" http://27.147.128.134:8000/"但每次我运行应用程序大约需要10到15分钟。此外,我试图通过谷歌Chrome浏览器播放它,但也无法正常工作。

这是我的代码部分。

public class MainActivity extends Activity {


    static MediaPlayer player;
    String StreamUrl = "http://27.147.128.134:8000/";
    ImageButton btnPlay;
    GifImageView gifImageView;
    Context context;
    SeekBar volume;
    ProgressBar progress_streaming;
    int REQUEST_CODE = 1230;
    private AudioManager audioManager = null;
    // Striming
    private PowerManager.WakeLock wl;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        try {
            context = this;
            progress_streaming = (ProgressBar) findViewById(R.id.progress_streming);
            // String text = "<font color=#ed1c24 size=8>Maasranga Television</font> <font color=#ffffff>, a Member of Square Group, is a leading satellite television channel in Bangladesh. Since its inception on July 30, 2011 as a mixed TV channel; Maasranga Television is broadcasting accurate, time-honoured and credible news from home and abroad along with exciting and inspiring entertainment for the whole family. </font>";
            // lblAbouttext1.setText(Html.fromHtml(text));
            init();
            if (player.isPlaying() == false) {
                Start_Streaming();
            }
            NoInternetConnectionReceiver();
        } catch (Exception ex) {
            MessageBox.Show(this, ex.getMessage());
        }

    }

    private void NoInternetConnectionReceiver() {

        try {
            context.registerReceiver(new BroadcastReceiver() {

                @Override
                public void onReceive(Context context, Intent intent) {
                    boolean noConnectivity = intent.getBooleanExtra(ConnectivityManager.EXTRA_NO_CONNECTIVITY, false);

                    if (noConnectivity) {
                        stopPlaying();
                        MessageBox.Show(context, "Please check your internet connection");
                    } else {
                        // MessageBox.Show(context,"false");
                        //Start_Streaming();
                    }
                }
            }, new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION));
        } catch (Exception ex) {
            MessageBox.Show(context, ex.getMessage());
        }
    }

    @SuppressLint("NewApi")
    private void init() {


        try {
            btnPlay = (ImageButton) findViewById(R.id.btnPlay);
            volume = (SeekBar) findViewById(R.id.volume);
            setVolumeControlStream(AudioManager.STREAM_MUSIC);
            gifImageView = (GifImageView) findViewById(R.id.imageView2);
            initializeMediaPlayer();
            VolumeControl();
        } catch (Exception ex) {
            MessageBox.Show(context, ex.getMessage());
        }

    }

    private void VolumeControl() {
        try {

            audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
            volume.setMax(audioManager
                    .getStreamMaxVolume(AudioManager.STREAM_MUSIC));
            volume.setProgress(audioManager
                    .getStreamVolume(AudioManager.STREAM_MUSIC));

            volume.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
                @Override
                public void onStopTrackingTouch(SeekBar arg0) {
                }

                @Override
                public void onStartTrackingTouch(SeekBar arg0) {
                }

                @Override
                public void onProgressChanged(SeekBar arg0, int progress,
                                              boolean arg2) {
                    audioManager.setStreamVolume(AudioManager.STREAM_MUSIC, progress, 0);
                }
            });
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void btnPlay_click(View view) {

        try {

            if (AppStatus.getInstance(context).isOnline()) {

                Start_Streaming();
            } else {
                MessageBox.Show(context, "Please check internet connection");
            }


        } catch (Exception ex) {
            MessageBox.Show(this, "btnPlay_click: " + ex.getMessage());
        }
    }

    private void Start_Streaming() {

        try {
            if (player.isPlaying() == false) {
                startPlaying();

            } else {
                stopPlaying();
            }
        } catch (Exception ex) {
            MessageBox.Show(context, "stream_error" + ex.getMessage());
        }
    }

    @Override
    protected void onResume() {
        // TODO Auto-generated method stub
        super.onResume();


    }

    private void startPlaying() {
        // buttonStopPlay.setEnabled(true);
        // buttonPlay.setEnabled(false);

        try {
            // playSeekBar.setVisibility(View.VISIBLE);

            if (player == null) {
                initializeMediaPlayer();

            }


            player.prepareAsync();

            progress_streaming.setVisibility(View.VISIBLE);
            btnPlay.setVisibility(View.GONE);
            gifImageView.setImageResource(R.drawable.test1);
            player.setOnPreparedListener(new OnPreparedListener() {

                public void onPrepared(MediaPlayer mp) {

                    //MessageBox.Show(context, "ready 4 play");

                    progress_streaming.setVisibility(View.GONE);
                    btnPlay.setVisibility(View.VISIBLE);
                    player.start();
                    // SetAlarm(getApplicationContext(), 0, 0, 30,
                    // REQUEST_CODE);
                    // start() ;
                }
            });

            player.setOnBufferingUpdateListener(new OnBufferingUpdateListener() {

                public void onBufferingUpdate(MediaPlayer mp, int percent) {
                    Log.v("buffer", "Buffering: " + percent);
                }
            });

            player.setOnCompletionListener(new OnCompletionListener() {

                @Override
                public void onCompletion(MediaPlayer mp) {
                    // MessageBox.Show(context, "Conplete...");
                    //mVisualizer.setEnabled(false);

                }
            });

            player.setOnInfoListener(new OnInfoListener() {

                @Override
                public boolean onInfo(MediaPlayer mp, int what, int extra) {
                    // TODO Auto-generated method stub
                    return false;
                }
            });

            btnPlay.setImageResource(R.drawable.rd_pause);
        } catch (Exception ex) {
            MessageBox.Show(this, "start playing" + ex.getMessage());
        }

    }

    private void stopPlaying() {
        if (player.isPlaying()) {
            player.stop();
            player.release();
            btnPlay.setImageResource(R.drawable.rd_play);
            gifImageView.setImageResource(R.drawable.rd_icon_middle_1);
            initializeMediaPlayer();
        }

    }

    private void initializeMediaPlayer() {


        try {
            player = new MediaPlayer();
            player.setAudioStreamType(AudioManager.STREAM_MUSIC);
            player.setDataSource(StreamUrl);

        } catch (IllegalArgumentException e
                ) {

            Log.v("esty", "IllegalArgumentException: " + e.getMessage());
            // e.printStackTrace();
        } catch (IllegalStateException e) {
            // e.printStackTrace();
            Log.v("esty", "IllegalStateException: " + e.getMessage());
        } catch (IOException e) {
            // .printStackTrace();

            Log.v("esty", "IOException: " + e.getMessage());
        }

    }


    @Override
    protected void onPause() {

        try {
            super.onPause();
            // CancelAlarm(getApplicationContext());
            //if (player.isPlaying() == false) {
            //  finish();
            //}
            //wl.release();
            Log.v("flag", "Pause");
        } catch (Exception ex) {
            MessageBox.Show(this, ex.getMessage());
        }
    }

    @Override
    protected void onDestroy() {
        // CancelAlarm(getApplicationContext());

        super.onDestroy();
        try {

            player.stop();
            player.release();
        } catch (Exception ex) {
            MessageBox.Show(context, ex.getMessage());
        }
    }


}

我无法弄清楚问题,但我将网址从此更改为此(&#34; http://eclipse.wavestreamer.com:5932/Live&#34;)它在应用和网络浏览器上顺利运行。

请指导我。我必须编码或解码任何东西吗?我错过了什么吗?

0 个答案:

没有答案