1. Check out our companion app, Forums for Android! Download from Google Play

Problems with ProgressBar and Button Stop

Discussion in 'App Inventor' started by Natlum, Oct 23, 2012.

  1. Natlum

    Natlum New Member
    Thread Starter
    5

    Oct 22, 2012
    2
    0
    5
    Hello.
    I am developing an application for online radio listening.
    But I have two problems that I can not solve:
    The first error is very problematic because when I give the play "play" button is activated stop "stop." So far so good, but if I press the Stop button before you start playing the audio, you stop off and activates the play but the application continues to play. This should not happen. I want the button to be pressed to activate when you begin to hear the audio, not when it starts to load.
    The second is related to the ProgressBar. I do not know why it does not load the progress bar. If it shows but not charging.
     

    Advertisement

  2. Natlum

    Natlum New Member
    Thread Starter
    5

    Oct 22, 2012
    2
    0
    5
    I hope I have been explicit about the problems I have.
    My Activity has this content:

    import java.io.IOException;
    import android.app.Activity;
    import android.media.MediaPlayer;
    import android.media.MediaPlayer.OnBufferingUpdateListener;
    import android.media.MediaPlayer.OnPreparedListener;
    import android.os.Bundle;
    import android.util.Log;
    import android.view.View;
    import android.view.View.OnClickListener;
    import android.widget.Button;
    import android.widget.ProgressBar;

    public class RadioActivity extends Activity implements OnClickListener {

    private final static String RADIO_STATION_URL = "http://xxxxxxxxxxxxxxxxxxxx";

    private ProgressBar playSeekBar;

    private Button buttonPlay;

    private Button buttonStopPlay;

    private MediaPlayer player;


    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.radio);

    initializeUIElements();

    initializeMediaPlayer();
    }

    private void initializeUIElements() {

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


    buttonPlay = (Button) findViewById(R.id.buttonPlay);
    buttonPlay.setOnClickListener(this);

    buttonStopPlay = (Button) findViewById(R.id.buttonStop);
    buttonStopPlay.setEnabled(false);
    buttonStopPlay.setOnClickListener(this);

    }

    @Override
    public void onClick(View v) {
    if (v == buttonPlay) {
    startPlaying();
    } else if (v == buttonStopPlay) {
    stopPlaying();
    }
    }


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

    playSeekBar.setVisibility(View.VISIBLE);

    player.prepareAsync();

    player.setOnPreparedListener(new OnPreparedListener() {

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

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

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

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

    player.setOnBufferingUpdateListener(new OnBufferingUpdateListener() {

    @Override
    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();**/
    /**stopPlaying();**/
    }

    }


    @Override
    protected void onStop(){
    super.onStop();
    if (player.isPlaying()){
    stopPlaying();
    }
    }




    }



    And my layout is this one:


    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="#0069AD" >


    <ImageView
    android:id="@+id/imageView1"
    android:layout_width="wrap_content"
    android:layout_height="320dp"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:contentDescription="@string/imgreproductor"
    android:src="@drawable/reproductor" />

    <TextView
    android:id="@+id/textView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/imageView1"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="10dp"
    android:text="@string/URL" />

    <ProgressBar
    android:id="@+id/progressBar1"
    style="?android:attr/progressBarStyleHorizontal"
    android:layout_width="@dimen/anchoBarra"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/textView1"
    android:layout_below="@+id/textView1"
    android:layout_marginTop="2dp" />

    <Button
    android:id="@+id/buttonStop"
    android:layout_width="@dimen/anchoPlay"
    android:layout_height="wrap_content"
    android:layout_alignRight="@+id/progressBar1"
    android:layout_below="@+id/progressBar1"
    android:layout_marginTop="13dp"
    android:text="@string/Stop" />

    <Button
    android:id="@+id/buttonPlay"
    android:layout_width="@dimen/anchoPlay"
    android:layout_height="wrap_content"
    android:layout_alignBaseline="@+id/buttonStop"
    android:layout_alignBottom="@+id/buttonStop"
    android:layout_alignLeft="@+id/progressBar1"
    android:text="@string/Play" />

    </RelativeLayout>


    I hope your help and thank you very much beforehand.
    greetings
     

Share This Page

Loading...