支援內容錄製
透過集合功能整理內容
你可以依據偏好儲存及分類內容。
電視輸入服務可讓使用者透過
時間移轉 API。
Android 7.0 隨時間轉移擴充資料
讓使用者儲存多個已記錄的工作階段。
使用者可以預先排定錄製時間,也可以在觀看時開始錄影
方案。系統儲存錄製內容後,使用者就能瀏覽、管理
然後使用系統 TV 應用程式播放錄製內容。
如要在電視輸入服務中提供錄製功能,
您必須向系統說明您的應用程式支援錄製、實作
能夠錄製程式、處理並傳達任何發生的錯誤
,以及管理錄影作業。
說明錄製功能支援
如要讓系統知道你的電視輸入服務支援錄影功能,請設定以下選項:
服務中繼資料 XML 檔案中的 android:canRecord
屬性
至 true
:
<tv-input xmlns:android="http://schemas.android.com/apk/res/android"
android:canRecord="true"
android:setupActivity="com.example.sampletvinput.SampleTvInputSetupActivity" />
如要進一步瞭解服務中繼資料檔案,請參閱
宣告電視輸入來源
定義服務
或者,您也可以使用以下項目,在程式碼中指出記錄支援:
步驟如下:
- 透過電視輸入服務「
onCreate()
」
方法,建立新的 TvInputInfo
物件 (使用
TvInputInfo.Builder
類別。
- 建立新的
TvInputInfo
物件時,請呼叫
撥打電話前須等待 setCanRecord(true)
build()
表示
服務支援錄製功能。
- 透過呼叫
TvInputInfo
TvInputManager.updateTvInputInfo()
。
錄製課程
電視輸入服務註冊支援錄製功能後
系統會呼叫
TvInputService.onCreateRecordingSession()
方法 (必須在需要存取)
應用程式內的錄製實作。自行實作
TvInputService.RecordingSession
子類別並傳回
onCreateRecordingSession()
回呼觸發時。這個子類別負責
記錄要求的資料 (例如切換至正確的頻道資料)
以及將記錄狀態和錯誤傳達給系統。
當系統呼叫
RecordingSession.onTune()
,傳入頻道 URI,請調整至頻道
以及 URI 指定的通知系統您的應用程式已根據
呼叫所需管道,
notifyTuned()
或者,如果應用程式無法調到正確的頻道,請呼叫
notifyError()
。
接著系統會叫用
RecordingSession.onStartRecording()
回呼。您的應用程式必須開始錄製
立即生效系統叫用這個回呼時,可能會提供 URI
,其中包含即將要記錄的節目相關資訊。
記錄完成後,請將資料複製到
RecordedPrograms
資料表中的資料,
最後,系統會呼叫
RecordingSession.onStopRecording()
。此時,您的應用程式必須停止
錄製影片您也需要在
RecordedPrograms
這個表格列出了所記錄的工作階段資料 URI
RecordedPrograms.COLUMN_RECORDING_DATA_URI
欄和任何節目
系統在初次呼叫
onStartRecording()
。
想進一步瞭解如何存取
RecordedPrograms
資料表,
請參閱「管理已錄製的工作階段」部分。
處理錄製錯誤
如果錄音時發生錯誤,導致記錄資料無法使用,
透過
notifyError()
。
你也可以撥打
建立錄製工作階段後notifyError()
,讓系統知道
您的應用程式無法再記錄工作階段。
如果錄製時發生錯誤,但你想提供
僅供使用者播放部分錄影,請呼叫
notifyRecordingStopped()
:讓系統
使用部分工作階段
管理錄製的工作階段
系統會維護所有已記錄工作階段的資訊
支援錄製功能的頻道應用程式
RecordedPrograms
內容供應器表格。您可透過
RecordedPrograms
內容錄製 URI將內容供應器 API 用於
讀取、新增及刪除這個資料表中的項目。
如要進一步瞭解如何使用內容供應器資料,請參閱
內容供應器基礎知識。
最佳做法
電視裝置的儲存空間可能有限,因此請在
正在分配儲存空間,以便儲存已記錄的工作階段。使用
RecordingCallback.onError(RECORDING_ERROR_INSUFFICIENT_SPACE)
時間
空間不足,無法儲存錄製的工作階段。
使用者啟動記錄作業後,應立即開始記錄資料
。為了方便起見,請先完成所有前置費時
例如,當系統叫用
onCreateRecordingSession()
回呼。這麼做可以先
即時錄影
onStartRecording()
回呼會觸發。
這個頁面中的內容和程式碼範例均受《內容授權》中的授權所規範。Java 與 OpenJDK 是 Oracle 和/或其關係企業的商標或註冊商標。
上次更新時間:2025-07-27 (世界標準時間)。
[[["容易理解","easyToUnderstand","thumb-up"],["確實解決了我的問題","solvedMyProblem","thumb-up"],["其他","otherUp","thumb-up"]],[["缺少我需要的資訊","missingTheInformationINeed","thumb-down"],["過於複雜/步驟過多","tooComplicatedTooManySteps","thumb-down"],["過時","outOfDate","thumb-down"],["翻譯問題","translationIssue","thumb-down"],["示例/程式碼問題","samplesCodeIssue","thumb-down"],["其他","otherDown","thumb-down"]],["上次更新時間:2025-07-27 (世界標準時間)。"],[],[],null,["# Support content recording\n\nTV input services let the user pause and resume channel playback using\n[time-shifting APIs](/training/tv/tif/time-shifting).\nAndroid 7.0 expands on time-shifting\nby letting the user save multiple recorded sessions.\n\nUsers can schedule recordings in advance or start a recording as they watch\na program. Once the system saves a recording, the user can browse, manage,\nand play back the recording using the system TV app.\n\nIf you want to provide recording functionality for your TV input service,\nyou must indicate to the system that your app supports recording, implement\nthe ability to record programs, handle and communicate any errors that occur\nduring recording, and manage your recorded sessions.\n\nIndicate support for recording\n------------------------------\n\nTo tell the system that your TV input service supports recording, set\nthe `android:canRecord` attribute in your service metadata XML file\nto `true`: \n\n```xml\n\u003ctv-input xmlns:android=\"http://schemas.android.com/apk/res/android\"\n android:canRecord=\"true\"\n android:setupActivity=\"com.example.sampletvinput.SampleTvInputSetupActivity\" /\u003e\n```\n\nFor more information on the service metadata file, see\n[Declare your TV input\nservice in the manifest](/training/tv/tif/tvinput#manifest).\n\nAlternatively, you can indicate recording support in your code using\nthese steps:\n\n1. In your TV input service [onCreate()](/reference/android/app/Service#onCreate()) method, create a new [TvInputInfo](/reference/android/media/tv/TvInputInfo) object using the [TvInputInfo.Builder](/reference/android/media/tv/TvInputInfo.Builder) class.\n2. When creating the new `TvInputInfo` object, call [setCanRecord(true)](/reference/android/media/tv/TvInputInfo.Builder#setCanRecord(boolean)) before calling [build()](/reference/android/media/tv/TvInputInfo.Builder#build()) to indicate that your service supports recording.\n3. Register your `TvInputInfo` object with the system by calling [TvInputManager.updateTvInputInfo()](/reference/android/media/tv/TvInputManager#updateTvInputInfo(android.media.tv.TvInputInfo)).\n\nRecord a session\n----------------\n\nAfter your TV input service registers that it supports recording\nfunctionality, the system calls your\n[TvInputService.onCreateRecordingSession()](/reference/android/media/tv/TvInputService#onCreateRecordingSession(java.lang.String)) method when it needs to access\nyour app's recording implementation. Implement your own\n[TvInputService.RecordingSession](/reference/android/media/tv/TvInputService.RecordingSession) subclass and return it\nwhen the `onCreateRecordingSession()` callback fires. This subclass is responsible\nfor switching to the correct channel data, recording the requested data,\nand communicating recording status and errors to the system.\n\nWhen the system calls\n[RecordingSession.onTune()](/reference/android/media/tv/TvInputService.RecordingSession#onTune(android.net.Uri)), passing in a channel URI, tune to the channel\nthat the URI specifies. Notify the system that your app has tuned to the\ndesired channel by calling\n[notifyTuned()](/reference/android/media/tv/TvInputService.RecordingSession#notifyTuned(android.net.Uri)) or, if your app can't tune to the proper channel, call\n[notifyError()](/reference/android/media/tv/TvInputService.RecordingSession#notifyError(int)).\n\nThe system next invokes the\n[RecordingSession.onStartRecording()](/reference/android/media/tv/TvInputService.RecordingSession#onStartRecording(android.net.Uri)) callback. Your app must start recording\nimmediately. When the system invokes this callback, it might provide a URI\nthat contains information about the program that is about to be recorded.\nWhen the recording is done, copy this data to the\n[RecordedPrograms](/reference/android/media/tv/TvContract.RecordedPrograms)\ndata table.\n\nFinally, the system calls\n[RecordingSession.onStopRecording()](/reference/android/media/tv/TvInputService.RecordingSession#onStopRecording()). At this point, your app must stop\nrecording immediately. You also need to create an entry in the\n`RecordedPrograms`\ntable that includes the recorded session data URI in the\n[RecordedPrograms.COLUMN_RECORDING_DATA_URI](/reference/android/media/tv/TvContract.RecordedPrograms#COLUMN_RECORDING_DATA_URI) column, and any program\ninformation that the system provided in the initial call to\n`onStartRecording()`.\n\nFor more details on how to access the\n`RecordedPrograms` table,\nsee the [Manage recorded sessions](#sessions) section.\n\nHandle recording errors\n-----------------------\n\nIf an error occurs during recording, resulting in unusable recorded data,\nnotify the system by calling\n[notifyError()](/reference/android/media/tv/TvInputService.RecordingSession#notifyError(int)).\nYou can also call\nnotifyError() after a recording session is created to let the system know\nthat your app can no longer record sessions.\n\nIf an error occurs during recording but you want to provide a\npartial recording to users for playback, call\n[notifyRecordingStopped()](/reference/android/media/tv/TvInputService.RecordingSession#notifyRecordingStopped(android.net.Uri)) to enable the system to\nuse the partial session.\n\nManage recorded sessions\n------------------------\n\nThe system maintains information for all recorded sessions from all\nrecording-capable channel apps in the\n[RecordedPrograms](/reference/android/media/tv/TvContract.RecordedPrograms)\ncontent provider table. This information is accessible through the\nRecordedPrograms\ncontent recording URIs. Use content provider APIs to\nread, add, and delete entries from this table.\n\nFor more information on working with content provider data, see\n[Content provider basics](/guide/topics/providers/content-provider-basics).\n\nBest practices\n--------------\n\nTV devices might have limited storage, so use your best judgment when\nallocating storage to save recorded sessions. Use\n[RecordingCallback.onError(RECORDING_ERROR_INSUFFICIENT_SPACE)](/reference/android/media/tv/TvRecordingClient.RecordingCallback#onError(int)) when\nthere isn't enough space to save a recorded session.\n\nWhen the user initiates recording, start recording data as soon\nas possible. To facilitate this, complete any up-front time-consuming tasks,\nlike accessing and allocating storage space, when the system invokes the\n[onCreateRecordingSession()](/reference/android/media/tv/TvInputService#onCreateRecordingSession(java.lang.String)) callback. Doing so lets you start\nrecording immediately when the\n[onStartRecording()](/reference/android/media/tv/TvInputService.RecordingSession#onStartRecording(android.net.Uri)) callback fires."]]