Import the ringtone module and related common modules.
import { common } from '@kit.AbilityKit';
import { ringtone } from '@kit.RingtoneKit';
import { BusinessError } from '@kit.BasicServicesKit';
import { fileUri, picker } from '@kit.CoreFileKit'; Call the ringtone.getSupportedRingtoneTypes API to query the supported ringtone types.
import { ringtone } from '@kit.RingtoneKit'
import { JSON } from '@kit.ArkTS';
import { hilog } from '@kit.PerformanceAnalysisKit';
const APP_TAG = 'Msc_Demo'
const DOMAIN = 0x0001
@Entry
@Component
struct GetSupportedRingtoneTypes {
build() {
Stack() {
Column() {
Button('Query Supported Custom Ringtone Types')
.width(200)
.height(50)
.onClick(() => {
let typeList: ringtone.RingtoneType[] = ringtone.getSupportedRingtoneTypes()
hilog.info(DOMAIN, APP_TAG, `getSupportedRingtoneTypes: ${JSON.stringify(typeList)}`);
})
}
.width('100%')
.height('100%')
.backgroundColor(Color.Pink)
}
.height('100%')
.width('100%')
}
} Call the ringtone.getSupportedDataTypes API to query the supported data types. Currently supported formats are MP3, OGG, FLAC, AAC, MP2, M4A, and MP4.
import { ringtone } from '@kit.RingtoneKit'
import { BusinessError } from '@kit.BasicServicesKit';
import { uniformTypeDescriptor } from '@kit.ArkData';
import { JSON } from '@kit.ArkTS';
import { hilog } from '@kit.PerformanceAnalysisKit';
const APP_TAG = 'Msc_Demo'
const DOMAIN = 0x0001
@Entry
@Component
struct GetSupportedDataTypes {
build() {
Stack() {
Column() {
Button('Query Supported File Types')
.width(200)
.height(50)
.onClick(() => {
try {
let typeList: uniformTypeDescriptor.UniformDataType[] =
ringtone.getSupportedDataTypes(ringtone.RingtoneType.NOTIFICATION)
hilog.info(DOMAIN, APP_TAG, `getSupportedDataType: ${JSON.stringify(typeList)}`);
} catch (error) {
let err: BusinessError = error as BusinessError;
hilog.error(DOMAIN, APP_TAG,
`getSupportedDataType error message: ${err.message}, error code: ${err.code}`);
}
})
}
.width('100%')
.height('100%')
.backgroundColor(Color.Pink)
}
.height('100%')
.width('100%')
}
} Call the ringtone.startRingtoneSetting API to display the setting dialog box. After the user sets the ringtone, the set ringtone type is returned.
The result is returned asynchronously through a promise:
import { common } from '@kit.AbilityKit';
import { ringtone } from '@kit.RingtoneKit';
import { BusinessError } from '@kit.BasicServicesKit';
import { fileUri, picker } from '@kit.CoreFileKit';
@Entry
@Component
struct Index {
@State isShowUIExtensionCom: boolean = false;
private prefixUri: string = '';
private buttonText: string = '';
private context = this.getUIContext().getHostContext() as common.UIAbilityContext;
aboutToAppear(): void {
const documentViewPicker = new picker.DocumentViewPicker(this.context);
const documentSaveOptions = new picker.DocumentSaveOptions();
documentSaveOptions.pickerMode = picker.DocumentPickerMode.DOWNLOAD;
documentViewPicker.save(documentSaveOptions).then((documentSaveResult: Array<string>) => {
let savePath = documentSaveResult[0];
let fileUriObject = new fileUri.FileUri(savePath);
this.prefixUri = fileUriObject.path
console.info('documentViewPicker.save succeed and prefixUri is:' + this.prefixUri);
}).catch((err: BusinessError) => {
console.error(`Invoke documentViewPicker.save failed, code is ${err.code}, message is ${err.message}`);
})
}
build() {
Column() {
Column() {
Text($r('app.string.setting_ringtone'))
.fontSize(30)
.fontWeight(FontWeight.Bold)
.fontColor($r('sys.color.ohos_id_color_text_primary'))
.alignSelf(ItemAlign.Start)
.margin({
top: 64,
left: 12,
bottom: 16
})
TextInput({ placeholder: $r('app.string.please_enter_the_file_name') })
.width(312)
.height(40)
.onChange((value: string) => {
this.buttonText = value;
})
}
Button($r('app.string.setting_ringtone'))
.width(312)
.height(40)
.margin({
bottom: 16
})
.onClick(async () => {
if (this.buttonText) {
let audioPath: string = this.prefixUri + '/' + this.buttonText;
console.info(`audioPath:${audioPath}`);
try {
let fileName: string = audioPath.substring(audioPath.lastIndexOf('/') + 1, audioPath.lastIndexOf('.'));
console.info(`fileName:${fileName}`);
ringtone.startRingtoneSetting(this.context, audioPath, fileName).then(res => {
console.info(`setFlag : ${res}`);
});
} catch (error) {
let err: BusinessError = error as BusinessError;
if (err.code === ringtone.RingtoneErrors.ERROR_FILE_NOT_FOUND) {
this.getUIContext().getPromptAction().showToast({
message: $r('app.string.file_exist'),
duration: 2000
});
}
console.error(`accessSync failed with error. message: ${err.message}, code: ${err.code}`);
}
} else {
this.getUIContext().getPromptAction().showToast({
message: $r('app.string.please_enter_the_file_name'),
duration: 2000
});
}
})
}
.justifyContent(FlexAlign.SpaceBetween)
.width('100%')
.height('100%')
}
} The result is returned asynchronously through a callback:
// For details about the code, see the API reference.
let prefixUri: string = '';
let audioPath: string = prefixUri + '/' + this.buttonText;
let fileName: string = audioPath.substring(audioPath.lastIndexOf('/') + 1, audioPath.lastIndexOf('.'));
ringtone.startRingtoneSetting(this.context, audioPath, fileName, (err, data) => {
hilog.info(DOMAIN, APP_TAG, `Return value: ${JSON.stringify(data)}`)
}); Call the ringtone.getSupportedMaxDuration API to obtain the maximum duration supported by the current ringtone.
import { ringtone } from '@kit.RingtoneKit'
import { BusinessError } from '@kit.BasicServicesKit';
import { uniformTypeDescriptor } from '@kit.ArkData';
import { hilog } from '@kit.PerformanceAnalysisKit';
const APP_TAG = 'Msc_Demo'
const DOMAIN = 0x0001
@Entry
@Component
struct GetSupportedMaxDuration {
build() {
Stack() {
Column() {
Button('Query Maximum Duration')
.width(200)
.height(50)
.onClick(() => {
try {
let maxDuration: number =
ringtone.getSupportedMaxDuration(ringtone.RingtoneType.MESSAGE,
uniformTypeDescriptor.UniformDataType.MP3)
hilog.info(DOMAIN, APP_TAG, `getSupportedMaxDuration: ${maxDuration}`);
} catch (error) {
let err: BusinessError = error as BusinessError;
hilog.error(DOMAIN, APP_TAG,
`getSupportedMaxDuration error message: ${err.message}, error code: ${err.code}`);
}
})
}
.width('100%')
.height('100%')
.backgroundColor(Color.Pink)
}
.height('100%')
.width('100%')
}
} Call the ringtone.getSupportedMaxSize API to obtain the maximum file size supported by the current ringtone.
import { ringtone } from '@kit.RingtoneKit'
import { BusinessError } from '@kit.BasicServicesKit';
import { uniformTypeDescriptor } from '@kit.ArkData';
import { hilog } from '@kit.PerformanceAnalysisKit';
const APP_TAG = 'Msc_Demo'
const DOMAIN = 0x0001
@Entry
@Component
struct GetSupportedMaxSize {
build() {
Stack() {
Column() {
Button('Query File Size Limit')
.width(200)
.height(50)
.onClick(() => {
try {
let maxSize: number =
ringtone.getSupportedMaxSize(ringtone.RingtoneType.CALL,
uniformTypeDescriptor.UniformDataType.MP3)
hilog.info(DOMAIN, APP_TAG, `getSupportedMaxSize: ${maxSize}`);
} catch (error) {
let err: BusinessError = error as BusinessError;
hilog.error(DOMAIN, APP_TAG,
`getSupportedMaxSize error message: ${err.message}, error code: ${err.code}`);
}
})
}
.width('100%')
.height('100%')
.backgroundColor(Color.Pink)
}
.height('100%')
.width('100%')
}
}