2012-04-21 84 views
0

我的Android應用似乎在播放背景音樂時遇到問題。如何繼續音樂?

我寫了一個代碼來播放整個應用程序的音樂... 但是當我點擊一個按鈕去一個新的.class活動。音樂滯後幾秒....或重新開始?

這是我的代碼:MusicService.java

public class MusicService extends Service implements 
    MediaPlayer.OnErrorListener { 

private final IBinder mBinder = new ServiceBinder(); 
private MediaPlayer mPlayer; 
public static int length = 0; 

public MusicService() { 
} 

public class ServiceBinder extends Binder { 
    MusicService getService() { 
     return MusicService.this; 
    } 
} 

@Override 
public IBinder onBind(Intent arg0) { 
    return mBinder; 
} 

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

    mPlayer = MediaPlayer.create(MusicService.this, R.raw.bgmusic); 
    mPlayer.setOnErrorListener(this); 

    if (mPlayer != null) { 
     mPlayer.setLooping(true); 
     mPlayer.setVolume(100, 100); 
    } 

    mPlayer.setOnErrorListener(new OnErrorListener() { 

     public boolean onError(MediaPlayer mp, int what, int extra) { 

      onError(mPlayer, what, extra); 
      return true; 
     } 
    }); 
} 

@Override 
public int onStartCommand(Intent intent, int flags, int startId) { 
    Log.d("Moron Test", "Moron Test OnStart Length : " + length); 
    if(length == 0) 
    { 
     mPlayer.start(); 
    } 
    else 
    { 
     resumeMusic(); 
    } 
    return START_STICKY; 
} 


public void pauseMusic() { 
    if (mPlayer != null && mPlayer.isPlaying()) { 
     mPlayer.pause(); 
     length = mPlayer.getCurrentPosition(); 
    } 
    Log.d("Moron Test", "Moron Test pauseMusic Length : " + length); 
} 

public void resumeMusic() { 
    Log.d("Moron Test", "Moron Test resumeMusic Length : " + length); 
    if (mPlayer != null && mPlayer.isPlaying() == false) { 
     mPlayer.seekTo(length); 
     mPlayer.start(); 
    } 
    else 
    { 
     mPlayer.start(); 
    } 
} 

public void stopMusic() { 
    mPlayer.stop(); 
    mPlayer.release(); 
    mPlayer = null; 
} 

@Override 
public void onDestroy() { 
    super.onDestroy(); 
    if (mPlayer != null) { 
     try { 
      pauseMusic(); 
      mPlayer.stop(); 
      mPlayer.release(); 
     } finally { 
      mPlayer = null; 
     } 
    } 
} 

public boolean onError(MediaPlayer mp, int what, int extra) { 

    Toast.makeText(this, "music player failed", Toast.LENGTH_SHORT).show(); 
    if (mPlayer != null) { 
     try { 
      mPlayer.stop(); 
      mPlayer.release(); 
     } finally { 
      mPlayer = null; 
     } 
    } 
    return false; 
} 

}

和我的其他代碼:WAEBaseActivity.java

public class WAEBaseActivity extends Activity { 
Intent svc; 
boolean isServiceStarted; 
@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    isServiceStarted = false; 
} 
@Override 
protected void onResume() { 
    super.onResume(); 
    try { 
     if (Prefs.getMusic(this)) { 
      svc = new Intent(this, MusicService.class); 
      startService(svc); 
      isServiceStarted = true; 
     } 
    } catch (Exception e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } 
} 

@Override 
protected void onPause() { 
    super.onPause(); 
    if(isServiceStarted) 
     stopService(svc); 

} 

}

在我正常的應用程序的每個。我有這行代碼

public class Home extends WAEBaseActivity { 

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 
    MusicService.length = 0; 

這就是它。 感謝您的幫助

瓦希德

回答

0

服務的主要UI線程上運行,它看起來像您尚未創建另一個線程來處理音樂在後臺播放。嘗試創建一個新的線程,這將反過來啓動播放器。我的猜測是,這將有助於很多事情。

+0

我已經得到了啓動播放器的信息: public class Home extends WAEBaseActivity { – Wally 2012-04-21 13:31:05