Audio stream on some mobile models does not work

Asked

Viewed 47 times

0

I am concluding a simple audio stream (Web Radio) to finalize a project in my city. I am still new to programming app, but I noticed that some models of Smartphone (Moto Xplay and Sony Xpéria), stream does not run, but testing on my model (A5) the stream plays normally.

I’ve already declared every possible permissions that could be made, but I’m not finding a solution in this case.

package conciso.radioweb156natal;

import android.media.AudioManager;
import android.media.MediaPlayer;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;


import java.io.IOException;

public class MainActivity extends AppCompatActivity {

    Button b_play;
    MediaPlayer mediaPlayer;

    boolean prepared = false;
    boolean started = false;
    boolean primeiraExecucao = true;

    String url = "http://radio.imd.ufrn.br/156natal";

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

        b_play = (Button) findViewById(R.id.b_play);
        b_play.setEnabled(false);
        b_play.setText("   CONECTANDO...    ");

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

        new PlayerTask().execute(url);


        b_play.setOnClickListener (new View.OnClickListener(){
            @Override
            public void onClick (View view) {
                if(started){
                    started = false;
                    mediaPlayer.start();
                    b_play.setText("PAUSE");
                } else {
                    started = true;
                    mediaPlayer.pause();
                    b_play.setText("PLAY");
                }
            }

        });
    }
    class PlayerTask extends AsyncTask<String, Void, Boolean> {
        @Override
        protected Boolean doInBackground(String... strings) {

            try {
                mediaPlayer.setDataSource(strings[0]);
                mediaPlayer.prepare();
                prepared = true;
                mediaPlayer.start();
            } catch (IOException e) {
                e.printStackTrace();
            }

            return prepared;
        }

        @Override
        protected void onPostExecute(Boolean aBoolean) {
            super.onPostExecute(aBoolean);
            if(primeiraExecucao){
                b_play.setEnabled(true);
                b_play.setText("Pause");
                primeiraExecucao = false;
            }
            else {
                b_play.setEnabled(true);
                b_play.setText("PLAY");
            }
        }
    }

    @Override
    protected void onPause() {
        super.onPause();
        if(started){
            mediaPlayer.pause();
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
        if(started){
            mediaPlayer.start();
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if(prepared){
            mediaPlayer.release();
        }
    }

    public void clickexit(View v) {
        started = false;
        b_play.setText("PAUSE");
        this.finish();
    }

    @Override
    public void onBackPressed() {
        moveTaskToBack(true);
    }
}
  • Are you testing with the device connected on the pc? If yes, which Logcat result?

  • Opa fera, no then, on the devices that does not play the stream I am not testing connected to the pc.

  • Sometimes this can help you, Media Formats for Android. https://developer.android.com/guide/topics/media/media-formats.html

  • Xperia is generating this error: error (1, -2147483648) in Logcat, I am testing 2 URLS usage

  • I made a small Médiaplayer script with the url to play in the mobile browser in Xpéria and as amazing as it looks the stream plays perfectly: <audio autoplay="autoplay" Controls="Controls" loop="loop" src="http://radio.imd.ufrn.br/156natal"> <source src="music.ogg" type="audio/ogg"/> <source src="music.mp3" type="audio/mp3"/> <source src="music.wav" type="audio/wav"/> </audio>

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.