2012-10-10 26 views
10

Tengo una aplicación de Android que reproduce audio transmitido desde Internet utilizando la clase MediaPlayer.¿Cómo se reproduce el audio transmitido en segundo plano en Android?

¿Cómo dejo que continúe reproduciendo el audio en el fondo cuando el usuario toca el botón de inicio para ejecutar otras aplicaciones?

Mientras ejecuto otras aplicaciones, me gustaría que continúe reproduciendo el audio.

Respuesta

10

Tienes que usar algo llamado Android Services.

A partir de los documentos:

"Un servicio es un componente de aplicación que representa el deseo sea una aplicación de realizar una operación a largo funcionando mientras no interactúa con el usuario o para suministrar funcionalidad para otras aplicaciones para su uso."

Aquí está la excelente guía oficial para uso de los servicios para que pueda empezar: http://developer.android.com/guide/components/services.html

Aquí hay un buen tutorial sobre la construcción de un reproductor de audio: http://www.androidhive.info/2012/03/android-building-audio-player-tutorial/

Aquí hay un tutorial en vídeo para la construcción de un reproductor de música en streaming: http://www.youtube.com/watch?v=LKL-efbiIAM

+0

Gracias, ¡eche un vistazo a los Servicios! – Winston

4

Deberá implementar un Servicio para reproducir medios en segundo plano sin que esté vinculado a la Actividad que inició la reproducción. Eche un vistazo al this example.

+0

¡Gracias por la pista sobre Servicios y para el enlace! – Winston

1

La clave es definir Service.START_STICKY seguir jugando en el fondo:

public int onStartCommand(Intent intent, int flags, int startId) { 
     myMediaPlayer.start(); 
     return Service.START_STICKY; 
    } 

Service.START_STICKY: si el proceso de este servicio se mató mientras que es inicia el sistema intentará volver a crear el servicio.

Este es un ejemplo de hacer esto: el servicio

import android.app.Service; 
import android.content.Intent; 
import android.media.MediaPlayer; 
import android.os.IBinder; 
import android.util.Log; 
import android.widget.Toast; 

/** 
* Created by jorgesys. 
*/ 

/* Add declaration of this service into the AndroidManifest.xml inside application tag*/ 

public class BackgroundSoundService extends Service { 

    private static final String TAG = "BackgroundSoundService"; 
    MediaPlayer player; 

    public IBinder onBind(Intent arg0) { 
     Log.i(TAG, "onBind()"); 
     return null; 
    } 

    @Override 
    public void onCreate() { 
     super.onCreate(); 
     player = MediaPlayer.create(this, R.raw.jorgesys_song); 
     player.setLooping(true); // Set looping 
     player.setVolume(100,100); 
     Toast.makeText(this, "Service started...", Toast.LENGTH_SHORT).show(); 
     Log.i(TAG, "onCreate() , service started..."); 

    } 
    public int onStartCommand(Intent intent, int flags, int startId) { 
     player.start(); 
     return Service.START_STICKY; 
    } 

    public IBinder onUnBind(Intent arg0) { 
     Log.i(TAG, "onUnBind()"); 
     return null; 
    } 

    public void onStop() { 
     Log.i(TAG, "onStop()"); 
    } 
    public void onPause() { 
     Log.i(TAG, "onPause()"); 
    } 
    @Override 
    public void onDestroy() { 
     player.stop(); 
     player.release(); 
     Toast.makeText(this, "Service stopped...", Toast.LENGTH_SHORT).show(); 
     Log.i(TAG, "onCreate() , service stopped..."); 
    } 

    @Override 
    public void onLowMemory() { 
     Log.i(TAG, "onLowMemory()"); 
    } 
} 

de inicio:

Intent myService = new Intent(MainActivity.this, BackgroundSoundService.class); 
startService(myService); 

Detener servicio:

Intent myService = new Intent(MainActivity.this, BackgroundSoundService.class); 
stopService(myService); 

Complete code of this example.

Cuestiones relacionadas