媒体播放器不播放shoutcast广播流

时间:2019-02-18 11:02:37

标签: java android android-mediaplayer android-studio-3.0

我正在尝试安装可与shoutcast链接一起使用的流播放器,但它对我不起作用。我想知道错误可能是什么,我已经附上了代码和链接。谢谢。

public class PrincipalActivity extends Activity {

    //creando botones
    Button btnPlay, btnRecargar, btnParar, btnTop, btnNosotros, btnSalir;

    MediaPlayer mediaPlayer; // creando clase mediaPlayer para reproducir archivos.

    String stream = "http://stream.dancefm.cl:9332"; //variable para la ulr streaming
boolean prepared, started = false; // 2 boleano para el estado del reproductor


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_principal);

        //instanciando botones creados arriba con los botones fisicos.
        btnPlay = (Button) findViewById(R.id.btnPlay);
        btnRecargar = (Button) findViewById(R.id.btnRecargar);
        btnParar = (Button) findViewById(R.id.btnStop);
        btnTop = (Button) findViewById(R.id.btnTop);
        btnNosotros = (Button) findViewById(R.id.btnNosotros);
        btnSalir = (Button) findViewById(R.id.btnSalir);

        WebView myWebbView = (WebView) this.findViewById(R.id.webprincipal); //creando e instanciando webview

        myWebbView.loadUrl("https://www.dancefm.cl/noticias/");//Cargando webview

        Toast toast = Toast.makeText(getApplicationContext(), "Esperando respuesta del servidor...", Toast.LENGTH_LONG);
         toast.show(); //arriba creamos un toast para mostrar un mensaje y aqui abajo lo mostramos.

        //Asignando estado a los botones... esto para que mientras este cargando no se utilzen estos botones.
        btnPlay.setEnabled(false);
        btnParar.setEnabled(false);
        btnRecargar.setEnabled(false);

        mediaPlayer = new MediaPlayer();
        mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);

        new PlayerTask().execute(stream); //llamara a quien reproducira el audio
    }

    class PlayerTask extends AsyncTask<String, Void, Boolean> {
        @Override
        protected Boolean doInBackground(String... strings) {
            try {
                mediaPlayer.setDataSource(strings[0]);
                mediaPlayer.prepare();
                prepared=true;
            } catch (IOException e) {
                e.printStackTrace();
            }
            return prepared;
        }

        @Override
        protected void onPostExecute (Boolean aBoolean){
            super.onPostExecute(aBoolean);
            //btnPlay.setBackground(R.mipmap.pause);
            mediaPlayer.start();
         }
     }
 }

任何合作都会受到赞赏,因为我只希望音频流开始

2 个答案:

答案 0 :(得分:0)

您可以使用prepareAsyc()来准备mediaplyer。

尝试此代码。

//creando botones
Button btnPlay, btnRecargar, btnParar, btnTop, btnNosotros, btnSalir;

MediaPlayer mediaPlayer; // creando clase mediaPlayer para reproducir archivos.

String stream = "http://stream.dancefm.cl:9332"; //variable para la ulr streaming
boolean prepared, started = false; // 2 boleano para el estado del reproductor


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_principal);

    //instanciando botones creados arriba con los botones fisicos.
    btnPlay = (Button) findViewById(R.id.btnPlay);
    btnRecargar = (Button) findViewById(R.id.btnRecargar);
    btnParar = (Button) findViewById(R.id.btnStop);
    btnTop = (Button) findViewById(R.id.btnTop);
    btnNosotros = (Button) findViewById(R.id.btnNosotros);
    btnSalir = (Button) findViewById(R.id.btnSalir);

    WebView myWebbView = (WebView) this.findViewById(R.id.webprincipal); //creando e instanciando webview

    myWebbView.loadUrl("https://www.dancefm.cl/noticias/");//Cargando webview

    Toast toast = Toast.makeText(getApplicationContext(), "Esperando respuesta del servidor...", Toast.LENGTH_LONG);
    toast.show(); //arriba creamos un toast para mostrar un mensaje y aqui abajo lo mostramos.

    //Asignando estado a los botones... esto para que mientras este cargando no se utilzen estos botones.
    btnPlay.setEnabled(false);
    btnParar.setEnabled(false);
    btnRecargar.setEnabled(false);

    mediaPlayer = new MediaPlayer();
    mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);

    mediaPlayer.setDataSource(stream);
    mediaPlayer.prepareAsync();
    mediaPlayer.setOnPreparedListener(new OnPreparedListener() {
        @Override
        public void onPrepared(MediaPlayer mp) {
            mp.start();
        }
    });

}

答案 1 :(得分:0)

您只需要将http://xxx.xx.xxx.x/http://xxx.xx.xxx.x/;stream.mp3更改为QnAMakerOptions Class,这很重要;在shoucast网址中的/之后,如果您使用IOS,并且确实可以检查您的Shoucast版本,则Ios仅可以复制Shoucast v2

相关问题