ในประสบการณ์การใช้งานรายการทีวีสด ผู้ใช้จะเปลี่ยนช่องและเห็นข้อมูลช่องและรายการทีวีสั้นๆ ก่อนที่ข้อมูลจะหายไป ข้อมูลประเภทอื่นๆ เช่น ข้อความ ("ห้ามลองทำที่บ้าน") คำบรรยาย หรือโฆษณาอาจต้องแสดงต่อไป เช่นเดียวกับแอปทีวีอื่นๆ ข้อมูลดังกล่าวไม่ควรไปรบกวนเนื้อหารายการทีวีที่เล่นบนหน้าจอ
นอกจากนี้ ให้พิจารณาด้วยว่าควรแสดงเนื้อหารายการทีวีบางรายการหรือไม่ โดยพิจารณาจากการจัดประเภทเนื้อหาและการตั้งค่าการควบคุมโดยผู้ปกครอง รวมถึงพิจารณาว่าแอปของคุณทำงานอย่างไรและแจ้งให้ผู้ใช้ทราบอย่างไรเมื่อเนื้อหาถูกบล็อกหรือไม่พร้อมใช้งาน บทเรียนนี้อธิบายวิธีพัฒนาประสบการณ์ของผู้ใช้ของอินพุตทีวีเพื่อให้สอดคล้องกับข้อควรพิจารณาเหล่านี้
ลองใช้แอปตัวอย่าง TV Input Service
ผสานรวมเพลเยอร์กับ Surface
อินพุตทีวีต้องแสดงวิดีโอลงในออบเจ็กต์ Surface ซึ่งส่งผ่านโดย
เมธอด TvInputService.Session.onSetSurface()
ต่อไปนี้เป็นตัวอย่างวิธีใช้อินสแตนซ์ MediaPlayer เพื่อเล่นเนื้อหาในออบเจ็กต์ Surface
Kotlin
override fun onSetSurface(surface: Surface?): Boolean { player?.setSurface(surface) mSurface = surface return true } override fun onSetStreamVolume(volume: Float) { player?.setVolume(volume, volume) mVolume = volume }
Java
@Override public boolean onSetSurface(Surface surface) { if (player != null) { player.setSurface(surface); } mSurface = surface; return true; } @Override public void onSetStreamVolume(float volume) { if (player != null) { player.setVolume(volume, volume); } mVolume = volume; }
ในทำนองเดียวกัน ต่อไปนี้เป็นวิธีใช้ ExoPlayer:
Kotlin
override fun onSetSurface(surface: Surface?): Boolean { player?.createMessage(videoRenderer)?.apply { type = MSG_SET_SURFACE payload = surface send() } mSurface = surface return true } override fun onSetStreamVolume(volume: Float) { player?.createMessage(audioRenderer)?.apply { type = MSG_SET_VOLUME payload = volume send() } mVolume = volume }
Java
@Override public boolean onSetSurface(@Nullable Surface surface) { if (player != null) { player.createMessage(videoRenderer) .setType(MSG_SET_SURFACE) .setPayload(surface) .send(); } mSurface = surface; return true; } @Override public void onSetStreamVolume(float volume) { if (player != null) { player.createMessage(videoRenderer) .setType(MSG_SET_VOLUME) .setPayload(volume) .send(); } mVolume = volume; }
ใช้การซ้อนทับ
ใช้การซ้อนทับเพื่อแสดงคำบรรยาย ข้อความ โฆษณา หรือการออกอากาศข้อมูล MHEG-5 การซ้อนทับจะปิดใช้อยู่โดยค่าเริ่มต้น คุณเปิดใช้ได้เมื่อสร้างเซสชันโดยเรียกใช้ TvInputService.Session.setOverlayViewEnabled(true) ดังตัวอย่างต่อไปนี้
Kotlin
override fun onCreateSession(inputId: String): Session = onCreateSessionInternal(inputId).apply { setOverlayViewEnabled(true) sessions.add(this) }
Java
@Override public final Session onCreateSession(String inputId) { BaseTvInputSessionImpl session = onCreateSessionInternal(inputId); session.setOverlayViewEnabled(true); sessions.add(session); return session; }
ใช้ออบเจ็กต์ View สำหรับการซ้อนทับ ซึ่งส่งคืนจาก TvInputService.Session.onCreateOverlayView() ดังที่แสดงไว้ที่นี่
Kotlin
override fun onCreateOverlayView(): View = (context.getSystemService(LAYOUT_INFLATER_SERVICE) as LayoutInflater).run { inflate(R.layout.overlayview, null).apply { subtitleView = findViewById<SubtitleView>(R.id.subtitles).apply { // Configure the subtitle view. val captionStyle: CaptionStyleCompat = CaptionStyleCompat.createFromCaptionStyle(captioningManager.userStyle) setStyle(captionStyle) setFractionalTextSize(captioningManager.fontScale) } } }
Java
@Override public View onCreateOverlayView() { LayoutInflater inflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE); View view = inflater.inflate(R.layout.overlayview, null); subtitleView = (SubtitleView) view.findViewById(R.id.subtitles); // Configure the subtitle view. CaptionStyleCompat captionStyle; captionStyle = CaptionStyleCompat.createFromCaptionStyle( captioningManager.getUserStyle()); subtitleView.setStyle(captionStyle); subtitleView.setFractionalTextSize(captioningManager.fontScale); return view; }
คำจำกัดความของเลย์เอาต์สำหรับการซ้อนทับอาจมีลักษณะดังนี้
<?xml version="1.0" encoding="utf-8"?> <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent"> <com.google.android.exoplayer.text.SubtitleView android:id="@+id/subtitles" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="bottom|center_horizontal" android:layout_marginLeft="16dp" android:layout_marginRight="16dp" android:layout_marginBottom="32dp" android:visibility="invisible"/> </FrameLayout>
ควบคุมเนื้อหา
เมื่อผู้ใช้เลือกช่อง อินพุตทีวีจะจัดการการเรียกกลับ onTune() ในออบเจ็กต์ TvInputService.Session การควบคุมโดยผู้ปกครองของแอปทีวีในระบบจะเป็นตัวกำหนดเนื้อหาที่จะแสดงโดยพิจารณาจากการจัดประเภทเนื้อหา
ส่วนต่อไปนี้อธิบายวิธีจัดการการเลือกช่องและรายการทีวีโดยใช้เมธอด notify ของ TvInputService.Session ซึ่งสื่อสารกับแอป TV ในระบบ
ทำให้วิดีโอไม่พร้อมใช้งาน
เมื่อผู้ใช้เปลี่ยนช่อง คุณต้องตรวจสอบว่าหน้าจอไม่แสดงสิ่งประดิษฐ์วิดีโอที่หลงเหลืออยู่ก่อนที่อินพุตทีวีจะแสดงเนื้อหา เมื่อเรียกใช้ TvInputService.Session.onTune() คุณจะป้องกันไม่ให้วิดีโอแสดงได้โดยเรียกใช้ TvInputService.Session.notifyVideoUnavailable() และส่งค่าคงที่ VIDEO_UNAVAILABLE_REASON_TUNING ดังตัวอย่างต่อไปนี้
Kotlin
override fun onTune(channelUri: Uri): Boolean { subtitleView?.visibility = View.INVISIBLE notifyVideoUnavailable(TvInputManager.VIDEO_UNAVAILABLE_REASON_TUNING) unblockedRatingSet.clear() dbHandler.apply { removeCallbacks(playCurrentProgramRunnable) playCurrentProgramRunnable = PlayCurrentProgramRunnable(channelUri) post(playCurrentProgramRunnable) } return true }
Java
@Override public boolean onTune(Uri channelUri) { if (subtitleView != null) { subtitleView.setVisibility(View.INVISIBLE); } notifyVideoUnavailable(TvInputManager.VIDEO_UNAVAILABLE_REASON_TUNING); unblockedRatingSet.clear(); dbHandler.removeCallbacks(playCurrentProgramRunnable); playCurrentProgramRunnable = new PlayCurrentProgramRunnable(channelUri); dbHandler.post(playCurrentProgramRunnable); return true; }
จากนั้นเมื่อแสดงเนื้อหาใน Surface แล้ว ให้เรียกใช้ TvInputService.Session.notifyVideoAvailable() เพื่ออนุญาตให้วิดีโอแสดง ดังนี้
Kotlin
fun onRenderedFirstFrame(surface:Surface) { firstFrameDrawn = true notifyVideoAvailable() }
Java
@Override public void onRenderedFirstFrame(Surface surface) { firstFrameDrawn = true; notifyVideoAvailable(); }
การเปลี่ยนผ่านนี้ใช้เวลาเพียงเสี้ยววินาที แต่การแสดงหน้าจอว่างจะดูดีกว่าการอนุญาตให้ภาพกระพริบเป็นจุดเล็กๆ และสั่น
ดูข้อมูลเพิ่มเติมเกี่ยวกับการทำงาน
กับ Surface เพื่อแสดงวิดีโอได้ที่ผสานรวมเพลเยอร์กับ Surface
ให้การควบคุมโดยผู้ปกครอง
หากต้องการตรวจสอบว่าเนื้อหาหนึ่งๆ ถูกบล็อกโดยการควบคุมโดยผู้ปกครองและการจัดประเภทเนื้อหาหรือไม่ ให้ตรวจสอบเมธอดของคลาส TvInputManager ได้แก่ isParentalControlsEnabled() และ isRatingBlocked(android.media.tv.TvContentRating) นอกจากนี้ คุณอาจต้องตรวจสอบว่า TvContentRating ของเนื้อหารวมอยู่ในชุดการจัดประเภทเนื้อหาที่อนุญาตในปัจจุบัน ข้อควรพิจารณาเหล่านี้แสดงอยู่ในตัวอย่างต่อไปนี้
Kotlin
private fun checkContentBlockNeeded() { currentContentRating?.also { rating -> if (!tvInputManager.isParentalControlsEnabled || !tvInputManager.isRatingBlocked(rating) || unblockedRatingSet.contains(rating)) { // Content rating is changed so we don't need to block anymore. // Unblock content here explicitly to resume playback. unblockContent(null) return } } lastBlockedRating = currentContentRating player?.run { // Children restricted content might be blocked by TV app as well, // but TIF should do its best not to show any single frame of blocked content. releasePlayer() } notifyContentBlocked(currentContentRating) }
Java
private void checkContentBlockNeeded() { if (currentContentRating == null || !tvInputManager.isParentalControlsEnabled() || !tvInputManager.isRatingBlocked(currentContentRating) || unblockedRatingSet.contains(currentContentRating)) { // Content rating is changed so we don't need to block anymore. // Unblock content here explicitly to resume playback. unblockContent(null); return; } lastBlockedRating = currentContentRating; if (player != null) { // Children restricted content might be blocked by TV app as well, // but TIF should do its best not to show any single frame of blocked content. releasePlayer(); } notifyContentBlocked(currentContentRating); }
เมื่อพิจารณาแล้วว่าควรบล็อกเนื้อหาหรือไม่ ให้แจ้งแอปทีวีในระบบโดยเรียกใช้เมธอด notifyContentAllowed() หรือ notifyContentBlocked() ของ TvInputService.Session ดังที่แสดงในตัวอย่างก่อนหน้า
ใช้คลาส TvContentRating เพื่อสร้างสตริงที่ระบบกำหนดไว้สำหรับ
COLUMN_CONTENT_RATING ด้วย
TvContentRating.createRating()
เมธอด ดังที่แสดงไว้ที่นี่
Kotlin
val rating = TvContentRating.createRating( "com.android.tv", "US_TV", "US_TV_PG", "US_TV_D", "US_TV_L" )
Java
TvContentRating rating = TvContentRating.createRating( "com.android.tv", "US_TV", "US_TV_PG", "US_TV_D", "US_TV_L");
จัดการการเลือกแทร็ก
คลาส TvTrackInfo มีข้อมูลเกี่ยวกับแทร็กสื่อ เช่น ประเภทแทร็ก (วิดีโอ เสียง หรือคำบรรยาย) และอื่นๆ
เมื่อเซสชันอินพุตทีวีสามารถรับข้อมูลแทร็กได้เป็นครั้งแรก เซสชันควรเรียกใช้
TvInputService.Session.notifyTracksChanged() พร้อมรายการแทร็กทั้งหมดเพื่ออัปเดตแอป TV ในระบบ เมื่อมี
การเปลี่ยนแปลงในข้อมูลแทร็ก ให้เรียกใช้
notifyTracksChanged()
อีกครั้งเพื่ออัปเดตระบบ
แอป TV ในระบบมีอินเทอร์เฟซให้ผู้ใช้เลือกแทร็กที่เฉพาะเจาะจงหากมีแทร็กมากกว่า 1 รายการสำหรับแทร็กประเภทหนึ่งๆ เช่น คำบรรยายในภาษาต่างๆ อินพุตทีวีจะตอบสนองต่อการเรียก onSelectTrack() จากแอป TV ในระบบโดยเรียกใช้ notifyTrackSelected() ดังตัวอย่างต่อไปนี้ โปรดทราบว่าเมื่อส่ง null เป็นรหัสแทร็ก ระบบจะ ยกเลิกการเลือก แทร็ก
Kotlin
override fun onSelectTrack(type: Int, trackId: String?): Boolean = mPlayer?.let { player -> if (type == TvTrackInfo.TYPE_SUBTITLE) { if (!captionEnabled && trackId != null) return false selectedSubtitleTrackId = trackId subtitleView.visibility = if (trackId == null) View.INVISIBLE else View.VISIBLE } player.trackInfo.indexOfFirst { it.trackType == type }.let { trackIndex -> if( trackIndex >= 0) { player.selectTrack(trackIndex) notifyTrackSelected(type, trackId) true } else false } } ?: false
Java
@Override public boolean onSelectTrack(int type, String trackId) { if (player != null) { if (type == TvTrackInfo.TYPE_SUBTITLE) { if (!captionEnabled && trackId != null) { return false; } selectedSubtitleTrackId = trackId; if (trackId == null) { subtitleView.setVisibility(View.INVISIBLE); } } int trackIndex = -1; MediaPlayer.TrackInfo[] trackInfos = player.getTrackInfo(); for (int index = 0; index < trackInfos.length; index++) { MediaPlayer.TrackInfo trackInfo = trackInfos[index]; if (trackInfo.getTrackType() == type) { trackIndex = index; break; } } if (trackIndex >= 0) { player.selectTrack(trackIndex); notifyTrackSelected(type, trackId); return true; } } return false; }