如何从EditText获取URL来播放广播流?

时间:2016-08-20 11:08:09

标签: android android-studio android-edittext streaming

我想从这里播放收音机:“http://legacy.scahw.com.au/3rmr_32”。我希望从用户应该输入的EditText中获取它。

当我将url直接放到一个字符串时,它可以工作但是当我将它写入EditText并将其传递给字符串时,它不起作用。 任何人都可以帮我解决问题吗?

MainActivity

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        url = (EditText) findViewById(R.id.edit);
        final String urlString = url.getText().toString();

        playSeekBar = (ProgressBar) findViewById(R.id.progressBar1);
        playSeekBar.setMax(100);
        playSeekBar.setVisibility(View.VISIBLE);

        buttonPlay = (Button) findViewById(R.id.buttonPlay);
        buttonPlay.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {

                startPlaying(urlString);
            }
        });

        buttonStopPlay = (Button) findViewById(R.id.buttonStopPlay);
        buttonStopPlay.setEnabled(false);
        buttonStopPlay.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                stopPlaying();
            }
        });



        player = new MediaPlayer();
        try {  
            player.setDataSource(urlString);

        } catch (IllegalArgumentException e) {
            e.printStackTrace();
        } catch (IllegalStateException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

        player.setOnBufferingUpdateListener(new OnBufferingUpdateListener() {

            public void onBufferingUpdate(MediaPlayer mp, int percent) {
                playSeekBar.setSecondaryProgress(percent);
                Log.i("Buffering", "" + percent);
            }
        });
    }

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

        playSeekBar.setVisibility(View.VISIBLE);

        player.prepareAsync();

        player.setOnPreparedListener(new OnPreparedListener() {

            public void onPrepared(MediaPlayer mp) {
                mp.start();
            }
        });
    }

    private void stopPlaying() {
        if (player.isPlaying()) {
            player.stop();
            player.release();
            initializeMediaPlayer();
        }

        buttonPlay.setEnabled(true);
        buttonStopPlay.setEnabled(false);
        playSeekBar.setVisibility(View.INVISIBLE);
    }

    private void initializeMediaPlayer() {
        player = new MediaPlayer();
        try {  
            player.setDataSource(urlString);
        } catch (IllegalArgumentException e) {
            e.printStackTrace();
        } catch (IllegalStateException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

        player.setOnBufferingUpdateListener(new OnBufferingUpdateListener() {

            public void onBufferingUpdate(MediaPlayer mp, int percent) {
                playSeekBar.setSecondaryProgress(percent);
                Log.i("Buffering", "" + percent);
            }
        });
    }

    @Override
    protected void onPause() {
        super.onPause();
        if (player.isPlaying()) {
            player.stop();
        }
    }
}

main.xml中

<ProgressBar
    android:id="@+id/progressBar1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" />
<EditText
    android:id="@+id/edit"
    android:layout_marginTop="100dp"
    android:hint="Enter URL"
    android:layout_centerHorizontal="true"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" />
<Button
    android:id="@+id/buttonPlay"
    android:layout_centerVertical="true"
    android:layout_centerHorizontal="true"
    android:text="paly"
    android:layout_below="@+id/edit"
    android:layout_width="200dp"
    android:layout_height="wrap_content" />
<Button
    android:id="@+id/buttonStopPlay"
    android:text="stop"
    android:layout_centerHorizontal="true"
    android:layout_below="@+id/buttonPlay"
    android:layout_width="200dp"
    android:layout_height="wrap_content" />

3 个答案:

答案 0 :(得分:0)

您正在使用final String urlString = url.getText()。toString(); 因此,字符串将包含空白,并且在您输入时将来不会更改。 代替最终变量,请直接使用url.getText()。toString()来传递值。

答案 1 :(得分:0)

这一行

final String urlString = url.getText().toString();

你应该写onClickListene,如下所示

buttonPlay.setOnClickListener(new OnClickListener() {
    @Override
    public void onClick(View v) {
        String urlString = url.getText().toString();
        startPlaying(urlString);
    }
});

这可能对你有所帮助,一切顺利

答案 2 :(得分:0)

替换:

final String urlString = url.getText().toString();

用这个:

final String urlString = URLEncoder.encode(url.getText().toString(), "UTF-8");