2016-07-31 30 views
6

我已經在我的應用程序中構建了一個自定義投射發送者,它完美地工作 - 我需要添加的唯一功能是偵聽「播放/暫停」和「跳過」按鈕,但谷歌的文檔(據我已經能夠搜索)不給任何線索,並增加在Advanced Cast Features Docs所示的功能後,沒有工作:如何通過投射控件界面捕獲UIMediaController點擊

CastMediaOptions castMediaOptions = new CastMediaOptions.Builder() 
    .setMediaIntentReceiverClassName(CastIntentReceiver.class.getName()) 
    .build(); 

return new CastOptions.Builder() 
     .setReceiverApplicationId(context.getString(R.string.cast_id)) 
     .setCastMediaOptions(castMediaOptions) 
     .build(); 

我有一個CastIntentReceiver延伸MediaIntentReceiver這是在CastOptionsProvider和清單中引用的。

public class CastIntentReceiver extends MediaIntentReceiver { 

    public static String TAG = "CastIntentReceiver"; 

    public CastIntentReceiver() { 
     Log.i(TAG, "CastIntentReceiver: Constructed!"); 
    } 
    ... 
    // The rest of the Override methods don't do anything 

} 

接收器工作,因爲日誌打印工作,但沒有一個覆蓋方法調用(沒有日誌打印)。

下面是關於我的實現一些附加細節:

所有鑄造功能是在兩個獨立的類,這是添加到在MainActivity視圖的底部的CastManager(控制器)和CastControls片段(視圖)(類似於到YouTube)。還有接口觸發這兩個類之間的事情。

在CastManager,我設置了UIMediaController並綁定瀏覽:

View view = CastingBridge.CAST_CONTROLS_VIEW.findViewById(R.id.cast_drawer_controls); 

uiMediaController = new UIMediaController(activity); 
SeekBar seekBar = (SeekBar) view.findViewById(R.id.cast_control_seek_bar); 
TextView start = (TextView) view.findViewById(R.id.cast_control_time_start); 
TextView end = (TextView) view.findViewById(R.id.cast_control_time_end); 
ImageButton playPauseButton = (ImageButton) view.findViewById(R.id.cast_control_play_pause); 
ImageButton rewindButton = (ImageButton) view.findViewById(R.id.cast_control_rewind); 
ImageButton forwardButton = (ImageButton) view.findViewById(R.id.cast_control_forward); 
ImageButton stopButton = (ImageButton) view.findViewById(R.id.cast_control_stop); 
ProgressBar loadingSpinner = (ProgressBar) view.findViewById(R.id.cast_control_loading); 

如前所述,所有這些工作,因爲他們應該,但我需要聽(捕獲)觸摸/點擊事件,以便當視頻暫停或停止時,我可以保存當前的手錶長度以用於恢復目的。

如何執行MediaIntentReceiver來收聽這些事件?我已經嘗試將點擊偵聽器添加到單個視圖中,但正如預期的那樣,@Override onClick刪除了最初的預期功能。

documentation指定什麼似乎是我需要的方法,但是這是如何引用的?

我也嘗試添加唯一可用的監聽器UIMediaController

uiMediaController.setPostRemoteMediaClientListener(new RemoteMediaClient.Listener() { 
    @Override 
    public void onStatusUpdated() { 
     Log.i(TAG, "onStatusUpdated: Status Updated"); 
    } 
}); 

然而這並沒有做任何事情,因爲onStatusUpdated方法提供任何參數。

任何人都可以幫我解釋一下嗎?我一直在嘗試不同的事情,並且搜索了幾天,所以有時間尋求幫助。

謝謝!

回答

3

您定義的接收器(和MediaIntentReceiver一般)用於處理來自通知,鎖定屏幕和投射對話框的操作;所以有一個自定義的影響從這些地方發起的行爲。如果您使用的是UIMediaController,那麼當用戶與這些控件進行交互時,您需要使用onSendingRemoteMediaRequest()來通知。

+0

感謝您的回覆。我已經實現了這一點,它確實工作,但onStatusUpdated方法沒有參數。我將如何確定(實際使用中的三個)哪個按鈕被實際點擊了? – mwieczorek