I am completing a simple audio stream (Web Radio) for finalizing a project in my city. I am still new to the programming area of app
, however I noticed that some Smartphone models (Moto Xplay and Sony Xpéria) , the stream does not run, however doing tests on my model ( A5) the stream plays normally.
I have already declared all possible permissions that could be made, but I am 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);
}
}