支持内容录制
使用集合让一切井井有条
根据您的偏好保存内容并对其进行分类。
借助 TV 输入服务,用户可以使用
时移 API。
Android 7.0 扩展了时移功能
方法是允许用户保存多个录制的会话
用户可以提前安排录制时间,也可以在观看节目时开始录制
一个程序。系统保存录音后,用户可以浏览、管理和
并使用系统 TV 应用播放录制的内容。
如果您想为 TV 输入服务提供录制功能,
您必须向系统表明您的应用支持录制,
能够录制节目、处理和传达发生的任何错误
以及管理录制的会话
指明支持录制
如需告知系统您的 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" />
如需详细了解服务元数据文件,请参阅
声明 TV 输入源
服务。
或者,您也可以使用
具体步骤:
- 在 TV 输入服务
onCreate()
中
方法,请使用TvInputInfo
TvInputInfo.Builder
类。
- 创建新的
TvInputInfo
对象时,请调用
通话前 setCanRecord(true)
build()
即表示
服务支持录制。
- 通过调用在系统中注册
TvInputInfo
对象
TvInputManager.updateTvInputInfo()
。
录制会话
TV 输入服务注册其支持录制后
功能时,系统会调用您的
TvInputService.onCreateRecordingSession()
方法需要访问时
应用的录制实现。实现您自己的
TvInputService.RecordingSession
子类并将其返回
在 onCreateRecordingSession()
回调触发时触发。这个子类负责
用于切换到正确的频道数据、记录请求的数据;
以及向系统传达录制状态和错误
当系统调用
RecordingSession.onTune()
,传入频道 URI,调到该频道
URI 所指定的地址通知系统您的应用已调到
调用所需的频道
notifyTuned()
,或者,如果您的应用无法调到正确的频道,请调用
notifyError()
。
接下来,系统会调用
RecordingSession.onStartRecording()
回调。您的应用必须开始录制
。当系统调用此回调时,可能会提供一个 URI
,其中包含即将录制的节目的相关信息。
录制完成后,请将此数据复制到
RecordedPrograms
数据表。
最后,系统会调用
RecordingSession.onStopRecording()
。此时,您的应用必须停止
录音。您还需要在
RecordedPrograms
表格(在
RecordedPrograms.COLUMN_RECORDING_DATA_URI
列,以及任何计划
系统首次调用
onStartRecording()
。
如需详细了解如何访问
RecordedPrograms
个表,
请参阅管理录制的会话部分。
处理录制错误
如果录制期间发生错误,导致录制的数据无法使用,
通过调用
notifyError()
。
您也可以呼叫
创建录制会话后发出 notifyError()
指令,以通知系统
您的应用无法再录制会话。
如果在录制过程中发生错误,但您希望提供
将部分录音发送给用户播放、
notifyRecordingStopped()
,使系统能够
使用部分会话。
管理录制的会话
系统会保留来自各个广告系列的所有已录制会话的信息
支持录制的频道应用
RecordedPrograms
content provider 表。此信息可通过
RecordedPrograms
内容录制 URI。使用 content provider API 实现以下目的:
读取、添加和删除此表中的条目。
如需详细了解如何使用内容提供程序数据,请参阅
<ph type="x-smartling-placeholder"></ph>
content provider 基础知识。
最佳做法
TV 设备的存储空间可能有限,因此请自行判断
分配存储空间以保存录制的会话。使用
当发生以下情况时,RecordingCallback.onError(RECORDING_ERROR_INSUFFICIENT_SPACE)
空间不足,无法保存录制的会话。
当用户发起录制时,尽快开始录制数据
。为此,请完成所有预先耗时的任务
比如访问和分配存储空间
onCreateRecordingSession()
回调。这样你就可以开始
立即录制
触发 onStartRecording()
回调。
本页面上的内容和代码示例受内容许可部分所述许可的限制。Java 和 OpenJDK 是 Oracle 和/或其关联公司的注册商标。
最后更新时间 (UTC):2025-07-27。
[null,null,["最后更新时间 (UTC):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."]]