Skip to main content
4 de 4
título editado
Einer
  • 20.6k
  • 2
  • 17
  • 39

Error IllegalStateException utilizando MediaPlayer - Android

Estoy desarrollando una app, funciona, le doy al list view la radio que quiero, empieza a reproducir, pero si intento cambiar de radio falla.

¿alguna solución?

Aquí el LogCat:

10-03 11:56:32.895 1410-1410/com.david.animefm2 D/ViewRootImpl@e6a08af[MainActivity]: ViewPostImeInputStage processPointer 0 10-03 11:56:32.979 1410-1410/com.david.animefm2 D/ViewRootImpl@e6a08af[MainActivity]: ViewPostImeInputStage processPointer 1 10-03 11:56:32.980 1410-1410/com.david.animefm2 D/AbsListView: onTouchUp() mTouchMode : 0

--------- beginning of system 10-03 11:56:33.056 1410-1410/com.david.animefm2 V/MediaPlayer-JNI: stop 10-03 11:56:33.056 1410-1410/com.david.animefm2 V/MediaPlayer: stop 10-03 11:56:33.056 1410-1630/com.david.animefm2 V/MediaPlayer: message received msg=8, ext1=0, ext2=0 10-03 11:56:33.056 1410-1630/com.david.animefm2 V/MediaPlayer: unrecognized message: (8, 0, 0) 10-03 11:56:33.056 1410-1630/com.david.animefm2 V/MediaPlayer: callback application 10-03 11:56:33.057 1410-1630/com.david.animefm2 V/MediaPlayer: back from callback 10-03 11:56:33.058 1410-1410/com.david.animefm2 V/MediaPlayer-JNI: release 10-03 11:56:33.058 1410-1410/com.david.animefm2 V/MediaPlayer: setListener 10-03 11:56:33.058 1410-1410/com.david.animefm2 V/MediaPlayer: disconnect 10-03 11:56:33.088 1410-1410/com.david.animefm2 V/MediaPlayer: destructor 10-03 11:56:33.089 1410-1410/com.david.animefm2 V/MediaPlayer: disconnect 10-03 11:56:33.107 1410-1410/com.david.animefm2 D/AndroidRuntime: Shutting down VM

--------- beginning of crash 10-03 11:56:33.115 1410-1410/com.david.animefm2 E/AndroidRuntime: FATAL EXCEPTION: main Process: com.david.animefm2, PID: 1410 java.lang.IllegalStateException at android.media.MediaPlayer.nativeSetDataSource(Native Method) at android.media.MediaPlayer.setDataSource(MediaPlayer.java:1173) at android.media.MediaPlayer.setDataSource(MediaPlayer.java:1129) at com.david.animefm2.MainActivity.BotonReproducir(MainActivity.java:102) at com.david.animefm2.MainActivity.SetUrl(MainActivity.java:95) at com.david.animefm2.MainActivity.access$100(MainActivity.java:18) at com.david.animefm2.MainActivity$2.onItemClick(MainActivity.java:57) at android.widget.AdapterView.performItemClick(AdapterView.java:339) at android.widget.AbsListView.performItemClick(AbsListView.java:1718) at android.widget.AbsListView$PerformClick.run(AbsListView.java:4184) at android.widget.AbsListView$13.run(AbsListView.java:6754) at android.os.Handler.handleCallback(Handler.java:751) at android.os.Handler.dispatchMessage(Handler.java:95) at android.os.Looper.loop(Looper.java:154) at android.app.ActivityThread.main(ActivityThread.java:6776) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1520) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1410)

Aqui mi codigo:

package com.david.animefm2;

import android.app.Activity;
import android.content.pm.ActivityInfo;
import android.media.AudioManager;
import android.os.Bundle;
import android.media.MediaPlayer;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import java.io.IOException;
import java.util.ArrayList;


public class MainActivity extends Activity {

    private ArrayList radios;
    private TextView tvNombre, tvNumCelda;
    private RadioAdapter adapter;
    private ListView lvRadios;
    private int isPlay ;
    private String url ="";
    private MediaPlayer mediaPlayer = new MediaPlayer();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //config
        setVolumeControlStream(AudioManager.STREAM_MUSIC);
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);

        //variables
        radios = new ArrayList<radio>();
        rellenarArrayList();
        adapter = new RadioAdapter(this, radios);
        lvRadios = (ListView) findViewById(R.id.lvRadeo);
        lvRadios.setAdapter(adapter);

        //Boton Reproducir y pausa
        Button start = (Button) findViewById(R.id.START);
        start.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                BotonReproducir();
            }
        });

        //Lista
        ListView lista = (ListView) findViewById(R.id.lvRadeo);
        lista.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView adapterView, View view, int i, long l) {
                SetUrl(i);
            }
        });
    }
    private void rellenarArrayList() {
        radios.add(new radio("Radio Vocaloid", R.drawable.radiovocaloid));
        radios.add(new radio("Radio Anime", R.drawable.ic_home_black_24dp));
        radios.add(new radio("caballo", R.drawable.ic_home_black_24dp));
        radios.add(new radio("camaleon", R.drawable.ic_home_black_24dp));
        radios.add(new radio("canario", R.drawable.ic_home_black_24dp));
        radios.add(new radio("cerdo", R.drawable.ic_home_black_24dp));
        radios.add(new radio("delfin", R.drawable.ic_home_black_24dp));
        radios.add(new radio("gato", R.drawable.ic_home_black_24dp));
        radios.add(new radio("iguana", R.drawable.ic_home_black_24dp));
        radios.add(new radio("lince", R.drawable.ic_home_black_24dp));
        radios.add(new radio("lobo", R.drawable.ic_home_black_24dp));
        radios.add(new radio("morena", R.drawable.ic_home_black_24dp));
        radios.add(new radio("orca", R.drawable.ic_home_black_24dp));
        radios.add(new radio("perro", R.drawable.ic_home_black_24dp));
        radios.add(new radio("vaca", R.drawable.ic_home_black_24dp));
    };

    private void SetUrl(int i){
        if(mediaPlayer!=null && mediaPlayer.isPlaying()){
            mediaPlayer.stop();
            mediaPlayer.release();
        }
        Toast.makeText(getApplicationContext(), "posicion " + (i), Toast.LENGTH_SHORT).show();
        switch (i) {
            case 0:
                url= "http://curiosity.shoutca.st:8019/stream"; //Radio Vocaloid http://www.radiovocaloid.com/
                isPlay = 0;
                break;
            case 1:
                url="http://perseus.shoutca.st:9253/stream"; //Radio Anime http://www.radio-anime.net/
                isPlay = 0;
                break;
        };
        BotonReproducir();
    }

    private void BotonReproducir() {

        if (url != "") {
            try {
                mediaPlayer.setDataSource(url);
                mediaPlayer.prepare();
            } catch (IOException e1) {
                e1.printStackTrace();
            }
            Toast.makeText(getApplicationContext(),"Bufering...", Toast.LENGTH_SHORT).show();
            mediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
                @Override
                public void onPrepared(MediaPlayer arg0) {
                    //Inicia reproducción.
                    mediaPlayer.start();
                }
            });
        } else {
            Toast.makeText(getApplicationContext(),"Select a rario", Toast.LENGTH_SHORT).show();
        }
    }

}