8
A
回答
4
不幸的是我發現沒有辦法做到這一點,而不是圍繞MediaPlayer與自定義狀態機容器。
加...
您可以創建一個容器,它是一個獨立的,並定義了它自己的狀態。與MediaPlayer的唯一交互是通過此容器完成的。所有其他類都需要訪問容器的API。
下面是一個簡單的輪廓:
public class MyContainer implements OnBufferingUpdateListener,
OnCompletionListener,
OnErrorListener,
OnInfoListener,
OnPreparedListener
{
static enum MP_STATES
{
MPS_IDLE,
MPS_INITIALIZED,
MPS_PREPARING,
MPS_PREPARED,
MPS_STARTED,
MPS_STOPPED,
MPS_PAUSED,
MPS_PLAYBACK_COMPLETED,
MPS_ERROR,
MPS_END,
}
private static MyContainer s_mpm = null;
private MP_STATES m_eState;
private MediaPlayer m_mp;
public static MyContainer get()
{
if (null == s_mpm)
{
s_mpm = new MyContainer();
}
return s_mpm;
}
private MyContainer()
{
m_mp = new MediaPlayer();
m_mp.setOnBufferingUpdateListener(this);
m_mp.setOnCompletionListener(this);
m_mp.setOnErrorListener(this);
m_mp.setOnInfoListener(this);
m_mp.setOnPreparedListener(this);
m_eState = MP_STATES.MPS_IDLE;
}
public MP_STATES getState()
{
return m_eState;
}
public void setUrl(String szUrl)
{
bringToIdleState();
try {
m_mp.setDataSource(szUrl);
m_eState = MP_STATES.MPS_INITIALIZED;
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (SecurityException e) {
e.printStackTrace();
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
m_mp.prepareAsync();
m_eState = MP_STATES.MPS_PREPARING;
}
public void play()
{
switch (m_eState)
{
case MPS_PREPARED:
case MPS_PAUSED:
m_mp.start();
m_eState = MP_STATES.MPS_STARTED;
break;
default:
// !! throw exception
}
}
public void pause()
{
switch (m_eState)
{
case MPS_STARTED:
m_mp.pause();
m_eState = MP_STATES.MPS_PAUSED;
break;
default:
// !! throw exception
}
}
public void release()
{
m_mp.release();
m_mp = null;
}
private void bringToIdleState()
{
// reset() should bring MP to IDLE
m_mp.reset();
m_eState = MP_STATES.MPS_IDLE;
}
// ** Callbacks
@Override
public void onPrepared(MediaPlayer mp)
{
m_eState = MP_STATES.MPS_PREPARED;
m_mp.start();
m_eState = MP_STATES.MPS_STARTED;
}
@Override
public boolean onInfo(MediaPlayer mp, int what, int extra)
{
return false;
}
@Override
public boolean onError(MediaPlayer mp, int what, int extra)
{
m_eState = MP_STATES.MPS_IDLE;
return false;
}
@Override
public void onCompletion(MediaPlayer mp)
{
// play the next song
}
@Override
public void onBufferingUpdate(MediaPlayer mp, int percent)
{
}
}
}
相關問題
- 1. MediaPlayer mOnPreparedListener爲空
- 2. MediaPlayer暫停()行爲
- 3. 從mediaplayer更改爲soundpool
- 4. 將InputStream轉換爲MediaPlayer
- 5. MediaPlayer爲什麼會失敗?
- 6. Android MediaPlayer - 睡眠行爲
- 7. 的MediaPlayer的start()/停止()穆裏爲空
- 8. 使用任意流作爲MediaPlayer的源
- 9. mediaplayer目前的位置總是爲零
- 10. 設置數據源爲MediaPlayer的
- 11. 與mediaplayer和seek的奇怪行爲
- 12. MediaPlayer的setDataSource爲expansionFilePath不起作用
- 13. E/MediaPlayer的:啓動名爲狀態64時,MPlayer(0xaf149260)使用的MediaPlayer
- 14. 的MediaPlayer在Android
- 15. MediaPlayer的崩潰
- 16. MediaPlayer的手機
- 17. JavaFX中的MediaPlayer
- 18. java.lang.IllegalStateException在MediaPlayer的
- 19. TYPE_NOTIFICATION與MediaPlayer的
- 20. MediaPlayer,ProgressBar
- 21. mediaplayer fadein
- 22. Mediaplayer NullPointerException
- 23. Android Mediaplayer
- 24. Mediaplayer NullPointerException?
- 25. Android MediaPlayer
- 26. MediaPlayer Android
- 27. 的Android SDK的MediaPlayer
- 28. XNA MediaPlayer的快進/
- 29. 更改MediaPlayer的SurfaceViews
- 30. RTP Android上的MediaPlayer
你能告訴更多? – neworld
我在帖子中添加了一個示例。即使MP狀態是公開的,我仍然會將MP包圍在這個容器中,以便抽象和限制對MP的訪問。 – paiego