Android MediaPlayer chopping in background
<p>I'm trying to implement a service to play a stream in background using mediaplayer class.. anyway in some phone (galaxy tab and some LG model) when my app is in background, the sound chops when I open another application... why this? following some tutorial I implemented the service in this way:</p>

<pre><code>import com.somafm.api.PlayListFile;
import com.somafm.api.Playlist;

import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.net.Uri;
import android.os.IBinder;
import android.os.PowerManager;
import android.util.Log;

public class PlayerBackgroundService extends Service {

    static MediaPlayer.OnPreparedListener prepared_listener;
    static MediaPlayer.OnBufferingUpdateListener buffering_listener;
    static MediaPlayer.OnCompletionListener completion_listener;
    static MediaPlayer.OnErrorListener error_listener;

    private static Playlist playlist;
    private static PlayerController player;
    private static StreamProxy proxy;
    private static MediaPlayer mp = new MediaPlayer();

    static boolean canPlay;
    static boolean clean;
    static boolean cancelRequest;
    static boolean buffering;
    static boolean isPlaying;

    private static PowerManager.WakeLock powerLock;

    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @Override
    public void onCreate()
    {
        super.onCreate();
        init();
    }

    public void init()
    {
        PowerManager pM = (PowerManager) getSystemService(Context.POWER_SERVICE);
        powerLock = pM.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "Prevent sleeping");
        powerLock.acquire();

        prepared_listener = new MediaPlayer.OnPreparedListener() {

            @Override
            public void onPrepared(MediaPlayer mp) {
                canPlay = true;
                clean = true;
            }
        };

        error_listener = new MediaPlayer.OnErrorListener() {

            @Override
            public boolean onError(MediaPlayer mp, int what, int extra) {
                Log.i("!!MEDIAERROR!!", "WHAT " + what + " - " + extra);
                canPlay = true;
                clean = false;
                isPlaying = false;
                return true;
            }
        };

        buffering_listener = new MediaPlayer.OnBufferingUpdateListener() {
            @Override
            public void onBufferingUpdate(MediaPlayer mp, int percent) {
                Log.i("BUFFERING", "" + percent);
                if(percent &gt; 0)
                {
                    if(mp.isPlaying() &amp;&amp; !buffering)
                    {
                        player.notifyAllBuffering();
                        buffering = true;
                    }
                }
                else
                {
                    if(buffering)
                    {
                        player.notifyAllNotBuffering();
                        buffering = false;
                    }
                }
            }
        };

        completion_listener = new MediaPlayer.OnCompletionListener() {

            @Override
            public void onCompletion(MediaPlayer mp) {
                //stop();
                Log.i("COMPLETED", "COMPLETED");
            }
        };

    }

    public synchronized static void start(){
        mp.start();
        isPlaying = true;
    }

    public synchronized static void prepare(){

        canPlay = false;
        clean = true;

        proxy = new StreamProxy();
        proxy.init();
        proxy.start();

        if (playlist == null)
        {
            clean = false;
            proxy.stop();
            return;
        }

        int i = 0;

        String proxyUrl = "";

        playlist.fetchContent();
        PlayListFile[] urlsToPlay = playlist.getFiles();

        if (urlsToPlay == null)
        {
            clean = false;
            proxy.stop();
            return;
        }

        if (urlsToPlay.length == 0)
        {
            clean = false;
            proxy.stop();
            return;
        }

        do{
            try{

                proxyUrl = String.format("http://127.0.0.1:%d/%s", proxy
                        .getPort(), Uri.parse(urlsToPlay[i].getUrl()));
                i++;
                Log.i("Trying link", "" + urlsToPlay.length);
                mp = new MediaPlayer();
                mp.setOnPreparedListener(prepared_listener);
                mp.setOnBufferingUpdateListener(buffering_listener);
                mp.setOnErrorListener(error_listener);
                mp.setOnCompletionListener(completion_listener);
                mp.setDataSource(proxyUrl);
                mp.setAudioStreamType(AudioManager.STREAM_MUSIC);
                mp.prepare();


            }catch(Exception ex)
            {
                clean = false;                 
                ex.printStackTrace();
            }

            if(cancelRequest == true)
            {
                clean = true;
                return;
            }

            if(i &gt;= urlsToPlay.length)
                canPlay = true;   

        }while(canPlay == false);

        if(clean == false &amp;&amp; proxy != null)
            proxy.stop();

    }


    public synchronized static void stop() {
        if(proxy != null)
            proxy.stop();
        if(mp != null)
            mp.stop(); 
        isPlaying = false;
    }

    public synchronized static void loadPlaylist(Playlist playlist) {
        PlayerBackgroundService.playlist = playlist;
    }

    public synchronized static void registerPlayer(PlayerController player) {
        PlayerBackgroundService.player = player;
    }

    @Override
    public void onDestroy() {
        super.onDestroy();

        if (powerLock != null &amp;&amp; powerLock.isHeld())
            powerLock.release(); 
    }


}
</code></pre>

<p>And launch it in this way:</p>

<pre><code>this.startService(new Intent(appcontext, PlayerBackgroundService.class));
</code></pre>

<p>maybe my class is wrong.. can you help me?</p>

<p>Thanks in advance</p>
