文档管理中心
您当前正在浏览HarmonyOS最新文档,覆盖已发布的所有API版本,可在API参考中筛选您使用的API版本。详细的版本配套关系请参考版本说明
API参考媒体Ringtone Kit(铃声服务)ArkTS APIringtone(铃声服务)

ringtone(铃声服务)

Phone5.0.0(12)+Tablet5.0.0(12)+

ringtone提供铃声设置的功能。

起始版本: 5.0.0(12)

导入模块

收起
自动换行
深色代码主题
复制
  1. import { ringtone } from '@kit.RingtoneKit';

RingtoneType

Phone5.0.0(12)+Tablet5.0.0(12)+

描述铃声的类型枚举。

模型约束: 此接口仅可在Stage模型下使用。

系统能力: SystemCapability.Ringtone.Core

起始版本: 5.0.0(12)

展开
名称 说明
CALL 0 来电铃声。
MESSAGE 1 信息铃声。
NOTIFICATION 2 通知铃声。
ALARM 3 闹钟铃声。

RingtoneErrors

Phone5.0.0(12)+Tablet5.0.0(12)+

该枚举为设置铃声,获取铃声支持类型和获取铃声支持文件类型等接口的错误码。

模型约束: 此接口仅可在Stage模型下使用。

系统能力: SystemCapability.Ringtone.Core

起始版本: 5.0.0(12)

展开
名称 说明
ERROR_INVALID_PARAM 401 参数非法。
ERROR_USER_CANCELED 1011600001 用户取消。
ERROR_FILE_NOT_FOUND 1011600002 文件不存在。
ERROR_SHOW_FAILED 1011600003 铃声弹框失败。
ERROR_CALL_SYSTEM_API_FAILED 1011600004 调用系统接口失败。
ERROR_DATA_TYPE_NOT_MATCHED 1011600005 文件类型不匹配。起始版本: 26.0.0
ERROR_SYSTEM 1011699999 系统内部错误。

ringtone.getSupportedRingtoneTypes

Phone5.0.0(12)+Tablet5.0.0(12)+

getSupportedRingtoneTypes(): Array<RingtoneType>

查询当前系统支持自定义的铃声类型。

模型约束: 此接口仅可在Stage模型下使用。

系统能力: SystemCapability.Ringtone.Core

起始版本: 5.0.0(12)

返回值:

展开
类型 说明
Array<RingtoneType> 当前系统支持自定义的铃声类型。

示例:

收起
自动换行
深色代码主题
复制
  1. import { ringtone } from '@kit.RingtoneKit';
  2. import { JSON } from '@kit.ArkTS';
  3. import { hilog } from '@kit.PerformanceAnalysisKit';
  4. const APP_TAG = 'Msc_Demo'
  5. const DOMAIN = 0x0001
  6. @Entry
  7. @Component
  8. struct Index {
  9. build() {
  10. Stack() {
  11. Column() {
  12. Button('查询当前系统支持自定义的铃声类型')
  13. .width(200)
  14. .height(50)
  15. .onClick(() => {
  16. let typeList: ringtone.RingtoneType[] = ringtone.getSupportedRingtoneTypes()
  17. hilog.info(DOMAIN, APP_TAG, `getSupportedRingtoneTypes: ${JSON.stringify(typeList)}`);
  18. })
  19. }
  20. .width('100%')
  21. .height('100%')
  22. .backgroundColor(Color.Pink)
  23. }
  24. .height('100%')
  25. .width('100%')
  26. }
  27. }

ringtone.getSupportedRingtoneTypes

Phone26.0.0+Tablet26.0.0+

getSupportedRingtoneTypes(mediaType: uniformTypeDescriptor.UniformDataType): Array<RingtoneType>

查询当前系统支持自定义的铃声类型。

模型约束: 此接口仅可在Stage模型下使用。

系统能力: SystemCapability.Ringtone.Core

起始版本: 26.0.0

参数:

展开
参数名 类型 必填 说明
mediaType uniformTypeDescriptor.UniformDataType 待查询的文件类型。

返回值:

展开
类型 说明
Array<RingtoneType> 当前系统支持自定义的铃声类型。

示例:

收起
自动换行
深色代码主题
复制
  1. import { ringtone } from '@kit.RingtoneKit';
  2. import { JSON } from '@kit.ArkTS';
  3. import { uniformTypeDescriptor } from '@kit.ArkData';
  4. import { hilog } from '@kit.PerformanceAnalysisKit';
  5. const APP_TAG = 'Msc_Demo'
  6. const DOMAIN = 0x0001
  7. @Entry
  8. @Component
  9. struct Index {
  10. build() {
  11. Stack() {
  12. Column() {
  13. Button('查询当前系统支持自定义的铃声类型')
  14. .width(200)
  15. .height(50)
  16. .onClick(() => {
  17. let typeList: ringtone.RingtoneType[] = ringtone.getSupportedRingtoneTypes(uniformTypeDescriptor.UniformDataType.AUDIO)
  18. hilog.info(DOMAIN, APP_TAG, `getSupportedRingtoneTypes: ${JSON.stringify(typeList)}`);
  19. })
  20. }
  21. .width('100%')
  22. .height('100%')
  23. .backgroundColor(Color.Pink)
  24. }
  25. .height('100%')
  26. .width('100%')
  27. }
  28. }

ringtone.getSupportedDataTypes

Phone5.0.0(12)+Tablet5.0.0(12)+

getSupportedDataTypes(ringtoneType: RingtoneType): Array<uniformTypeDescriptor.UniformDataType>

查询对应铃声类型支持的文件类型。

模型约束: 此接口仅可在Stage模型下使用。

系统能力:SystemCapability.Ringtone.Core

起始版本: 5.0.0(12)

参数:

展开
参数名 类型 必填 说明
ringtoneType RingtoneType 待查询的铃声类型。

返回值:

展开
类型 说明
Array<uniformTypeDescriptor.UniformDataType> 返回对应铃声类型支持的文件类型。

错误码:

以下错误码的详细介绍请参见通用错误码

展开
错误码ID 错误信息
401 Parameter error. Possible causes:ringtoneType is invalid.

示例:

收起
自动换行
深色代码主题
复制
  1. import { ringtone } from '@kit.RingtoneKit';
  2. import { BusinessError } from '@kit.BasicServicesKit';
  3. import { uniformTypeDescriptor } from '@kit.ArkData';
  4. import { JSON } from '@kit.ArkTS';
  5. import { hilog } from '@kit.PerformanceAnalysisKit';
  6. const APP_TAG = 'Msc_Demo'
  7. const DOMAIN = 0x0001
  8. @Entry
  9. @Component
  10. struct Index {
  11. build() {
  12. Stack() {
  13. Column() {
  14. Button('查询支持的文件类型')
  15. .width(200)
  16. .height(50)
  17. .onClick(() => {
  18. try {
  19. let typeList: uniformTypeDescriptor.UniformDataType[] =
  20. ringtone.getSupportedDataTypes(ringtone.RingtoneType.NOTIFICATION)
  21. hilog.info(DOMAIN, APP_TAG, `getSupportedDataType: ${JSON.stringify(typeList)}`);
  22. } catch (error) {
  23. let err: BusinessError = error as BusinessError;
  24. hilog.error(DOMAIN, APP_TAG,
  25. `getSupportedDataType error message: ${err.message}, error code: ${err.code}`);
  26. }
  27. })
  28. }
  29. .width('100%')
  30. .height('100%')
  31. .backgroundColor(Color.Pink)
  32. }
  33. .height('100%')
  34. .width('100%')
  35. }
  36. }

ringtone.getSupportedMaxDuration

Phone5.0.0(12)+Tablet5.0.0(12)+

getSupportedMaxDuration(ringtoneType: RingtoneType, dataType: uniformTypeDescriptor.UniformDataType): number

查询不同铃声类型和文件类型对应的文件时长上限。

模型约束: 此接口仅可在Stage模型下使用。

系统能力:SystemCapability.Ringtone.Core

起始版本: 5.0.0(12)

参数:

展开
参数名 类型 必填 说明
ringtoneType RingtoneType 待查询的铃声类型。
dataType uniformTypeDescriptor.UniformDataType 待查询的文件类型。

返回值:

展开
类型 说明
number 返回对应类型的铃声和文件支持的最大时长(单位:s),其中闹钟铃声时长为300s,短信铃声和通知铃声时长为7s,来电铃声时长为60s。

错误码:

以下错误码的详细介绍请参见通用错误码

展开
错误码ID 错误信息
401 Parameter error. Possible causes:data type does not match the ringtone type.

示例:

收起
自动换行
深色代码主题
复制
  1. import { ringtone } from '@kit.RingtoneKit';
  2. import { BusinessError } from '@kit.BasicServicesKit';
  3. import { uniformTypeDescriptor } from '@kit.ArkData';
  4. import { hilog } from '@kit.PerformanceAnalysisKit';
  5. const APP_TAG = 'Msc_Demo'
  6. const DOMAIN = 0x0001
  7. @Entry
  8. @Component
  9. struct Index {
  10. build() {
  11. Stack() {
  12. Column() {
  13. Button('查询最大时长')
  14. .width(200)
  15. .height(50)
  16. .onClick(() => {
  17. try {
  18. let maxDuration: number =
  19. ringtone.getSupportedMaxDuration(ringtone.RingtoneType.MESSAGE,
  20. uniformTypeDescriptor.UniformDataType.MP3)
  21. hilog.info(DOMAIN, APP_TAG, `getSupportedMaxDuration: ${maxDuration}`);
  22. } catch (error) {
  23. let err: BusinessError = error as BusinessError;
  24. hilog.error(DOMAIN, APP_TAG,
  25. `getSupportedMaxDuration error message: ${err.message}, error code: ${err.code}`);
  26. }
  27. })
  28. }
  29. .width('100%')
  30. .height('100%')
  31. .backgroundColor(Color.Pink)
  32. }
  33. .height('100%')
  34. .width('100%')
  35. }
  36. }

ringtone.getSupportedMaxSize

Phone26.0.0+Tablet26.0.0+

getSupportedMaxSize(ringtoneType: RingtoneType, dataType: uniformTypeDescriptor.UniformDataType): number

查询不同铃声类型和文件类型对应的文件大小上限。

模型约束: 此接口仅可在Stage模型下使用。

系统能力:SystemCapability.Ringtone.Core

起始版本: 26.0.0

参数:

展开
参数名 类型 必填 说明
ringtoneType RingtoneType 待查询的铃声类型。
dataType uniformTypeDescriptor.UniformDataType 待查询的文件类型。

返回值:

展开
类型 说明
number 返回对应类型的铃声和文件支持的最大文件大小(单位:kb),其中视频大小限制200MB及以下,音频大小无限制返回-1。

错误码:

以下错误码的详细介绍请参见通用错误码ArkTS API错误码

展开
错误码ID 错误信息
1011600005 The data type does not match the ringtone type.

示例:

收起
自动换行
深色代码主题
复制
  1. import { ringtone } from '@kit.RingtoneKit';
  2. import { BusinessError } from '@kit.BasicServicesKit';
  3. import { uniformTypeDescriptor } from '@kit.ArkData';
  4. import { hilog } from '@kit.PerformanceAnalysisKit';
  5. const APP_TAG = 'Msc_Demo'
  6. const DOMAIN = 0x0001
  7. @Entry
  8. @Component
  9. struct Index {
  10. build() {
  11. Stack() {
  12. Column() {
  13. Button('查询文件大小限制')
  14. .width(200)
  15. .height(50)
  16. .onClick(() => {
  17. try {
  18. let maxSize: number =
  19. ringtone.getSupportedMaxSize(ringtone.RingtoneType.CALL,
  20. uniformTypeDescriptor.UniformDataType.MP3)
  21. hilog.info(DOMAIN, APP_TAG, `getSupportedMaxSize: ${maxSize}`);
  22. } catch (error) {
  23. let err: BusinessError = error as BusinessError;
  24. hilog.error(DOMAIN, APP_TAG,
  25. `getSupportedMaxSize error message: ${err.message}, error code: ${err.code}`);
  26. }
  27. })
  28. }
  29. .width('100%')
  30. .height('100%')
  31. .backgroundColor(Color.Pink)
  32. }
  33. .height('100%')
  34. .width('100%')
  35. }
  36. }

ringtone.startRingtoneSetting

Phone5.0.0(12)+Tablet5.0.0(12)+

startRingtoneSetting(context: common.UIAbilityContext, path: string, name: string, callback: AsyncCallback<RingtoneType>): void

拉起设置铃声弹窗,并返回点击的铃声类型,使用Callback异步回调。

模型约束: 此接口仅可在Stage模型下使用。

系统能力:SystemCapability.Ringtone.Core

起始版本: 5.0.0(12)

参数:

展开
参数名 类型 必填 说明
context common.UIAbilityContext UIAbility上下文。
path string 具有访问权限的文件路径。
name string 文件名,限制长度1000字符。
callback AsyncCallback<RingtoneType> 回调函数。返回用户选择设置的铃声类型。

错误码:

以下错误码的详细介绍请参见通用错误码ArkTS API错误码

展开
错误码ID 错误信息
401 Parameter error. Possible causes: context is invalid.
1011600001 User canceled.
1011600002 The media file is not found.
1011600003 Failed to show the dialog box.
1011600004 Failed to call the system API.
1011699999 System exception.

示例:

收起
自动换行
深色代码主题
复制
  1. import { common } from '@kit.AbilityKit';
  2. import { ringtone } from '@kit.RingtoneKit';
  3. import { BusinessError } from '@kit.BasicServicesKit';
  4. import { JSON } from '@kit.ArkTS';
  5. import { hilog } from '@kit.PerformanceAnalysisKit';
  6. const APP_TAG = 'Msc_Demo'
  7. const DOMAIN = 0x0001
  8. @Entry
  9. @Component
  10. struct Index {
  11. private context = this.getUIContext().getHostContext() as common.UIAbilityContext;
  12. build() {
  13. Stack() {
  14. Column() {
  15. Button('设为铃声OGG格式')
  16. .width(200)
  17. .height(50)
  18. .onClick(() => {
  19. let audioPath: string = this.context.filesDir + '/test.ogg'
  20. let splitList = audioPath.split('/')
  21. let fileName = splitList[splitList.length - 1]
  22. hilog.info(DOMAIN, APP_TAG, `audioPath: ${audioPath}`)
  23. hilog.info(DOMAIN, APP_TAG, `fileName: ${fileName}`)
  24. try {
  25. ringtone.startRingtoneSetting(this.context, audioPath, fileName, (err, res) => {
  26. hilog.info(DOMAIN, APP_TAG, `返回值:${JSON.stringify(res)}`)
  27. })
  28. } catch (error) {
  29. let err: BusinessError = error as BusinessError;
  30. hilog.error(DOMAIN, APP_TAG,
  31. `accessSync failed with error message: ${err.message}, error code: ${err.code}`);
  32. }
  33. })
  34. }
  35. .width('100%')
  36. .height('100%')
  37. .backgroundColor(Color.Pink)
  38. }
  39. .height('100%')
  40. .width('100%')
  41. }
  42. }

ringtone.startRingtoneSetting

Phone5.0.0(12)+Tablet5.0.0(12)+

startRingtoneSetting(context: common.UIAbilityContext, path: string, name: string): Promise<RingtoneType>

拉起设置铃声弹窗,并返回点击的铃声类型,使用Promise异步回调。

模型约束: 此接口仅可在Stage模型下使用。

系统能力:SystemCapability.Ringtone.Core

起始版本: 5.0.0(12)

参数:

展开
参数名 类型 必填 说明
context common.UIAbilityContext UIAbility上下文。
path string 具有访问权限的文件路径。
name string 文件名,限制长度1000字符。

返回值:

展开
类型 说明
Promise<RingtoneType> Promise对象。返回用户选择设置的铃声类型。

错误码:

以下错误码的详细介绍请参见通用错误码ArkTS API错误码

展开
错误码ID 错误信息
401 Parameter error. Possible causes: context is invalid.
1011600001 User canceled.
1011600002 The media file is not found.
1011600003 Failed to show the dialog box.
1011600004 Failed to call the system API.
1011699999 System exception.

示例:

收起
自动换行
深色代码主题
复制
  1. import { common } from '@kit.AbilityKit';
  2. import { ringtone } from '@kit.RingtoneKit';
  3. import { BusinessError } from '@kit.BasicServicesKit';
  4. import { JSON } from '@kit.ArkTS';
  5. import { hilog } from '@kit.PerformanceAnalysisKit';
  6. const APP_TAG = 'Msc_Demo'
  7. const DOMAIN = 0x0001
  8. @Entry
  9. @Component
  10. struct Index {
  11. private context = this.getUIContext().getHostContext() as common.UIAbilityContext;
  12. build() {
  13. Stack() {
  14. Column() {
  15. Button('设为铃声OGG格式')
  16. .width(200)
  17. .height(50)
  18. .onClick(() => {
  19. let audioPath: string = this.context.filesDir + '/test.ogg'
  20. let splitList = audioPath.split('/')
  21. let fileName = splitList[splitList.length - 1]
  22. hilog.info(DOMAIN, APP_TAG, `audioPath: ${audioPath}`)
  23. hilog.info(DOMAIN, APP_TAG, `fileName: ${fileName}`)
  24. try {
  25. ringtone.startRingtoneSetting(this.context, audioPath, fileName).then(res => {
  26. hilog.info(DOMAIN, APP_TAG, `返回值:${JSON.stringify(res)}`)
  27. })
  28. } catch (error) {
  29. let err: BusinessError = error as BusinessError;
  30. hilog.error(DOMAIN, APP_TAG,
  31. `accessSync failed with error message: ${err.message}, error code: ${err.code}`);
  32. }
  33. })
  34. }
  35. .width('100%')
  36. .height('100%')
  37. .backgroundColor(Color.Pink)
  38. }
  39. .height('100%')
  40. .width('100%')
  41. }
  42. }
在 API参考 中进行搜索
请输入您想要搜索的关键词