We use essential cookies for the website to function, as well as analytics cookies for analyzing and creating statistics of the website performance. To agree to the use of analytics cookies, click "Accept All". You can manage your preferences at any time by clicking "Cookie Settings" on the footer. More Information.

Only Essential Cookies
Accept All

ringtone (Ringtone Service)

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

The ringtone module provides the function of setting ringtones.

Since: 5.0.0(12)

Modules to Import

Collapse
Word wrap
Dark theme
Copy code
  1. import { ringtone } from '@kit.RingtoneKit';

RingtoneType

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

Enumeration of ringtone types.

Model restriction: This API can be used only in the stage model.

System capability: SystemCapability.Ringtone.Core

Since: 5.0.0(12)

Expand
Name Value Description
CALL 0 Ringtone for incoming calls.
MESSAGE 1 Ringtone for SMS messages.
NOTIFICATION 2 Ringtone for notifications.
ALARM 3 Ringtone for alarms.

RingtoneErrors

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

Enumeration of error codes of the APIs for setting ringtones and obtaining supported ringtone types and ringtone file types.

Model restriction: This API can be used only in the stage model.

System capability: SystemCapability.Ringtone.Core

Since: 5.0.0(12)

Expand
Name Value Description
ERROR_INVALID_PARAM 401 Invalid parameter.
ERROR_USER_CANCELED 1011600001 The operation is canceled by the user.
ERROR_FILE_NOT_FOUND 1011600002 The file does not exist.
ERROR_SHOW_FAILED 1011600003 The ringtone dialog box fails to be displayed.
ERROR_CALL_SYSTEM_API_FAILED 1011600004 Failed to call the system API.
ERROR_DATA_TYPE_NOT_MATCHED 1011600005 Unmatched file type. Since: 26.0.0
ERROR_SYSTEM 1011699999 Internal system error.

ringtone.getSupportedRingtoneTypes

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

getSupportedRingtoneTypes(): Array<RingtoneType>

Queries custom ringtone types supported by the system.

Model restriction: This API can be used only in the stage model.

System capability: SystemCapability.Ringtone.Core

Since: 5.0.0(12)

Returns:

Expand
Type Description
Array<RingtoneType> Custom ringtone types supported by the system.

Example:

Collapse
Word wrap
Dark theme
Copy code
  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('Query Supported Custom Ringtone Types')
  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>

Queries custom ringtone types supported by the system.

Model restriction: This API can be used only in the stage model.

System capability: SystemCapability.Ringtone.Core

Since: 26.0.0

Parameters:

Expand
Parameter Type Mandatory Description
mediaType uniformTypeDescriptor.UniformDataType Yes File type to be queried.

Returns:

Expand
Type Description
Array<RingtoneType> Custom ringtone types supported by the system.

Example:

Collapse
Word wrap
Dark theme
Copy code
  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('Query Supported Custom Ringtone Types')
  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>

Queries the file types supported by a ringtone type.

Model restriction: This API can be used only in the stage model.

System capability: SystemCapability.Ringtone.Core

Since: 5.0.0(12)

Parameters:

Expand
Parameter Type Mandatory Description
ringtoneType RingtoneType Yes Ringtone type to be queried.

Returns:

Expand
Type Description
Array<uniformTypeDescriptor.UniformDataType> File types supported by a ringtone type.

Error codes:

For details about the error codes, please refer to Universal Error Codes.

Expand
ID Error Message
401 Parameter error. Possible causes:ringtoneType is invalid.

Example:

Collapse
Word wrap
Dark theme
Copy code
  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('Query Supported File Types')
  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

Queries the maximum file duration supported by different ringtone types and file types.

Model restriction: This API can be used only in the stage model.

System capability: SystemCapability.Ringtone.Core

Since: 5.0.0(12)

Parameters:

Expand
Parameter Type Mandatory Description
ringtoneType RingtoneType Yes Ringtone type to be queried.
dataType uniformTypeDescriptor.UniformDataType Yes File type to be queried.

Returns:

Expand
Type Description
number Maximum duration (unit: second) supported by the ringtone and file types. The maximum duration for an alarm ringtone is 300s, for an SMS message ringtone and notification ringtone is 7s, and for an incoming call ringtone is 60s.

Error codes:

For details about the error codes, please refer to Universal Error Codes.

Expand
ID Error Message
401 Parameter error. Possible causes:data type does not match the ringtone type.

Example:

Collapse
Word wrap
Dark theme
Copy code
  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('Query Maximum Duration')
  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

Queries the maximum file size supported by different ringtone types and file types.

Model restriction: This API can be used only in the stage model.

System capability: SystemCapability.Ringtone.Core

Since: 26.0.0

Parameters:

Expand
Parameter Type Mandatory Description
ringtoneType RingtoneType Yes Ringtone type to be queried.
dataType uniformTypeDescriptor.UniformDataType Yes File type to be queried.

Returns:

Expand
Type Description
number Maximum file size (unit: KB) supported by the ringtone and file types. The maximum size of a video file is 200 MB. If the size of an audio file is not limited, -1 is returned.

Error codes:

For details about the error codes, please refer to Universal Error Codes and ArkTS API Error Codes.

Expand
ID Error Message
1011600005 The data type does not match the ringtone type.

Example:

Collapse
Word wrap
Dark theme
Copy code
  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('Query File Size Limit')
  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

Opens the ringtone setting pop-up window and returns the type of the tapped ringtone. This API returns the result asynchronously through a callback.

Model restriction: This API can be used only in the stage model.

System capability: SystemCapability.Ringtone.Core

Since: 5.0.0(12)

Parameters:

Expand
Parameter Type Mandatory Description
context common.UIAbilityContext Yes UIAbility context.
path string Yes Path of the file on which you have the access permission.
name string Yes File name. The value contains a maximum of 1,000 characters.
callback AsyncCallback<RingtoneType> Yes Callback used to return the ringtone type set by the user.

Error codes:

For details about the error codes, please refer to Universal Error Codes and ArkTS API Error Codes.

Expand
ID Error Message
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.

Example:

Collapse
Word wrap
Dark theme
Copy code
  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('Set Ringtone OGG Format')
  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, `Return value: ${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>

Opens the ringtone setting pop-up window and returns the type of the tapped ringtone. This API uses a promise to return the result.

Model restriction: This API can be used only in the stage model.

System capability: SystemCapability.Ringtone.Core

Since: 5.0.0(12)

Parameters:

Expand
Parameter Type Mandatory Description
context common.UIAbilityContext Yes UIAbility context.
path string Yes Path of the file on which you have the access permission.
name string Yes File name. The value contains a maximum of 1,000 characters.

Returns:

Expand
Type Description
Promise<RingtoneType> Promise used to return the ringtone type set by the user.

Error codes:

For details about the error codes, please refer to Universal Error Codes and ArkTS API Error Codes.

Expand
ID Error Message
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.

Example:

Collapse
Word wrap
Dark theme
Copy code
  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('Set Ringtone OGG Format')
  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, `Return value: ${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. }
Search in References
Enter a keyword.