Boa tarde,
Estou usando o exemplo do Livro Google Android para tentar manter o stream de uma radio executando em segundo plano, porém não estou conseguindo executar o Service porque o meu bindService não está conseguindo se conectar. Alguém já passou por algum problema como esse?
Segue os código.
InterfaceMP3:
public interface InterfaceMp3 {
public void start(String mp3);
public void pause();
public void stop();
}
PlayerMP3:
[code]public class PlayerMp3 implements OnCompletionListener {
private static final String CATEGORIA = “livro”;
private static final int NOVO = 0;
private static final int INICIADO = 1;
private static final int PAUSADO = 2;
private static final int PARADO = 3;
// começa o status zerado
private int status = NOVO;
private MediaPlayer player;
private String mp3;
public PlayerMp3() {
// Cria o MediaPlayer
player = new MediaPlayer();
//executa o listener quando terminar a música
player.setOnCompletionListener(this);
}
public void start(String mp3) {
this.mp3 = mp3;
try {
switch (status) {
case INICIADO:
player.stop();
case PARADO:
player.reset();
case NOVO:
Log.i(CATEGORIA, "Estou tocando");
player.setDataSource(mp3);
player.setAudioStreamType(AudioManager.STREAM_MUSIC);
player.prepare();
player.start();
case PAUSADO:
player.start();
break;
}
status = INICIADO;
} catch (Exception e) {
Log.e(CATEGORIA,e.getMessage(),e);
}
}
public void pause() {
player.pause();
status = PAUSADO;
}
public void stop() {
player.stop();
status = PARADO;
}
//fecha o MediaPlayer e libera a memória
public void fechar() {
stop();
player.release();
player = null;
}
/**
* @see android.media.MediaPlayer.OnCompletionListener#onCompletion(android.media.MediaPlayer)
*/
public void onCompletion(MediaPlayer mp) {
//quando terminar a reprodução...
Log.i(CATEGORIA, "Fim da música: " + mp3);
}
}[/code]
ServicoMP3:
[code]public class ServicoMp3 extends Service implements InterfaceMp3 {
private static final String CATEGORIA = “livro”;
private PlayerMp3 player = new PlayerMp3();
private final ConexaoInterfaceMp3 conexao = new ConexaoInterfaceMp3();
public class ConexaoInterfaceMp3 extends Binder {
public InterfaceMp3 getService() {
// retorna a interface para controlar o Service
return ServicoMp3.this;
}
}
@Override
public IBinder onBind(Intent intent) {
// retorna a classe ConexaoInterfaceMp3 para a activity utilizar
Log.i(CATEGORIA, "ServicoMp3 onBind(). Aqui retorna o IBinder.");
return conexao;
}
@Override
public void onCreate() {
Log.i(CATEGORIA, "ServicoMp3 onCreate()");
}
@Override
public void onStart(Intent intent, int startId) {
Log.i(CATEGORIA, "ServicoMp3 onStart()");
}
// InterfaceMp3.start(mp3)
public void start(String mp3) {
try {
player.start(mp3);
Log.i(CATEGORIA, "Estou chando player");
} catch (Exception e) {
Log.e(CATEGORIA, e.getMessage(), e);
}
}
// InterfaceMp3.pause()
public void pause() {
player.pause();
}
// InterfaceMp3.stop()
public void stop() {
player.stop();
}
@Override
public void onDestroy() {
Log.i(CATEGORIA, "ServicoMp3 onDestroy(). Aqui o MediaPlayer também foi encerrado.");
player.fechar();
}
}[/code]
Main:
[code]public class Main extends Activity implements OnClickListener {
private static final String CATEGORIA = “livro”;
//Interface para se comunicar com o serviço em segundo plano
private InterfaceMp3 interfaceMp3;
private ImageView btStart;
private ServiceConnection conexao = new ServiceConnection() {
public void onServiceConnected(ComponentName className, IBinder service) {
Log.i(CATEGORIA,"onServiceConnected, serviço conectado");
//Recupera a interface para interagir com o serviço
ConexaoInterfaceMp3 conexao = (ConexaoInterfaceMp3) service;
Log.i(CATEGORIA,"Recuperada a interface para controlar o mp3.");
interfaceMp3 = conexao.getService();
}
public void onServiceDisconnected(ComponentName className) {
Log.i(CATEGORIA,"onServiceDisconnected, liberando recursos.");
interfaceMp3 = null;
}
};
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.main);
btStart = (ImageView) findViewById(R.id.playButton);
btStart.setTag("1");
btStart.setOnClickListener(this);
Log.i(CATEGORIA,"Chamando startService()...");
//inicia o service
startService(new Intent(this,ServicoMp3.class));
Log.i(CATEGORIA,"Chamando bindService()...");
boolean b = bindService(new Intent(Main.this,ServicoMp3.class), conexao, Context.BIND_AUTO_CREATE);
Log.i(CATEGORIA,"bindService retorno: " + b);
}
/**
* @see android.view.View.OnClickListener#onClick(android.view.View)
*/
public void onClick(View view) {
try {
if (view == btStart) {
String mp3 = "http://voxsc1.somafm.com:9002/";
btStart.setTag("0");
Log.i(CATEGORIA, "Estou chamando o serviço");
btStart.setImageResource(R.drawable.btn_menustop);
interfaceMp3.start(mp3);
} else if (btStart.getTag() == "0") {
//para o player
interfaceMp3.stop();
btStart.setImageResource(R.drawable.btn_menuplay);
//para o Service, com a mesma Intent que o criou
stopService(new Intent(this,ServicoMp3.class));
}
} catch (Exception e) {
Log.e(CATEGORIA, e.getMessage(), e);
Toast.makeText(this, e.getMessage(), Toast.LENGTH_SHORT).show();
}
}
@Override
protected void onDestroy() {
super.onDestroy();
Log.i(CATEGORIA, "Activity destruida! Mas a musica continua...");
unbindService(conexao);
}
}[/code]
Me ajudem por favor !!