Android MediaPlayer在背景中砍

时间:2011-09-21 11:34:15

标签: android

我正在尝试使用mediaplayer类实现一个在后台播放流的服务..无论如何在某些手机(galaxy tab和一些LG模型)中,当我的应用程序处于后台时,当我打开另一个应用程序时声音会消失。 .. 为什么这个?在一些教程之后,我以这种方式实现了服务:

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 > 0)
                {
                    if(mp.isPlaying() && !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 >= urlsToPlay.length)
                canPlay = true;   

        }while(canPlay == false);

        if(clean == false && 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 && powerLock.isHeld())
            powerLock.release(); 
    }


}

以这种方式启动它:

this.startService(new Intent(appcontext, PlayerBackgroundService.class));

也许我的班级错了..你能帮助我吗?

提前致谢

1 个答案:

答案 0 :(得分:0)

你需要手动停止并恢复播放器中播放的音频,使用暂停和恢复活动方法,看看这个,

 @Override
protected void onResume() {
    super.onResume();
    if (mediaPlayer != null) {
        mediaPlayer.start();
    }
}

protected void onPause() {
    super.onPause();
    if (mediaPlayer != null) {
        mediaPlayer.pause();
        if (isFinishing()) {
            mediaPlayer.stop();
            mediaPlayer.release();
        }
    }