设置铃声
====

1.  导入ringtone模块和相关公共模块。
    
    import { common } from '@kit.AbilityKit';
    import { ringtone } from '@kit.RingtoneKit'
    import { uniformTypeDescriptor } from '@kit.ArkData';
    import { JSON } from '@kit.ArkTS';
    import { hilog } from '@kit.PerformanceAnalysisKit';
    const APP\_TAG = "Msc\_Demo"
    const DOMAIN = 0x0001
    
2.  调用[ringtone.getSupportedRingtoneTypes](https://developer.huawei.com/consumer/cn/doc/harmonyos-references/ringtone-ringtone#ringtonegetsupportedringtonetypes)接口，查询支持设置的铃声类型。
    
    let ringtoneTypeList: Array<ringtone.RingtoneType> = ringtone.getSupportedRingtoneTypes();
    hilog.info(DOMAIN, APP\_TAG,'getSupportedRingtoneTypes : ' + JSON.stringify(ringtoneTypeList));
    
3.  调用[ringtone.getSupportedDataTypes](https://developer.huawei.com/consumer/cn/doc/harmonyos-references/ringtone-ringtone#ringtonegetsupporteddatatypes)接口，查询支持的数据类型。当前支持格式：MP3，OGG，FLAC，AAC，MP2，M4A。
    
    // 其中 ringtone.RingtoneType.NOTIFICATION 为通知铃声
    let dataTypeList: Array<uniformTypeDescriptor.UniformDataType> = ringtone.getSupportedDataTypes(ringtone.RingtoneType.NOTIFICATION);
    hilog.info(DOMAIN, APP\_TAG,'getSupportedDataTypes: ' + JSON.stringify(dataTypeList));
    
4.  调用[ringtone.startRingtoneSetting](https://developer.huawei.com/consumer/cn/doc/harmonyos-references/ringtone-ringtone#ringtonestartringtonesetting)接口拉起设置弹窗，用户设置铃声后返回设置的铃声类型。
    
    通过promise异步方式：
    
    // 详细代码参考API参考
    let prefixUri: string = '';
    let audioPath: string = prefixUri + '/' + this.buttonText;
    let fileName: string = audioPath.substring(audioPath.lastIndexOf('/') + 1, audioPath.lastIndexOf('.'));
    await ringtone.startRingtoneSetting(this.context, audioPath, fileName).then(res => {
      hilog.info(DOMAIN, APP\_TAG,'setFlag :' + res);
    });
    
    通过callback异步方式：
    
    // 详细代码参考API参考
    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,'setFlag :' + data);
    });
    
5.  调用[ringtone.getSupportedMaxDuration](https://developer.huawei.com/consumer/cn/doc/harmonyos-references/ringtone-ringtone#ringtonegetsupportedmaxduration)接口，获取当前铃声支持的最大时长。
    
    // 其中 ringtone.RingtoneType.MESSAGE 为短信铃声
    let maxDuration: number =
      ringtone.getSupportedMaxDuration(ringtone.RingtoneType.MESSAGE, uniformTypeDescriptor.UniformDataType.MP3)
    hilog.info(DOMAIN, APP\_TAG,'getSupportedMaxDuration: ' + maxDuration);