2011-09-21 73 views
0

我試圖實現一個服務,在後臺使用mediaplayer類來播放流......無論如何,在某些手機(星系標籤和一些LG模型)時,當我的應用程序在背景中時,聲音切斷時我打開另一個應用程序...爲什麼這樣?下面的一些教程中,我實現這樣的服務:在後臺切斷的Android MediaPlayer

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)); 

也許我的課是錯的..你可以幫助我嗎?

在此先感謝

回答

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(); 
     } 
    } 
+0

謝謝您快速回答!我的服務適用於所有應用程序的運行時間,我不想停止或暫停,只需在需要時播放音樂,並使音樂能夠在後臺播放。您的代碼有哪些變化? – Erenwoid

+0

此代碼不適用於mediaplayer類,此方法用於默認活動類,以避免在後臺切斷音樂。 – Karthi

+0

它不會影響您在後臺播放的音樂,只有當您的應用處於活動模式時,纔會讓您的應用播放音頻,如果您的應用變爲背景,則表示它會自動處理您的音樂。 – Karthi