Hola a todos y a todas.

Estoy desarrollando una aplicación, para escuchar una radio online.
Pero tengo dos problemas que no se cómo solucionar:

El primero es muy grabe ya que cuando le doy al play "reproducir", se 
activa el botón stop "parar". Hasta aquí todo bien, pero si pulso el botón 
Stop antes de que comience a reproducir el audio, se desactiva el stop y se 
activa el play pero la aplicación sigue reproduciéndose. Esto no tendría 
que pasar. Yo quiero que el botón se active para ser pulsado cuando se 
comience a escuchar el audio, no cuando se comience a cargar.

El segundo va relacionado con el ProgressBar. No sé porqué no se carga la 
barra de progreso. Si se muestra pero no se carga.


Espero haber sido explicito en los problemas que tengo.

Mi Activity tiene este contenido:


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();
    }
    }
    
    

    
}


Y mi Layout tiene este otro:

<?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>


Espero vuestra ayuda y muchas gracias de ante mano. 

Un saludo

-- 
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en

Reply via email to