Android Studio TV開發教程(十四)預覽視訊
阿新 • • 發佈:2019-02-17
科特林
匯入android.content.Context 匯入android.media.MediaPlayer 匯入android.media.tv.TvInputService 匯入android.net.Uri 匯入android.util.Log 匯入android.view.Surface 匯入java.io.IOException 類PreviewVideoInputService:TvInputService(){ 重寫fun onCreateSession(inputId:String):TvInputService.Session? { 返回PreviewSession(this)} 私人內部類PreviewSession( 內部變數上下文:上下文 ):TvInputService.Session(context){ 內部var mediaPlayer:MediaPlayer? = MediaPlayer() 覆蓋有趣的onRelease(){ 媒體播放器?.release() mediaPlayer = null } 重寫fun onTune(uri:Uri):Boolean { //讓TvInputService知道視訊正在載入。notifyVideoUnavailable(VIDEO_UNAVAILABLE_REASON_TUNING) //從TV Provider資料庫獲取流url //用於內容://android.media.tv/preview_program/ val id = uri.lastPathSegment //在後臺載入視訊。 retrieveYourVideoPreviewUrl(id){videoUri - > 如果(videoUri == null){Log.d(TAG,“找不到視訊$ id”) notifyVideoUnavailable(TvInputManager.VIDEO_UNAVAILABLE_REASON_UNKNOWN) } 嘗試{ mPlayer.setDataSource(getApplicationContext(),videoUri) mPlayer.prepare() mPlayer.start() notifyVideoAvailable() } catch(IOException e){ Log.e(標籤,“無法準備媒體播放器”,e) notifyVideoUnavailable(TvInputManager.VIDEO_UNAVAILABLE_REASON_UNKNOWN) } } 返回true } 覆蓋樂趣onSetSurface(surface:Surface?):Boolean { MEDIAPLAYER?.setSurface(表面) 返回true } 覆蓋樂趣onSetStreamVolume(音量:浮動){ //主螢幕可能會淡入淡出視訊的音量。 //你的播放器應該相應地更新。 mediaPlayer?.setVolume(音量,音量) } 在SetCaptionEnabled上重寫fun(b:Boolean){ //在此處啟用/停用字幕 } } 伴侶物件{ private const val TAG =“PreviewInputService” } }
Java的
import android.content.Context; import android.media.MediaPlayer; import android.media.tv.TvInputService; 匯入android.net.Uri; 匯入android.support.annotation.Nullable; 匯入android.util.Log; 匯入android.view.Surface; import java.io.IOException; 公共類PreviewVideoInputService繼承TvInputService { private static final String TAG =“PreviewVideoInputService”; @Nullable @覆蓋 public Session onCreateSession(String inputId){ 返回新的PreviewSession(this); } 私人類PreviewSession繼承TvInputService.Session { 私人MediaPlayer mPlayer; PreviewSession(上下文上下文){ 超級(上下文); mPlayer = new MediaPlayer(); } @覆蓋 public boolean onTune(Uri channelUri){ //讓TvInputService知道視訊正在載入。 notifyVideoUnavailable(VIDEO_UNAVAILABLE_REASON_TUNING); //從TV Provider資料庫獲取流url //用於內容://android.media.tv/preview_program/ String id = uri.getLastPathSegment(); //在後臺載入視訊。 retrieveYourVideoPreviewUrl(id,new MyCallback(){ public void callback(Uri videoUri){ 如果(videoUri == null){ Log.d(TAG,“找不到視訊”+ id); notifyVideoUnavailable(TvInputManager.VIDEO_UNAVAILABLE_REASON_UNKNOWN); } 嘗試{ mPlayer.setDataSource(getApplicationContext(),videoUri); mPlayer.prepare(); mPlayer.start(); notifyVideoAvailable(); } catch(IOException e){ Log.e(標籤,“無法準備媒體播放器”,e); notifyVideoUnavailable(TvInputManager.VIDEO_UNAVAILABLE_REASON_UNKNOWN); } } }); 返回true; } @覆蓋 public boolean onSetSurface(@Nullable Surface surface){ if(mPlayer!= null){ mPlayer.setSurface(表面); } 返回true; } @覆蓋 public void onRelease(){ if(mPlayer!= null){ mPlayer.release(); } mPlayer = null; } @覆蓋 public void onSetStreamVolume(float volume){ if(mPlayer!= null){ //主螢幕可能會淡入淡出視訊的音量。 //你的播放器應該相應地更新。 mPlayer.setVolume(volume,volume); } } @覆蓋 public void onSetCaptionEnabled(boolean enabled){ //在此處啟用/停用字幕 } } }