{"id":18850610,"url":"https://github.com/baidu-aip/speech-tts-cors","last_synced_at":"2025-09-03T07:42:24.431Z","repository":{"id":39953164,"uuid":"129352627","full_name":"Baidu-AIP/speech-tts-cors","owner":"Baidu-AIP","description":"百度语音 语音合成 跨域demo以及支持库","archived":false,"fork":false,"pushed_at":"2019-08-21T06:42:54.000Z","size":10,"stargazers_count":109,"open_issues_count":5,"forks_count":68,"subscribers_count":9,"default_branch":"master","last_synced_at":"2025-07-08T02:20:02.165Z","etag":null,"topics":["cors","cors-request","javascript","tts"],"latest_commit_sha":null,"homepage":null,"language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/Baidu-AIP.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2018-04-13T05:33:39.000Z","updated_at":"2025-06-23T08:22:38.000Z","dependencies_parsed_at":"2022-09-13T19:42:42.955Z","dependency_job_id":null,"html_url":"https://github.com/Baidu-AIP/speech-tts-cors","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/Baidu-AIP/speech-tts-cors","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Baidu-AIP%2Fspeech-tts-cors","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Baidu-AIP%2Fspeech-tts-cors/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Baidu-AIP%2Fspeech-tts-cors/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Baidu-AIP%2Fspeech-tts-cors/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Baidu-AIP","download_url":"https://codeload.github.com/Baidu-AIP/speech-tts-cors/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Baidu-AIP%2Fspeech-tts-cors/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":273410261,"owners_count":25100451,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","status":"online","status_checked_at":"2025-09-03T02:00:09.631Z","response_time":76,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["cors","cors-request","javascript","tts"],"created_at":"2024-11-08T03:30:06.693Z","updated_at":"2025-09-03T07:42:24.403Z","avatar_url":"https://github.com/Baidu-AIP.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# 百度语音合成浏览器跨域访问示例\n\n1. 本项目包含一个调用库baidu\\_tts\\_cors.js文件，以及一个demo.html示例文件，demo.html示例文件已经引入了调用库; 并且项目不依赖其他第三方库。\n2. 项目的跨域依赖于XHR2，[CORS跨域控制](https://developer.mozilla.org/zh-CN/docs/Web/HTTP/Access_control_CORS)\n3. 使用前请确保了解百度AI开发平台授权，百度AI语音合成RESTful接口相关概念。[百度语音合成RESTful接口文档](http://ai.baidu.com/docs#/TTS-API/top) [鉴权机制文档](http://ai.baidu.com/docs#/Auth/top)\n4. 项目为初期版本，后续版本实现和接口调用方式可能会有较大变动。\n\n## 兼容性说明\n\n1. 浏览器对于自动播放特性的支持情况有差异，因此没有添加自动播放的功能，也不建议自行添加自动播放属性。\n2. demo已经在主流浏览器下测试通过，非主流浏览器可能存在不支持的情况，包括微信小程序，各种hybrid框架等。\n\n## 如何获取tok参数\n\n首先在您创建的应用中查找Api Key 和 SecretKey。访问https://openapi.baidu.com/oauth/2.0/token 换取 token\n\n\n**访问如下网址， 可用浏览器测试**\n\n```\n// appKey = Va5yQRHl********LT0vuXV4\n// appSecret = 0rDSjzQ20XUj5i********PQSzr5pVw2\n\nhttps://openapi.baidu.com/oauth/2.0/token?grant_type=client_credentials\u0026client_id=Va5yQRHl********LT0vuXV4\u0026client_secret=0rDSjzQ20XUj5i********PQSzr5pVw2\n```\n\n**可以获取如下结果**\n\n```json\n{\n\"access_token\": \"1.a6b7dbd428f731035f771b8d********.86400.1292922000-2346678-124328\",\n\"expires_in\": 86400,\n\"refresh_token\": \"2.385d55f8615fdfd9edb7c4b********.604800.1293440400-2346678-124328\",\n\"scope\": \"public\",\n\"session_key\": \"ANXxSNjwQDugf8615Onqeik********CdlLxn\",\n\"session_secret\": \"248APxvxjCZ0VEC********aK4oZExMB\",\n}\n```\n\n在结果中可以看见 token = 1.a6b7dbd428f731035f771b8d********.86400.1292922000-2346678-124328，在86400秒后过期。\n\n## 调用示例\n\n您的项目只需引入baidu\\_tts\\_cors.js文件，即可使用全局函数btts。\n\n```\n// 调用语音合成接口\n// 参数含义请参考 https://ai.baidu.com/docs#/TTS-API/41ac79a6\nbtts({\n    tex: '百度语音合成',\n    tok: '请参照上一章节说明获取的access_token',\n    spd: 5,\n    pit: 5,\n    vol: 15,\n    per: 4\n}, {\n    volume: 0.3,\n    autoDestory: true,\n    timeout: 10000,\n    hidden: false,\n    onInit: function (htmlAudioElement) {\n\n    },\n    onSuccess: function(htmlAudioElement) {\n\n    },\n    onError: function(errorText) {\n    },\n    onTimeout: function () {\n    }\n});\n}\n```\n\nbtts全局函数参数说明\n\n    浏览器调用语音合成接口\n    @param {Object} param 百度语音合成接口参数\n            请参考 https://ai.baidu.com/docs#/TTS-API/41ac79a6\n\n    @param {Object} options 跨域调用api参数\n            timeout {number} 超时时间 默认不设置为60秒\n            volume {number} audio控件音量，范围 0-1\n            hidden {boolean} 是否隐藏audio控件\n            autoDestory {boolean} 播放音频完毕后是否自动删除控件\n            onInit {Function} 创建完audio控件后调用\n            onSuccess {Function} 远程语音合成完成，并且返回音频文件后调用\n            onError {Function}  远程语音合成完成，并且返回错误字符串后调用\n            onTimeout {Function} 超时后调用，默认超时时间为60秒\n\n\n*** 具体调用方法和参考示例  demo.html  文件 ***\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbaidu-aip%2Fspeech-tts-cors","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fbaidu-aip%2Fspeech-tts-cors","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbaidu-aip%2Fspeech-tts-cors/lists"}