// Generated at 2018-7-31 declare namespace swan { namespace request { type Param = { /** * 开发者服务器接口地址 */ url: string /** * 请求的参数 */ data?: any | string | ArrayBuffer /** * 设置请求的 header,header 中不能设置 Referer。 */ header?: any /** * (需大写)有效值:OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT * * @default GET */ method?: string /** * 如果设为json,会尝试对返回的数据做一次 JSON.parse * * @default json */ dataType?: string /** * 设置响应的数据类型。合法值:text、arraybuffer * * @default text */ responseType?: string /** * 收到开发者服务成功返回的回调函数 */ success?: ParamPropSuccess /** * 接口调用失败的回调函数 */ fail?: ParamPropFail /** * 接口调用结束的回调函数(调用成功、失败都会执行) */ complete?: ParamPropComplete } /** * 收到开发者服务成功返回的回调函数 */ type ParamPropSuccess = (res: ParamPropSuccessParam) => any type ParamPropSuccessParam = { /** * 开发者服务器返回的数据 * * **data 数据说明:** * * 最终发送给服务器的数据是 String 类型,如果传入的 data 不是 String 类型,会被转换成 String 。转换规则如下: * * * 对于 `GET` 方法的数据,会将数据转换成 query string(encodeURIComponent(k)=encodeURIComponent(v)&encodeURIComponent(k)=encodeURIComponent(v)...) * * 对于 `POST` 方法且 `header['content-type']` 为 `application/json` 的数据,会对数据进行 JSON 序列化 * * 对于 `POST` 方法且 `header['content-type']` 为 `application/x-www-form-urlencoded` 的数据,会将数据转换成 query string (encodeURIComponent(k)=encodeURIComponent(v)&encodeURIComponent(k)=encodeURIComponent(v)...) */ data: any | string | ArrayBuffer /** * 开发者服务器返回的 HTTP 状态码 */ statusCode: number /** * 开发者服务器返回的 HTTP Response Header * */ header: any } /** * 接口调用失败的回调函数 */ type ParamPropFail = (err: any) => any /** * 接口调用结束的回调函数(调用成功、失败都会执行) */ type ParamPropComplete = () => any type Return = { /** * 中断请求任务 * */ abort: ReturnPropAbort } /** * 中断请求任务 */ type ReturnPropAbort = () => any } /** * 发起网络请求。**使用前请先阅读[说明](https://smartapp.baidu.com/docs/develop/api/net/)**。 * * **返回值:** * * * 返回一个 `requestTask` 对象,通过 `requestTask`,可中断请求任务。 * * **Bug & Tip:** * * 1. `tip`: content-type 默认为 'application/json'; * 2. `bug`: 开发者工具 `0.10.102800` 版本,`header` 的 `content-type` 设置异常; * * **示例代码:** * * ```javascript * swan.request({ * url: 'test.php', //仅为示例,并非真实的接口地址 * data: { * x: '' , * y: '' * }, * header: { * 'content-type': 'application/json' // 默认值 * }, * success: function(res) { * console.log(res.data) * } * }) * ``` * * **示例代码:** * * ```javascript * const requestTask = swan.request({ * url: 'test.php', //仅为示例,并非真实的接口地址 * data: { * x: '' , * y: '' * }, * header: { * 'content-type': 'application/json' * }, * success: function(res) { * console.log(res.data) * } * }) * * requestTask.abort() // 取消请求任务 * ``` * */ function request(OBJECT: request.Param): request.Return namespace uploadFile { type Param = { /** * 开发者服务器 url */ url: string /** * 要上传文件资源的路径 */ filePath: string /** * 文件对应的 key , 开发者在服务器端通过这个 key 可以获取到文件二进制内容 */ name: string /** * HTTP 请求 Header, header 中不能设置 Referer */ header?: any /** * HTTP 请求中其他额外的 form data */ formData?: any /** * 接口调用成功的回调函数 */ success?: ParamPropSuccess /** * 接口调用失败的回调函数 */ fail?: ParamPropFail /** * 接口调用结束的回调函数(调用成功、失败都会执行) */ complete?: ParamPropComplete } /** * 接口调用成功的回调函数 */ type ParamPropSuccess = (res: ParamPropSuccessParam) => any type ParamPropSuccessParam = { /** * 开发者服务器返回的数据 */ data: string /** * 开发者服务器返回的 HTTP 状态码 */ statusCode: number } /** * 接口调用失败的回调函数 */ type ParamPropFail = (err: any) => any /** * 接口调用结束的回调函数(调用成功、失败都会执行) */ type ParamPropComplete = () => any type Return = { /** * 监听上传进度变化 * */ onProgressUpdate: ReturnPropOnProgressUpdate /** * 中断上传任务 * */ abort: ReturnPropAbort } /** * 监听上传进度变化 */ type ReturnPropOnProgressUpdate = (callback: ReturnPropOnProgressUpdateParam) => any type ReturnPropOnProgressUpdateParam = (res: ReturnPropOnProgressUpdateParamParam) => any type ReturnPropOnProgressUpdateParamParam = { /** * 上传进度百分比 */ progress: number /** * 已经上传的数据长度,单位 Bytes */ totalBytesSent: number /** * 预期需要上传的数据总长度,单位 Bytes */ totalBytesExpectedToSend: number } /** * 中断上传任务 */ type ReturnPropAbort = () => any } /** * 将本地资源上传到开发者服务器,客户端发起一个 HTTPS POST 请求,其中 `content-type` 为 `multipart/form-data` 。 * * 如页面通过 [swan.chooseImage](https://smartapp.baidu.com/docs/develop/api/media/#chooseImage) 等接口获取到一个本地资源的临时文件路径后,可通过此接口将本地资源上传到指定服务器。 * * **返回值:** * * * 返回一个 `uploadTask` 对象,通过 `uploadTask`,可监听上传进度变化事件,以及取消上传任务。 * * **示例代码:** * * ```javascript * swan.chooseImage({ * success: function(res) { * var tempFilePaths = res.tempFilePaths * swan.uploadFile({ * url: 'https://smartprogram.baidu.com/xxx', // 开发者服务器 url * filePath: tempFilePaths[0], * name: 'file', * formData:{ * 'user': 'test' * }, * success: function(res){ * var data = res.data * //do something * } * }) * } * }) * ``` * * **示例代码:** * * ```javascript * const uploadTask = swan.uploadFile({ * url: 'https://smartprogram.baidu.com/xxx', // 开发者服务器 url * filePath: tempFilePaths[0], * name: 'file', * formData:{ * 'user': 'test' * }, * success: function(res){ * var data = res.data * //do something * } * }) * * uploadTask.onProgressUpdate((res) => { * console.log('上传进度', res.progress) * console.log('已经上传的数据长度', res.totalBytesSent) * console.log('预期需要上传的数据总长度', res.totalBytesExpectedToSend) * }) * * uploadTask.abort() // 取消上传任务 * ``` * */ function uploadFile(OBJECT: uploadFile.Param): uploadFile.Return namespace downloadFile { type Param = { /** * 下载资源的 url */ url: string /** * HTTP 请求 Header,header 中不能设置 Referer */ header?: any /** * 下载成功后以 tempFilePath 的形式传给页面,res = {tempFilePath: '文件的临时路径'} */ success?: ParamPropSuccess /** * 接口调用失败的回调函数 */ fail?: ParamPropFail /** * 接口调用结束的回调函数(调用成功、失败都会执行) */ complete?: ParamPropComplete } /** * 下载成功后以 tempFilePath 的形式传给页面,res = {tempFilePath: '文件的临时路径'} */ type ParamPropSuccess = (res: ParamPropSuccessParam) => any type ParamPropSuccessParam = { /** * 临时文件路径,下载后的文件会存储到一个临时文件 */ tempFilePath: string /** * 开发者服务器返回的 HTTP 状态码 */ statusCode: number } /** * 接口调用失败的回调函数 */ type ParamPropFail = (err: any) => any /** * 接口调用结束的回调函数(调用成功、失败都会执行) */ type ParamPropComplete = () => any type Return = { /** * 监听下载进度变化 * */ onProgressUpdate: ReturnPropOnProgressUpdate /** * 中断下载任务 * */ abort: ReturnPropAbort } /** * 监听下载进度变化 */ type ReturnPropOnProgressUpdate = (callback: ReturnPropOnProgressUpdateParam) => any type ReturnPropOnProgressUpdateParam = (res: ReturnPropOnProgressUpdateParamParam) => any type ReturnPropOnProgressUpdateParamParam = { /** * 下载进度百分比 */ progress: number /** * 已经下载的数据长度,单位 Bytes */ totalBytesWritten: number /** * 预期需要下载的数据总长度,单位 Bytes */ totalBytesExpectedToWrite: number } /** * 中断下载任务 */ type ReturnPropAbort = () => any } /** * 下载文件资源到本地,客户端直接发起一个 HTTP GET 请求,返回文件的本地临时路径。 * * **返回值:** * * * 返回一个 `downloadTask` 对象,通过 `downloadTask`,可监听下载进度变化事件,以及取消下载任务。 * * **Tips:** * * 1. 文件的临时路径,在智能小程序本次启动期间可以正常使用,如需持久保存,需再主动调用 swan.saveFile,才能在智能小程序下次启动时访问得到。 * 2. 请在 header 中指定合理的 Content-Type 字段,以保证客户端正确处理文件类型 * 3. uploadFile 上传文件大小限制为 25M。 * * **示例代码:** * * ```javascript * swan.downloadFile({ * url: 'https://smartprogram.baidu.com/xxx', //仅为示例,并非真实的资源 * success: function(res) { * // 只要服务器有响应数据,就会把响应内容写入文件并进入 success 回调,业务需要自行判断是否下载到了想要的内容 * if (res.statusCode === 200) { * swan.playVoice({ * filePath: res.tempFilePath * }) * } * } * }) * ``` * * **示例代码:** * * ```javascript * const downloadTask = swan.downloadFile({ * url: 'https://smartprogram.baidu.com/xxx', //仅为示例,并非真实的资源 * success: function(res) { * swan.playVoice({ * filePath: res.tempFilePath * }) * } * }) * * downloadTask.onProgressUpdate((res) => { * console.log('下载进度', res.progress) * console.log('已经下载的数据长度', res.totalBytesWritten) * console.log('预期需要下载的数据总长度', res.totalBytesExpectedToWrite) * }) * * downloadTask.abort() // 取消下载任务 * ``` * */ function downloadFile(OBJECT: downloadFile.Param): downloadFile.Return namespace connectSocket { type Param = { /** * 开发者服务器接口地址,必须是 wss 协议,且域名必须是后台配置的合法域名 */ url: string /** * HTTP Header , header 中不能设置 Referer */ header?: any /** * 默认是GET,有效值:OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT */ method?: string /** * 子协议数组 * */ protocols?: string[] /** * 接口调用成功的回调函数 */ success?: ParamPropSuccess /** * 接口调用失败的回调函数 */ fail?: ParamPropFail /** * 接口调用结束的回调函数(调用成功、失败都会执行) */ complete?: ParamPropComplete } /** * 接口调用成功的回调函数 */ type ParamPropSuccess = (res: any) => any /** * 接口调用失败的回调函数 */ type ParamPropFail = (err: any) => any /** * 接口调用结束的回调函数(调用成功、失败都会执行) */ type ParamPropComplete = () => any } /** * 创建一个 [WebSocket](https://developer.mozilla.org/zh-CN/docs/Web/API/WebSocket) 连接。 * * * **示例代码:** * * ```javascript * swan.connectSocket({ * url: 'wss://example.baidu.com', * data:{ * x: '', * y: '' * }, * header:{ * 'content-type': 'application/json' * }, * protocols: ['protocol1'], * method:"GET" * }) * ``` * */ function connectSocket(OBJECT: connectSocket.Param): SocketTask namespace onSocketOpen { type Param = (res: ParamParam) => any type ParamParam = { /** * 连接成功的 HTTP 响应 Header * */ header?: any } } /** * 监听WebSocket连接打开事件。 * * **示例代码:** * * ```javascript * swan.connectSocket({ * url: 'test.php' * }) * swan.onSocketOpen(function(res) { * console.log('WebSocket连接已打开!') * }) * ``` * */ function onSocketOpen(callback?: onSocketOpen.Param): void /** * 监听WebSocket错误。 * * **示例代码:** * * ```javascript * swan.connectSocket({ * url: 'test.php' * }) * swan.onSocketOpen(function(res){ * console.log('WebSocket连接已打开!') * }) * swan.onSocketError(function(res){ * console.log('WebSocket连接打开失败,请检查!') * }) * ``` * */ function onSocketError(CALLBACK: any): void namespace sendSocketMessage { type Param = { /** * 需要发送的内容 */ data: string | ArrayBuffer /** * 接口调用成功的回调函数 */ success?: ParamPropSuccess /** * 接口调用失败的回调函数 */ fail?: ParamPropFail /** * 接口调用结束的回调函数(调用成功、失败都会执行) */ complete?: ParamPropComplete } /** * 接口调用成功的回调函数 */ type ParamPropSuccess = (res: any) => any /** * 接口调用失败的回调函数 */ type ParamPropFail = (err: any) => any /** * 接口调用结束的回调函数(调用成功、失败都会执行) */ type ParamPropComplete = () => any } /** * 通过 WebSocket 连接发送数据,需要先 [swan.connectSocket](https://smartapp.baidu.com/docs/develop/api/net/#connectSocket),并在 [swan.onSocketOpen](https://smartapp.baidu.com/docs/develop/api/net/#onSocketOpen) 回调之后才能发送。 * * **示例代码:** * * ```javascript * var socketOpen = false * var socketMsgQueue = [] * swan.connectSocket({ * url: 'test.php' * }) * * swan.onSocketOpen(function(res) { * socketOpen = true * for (var i = 0; i < socketMsgQueue.length; i++){ * sendSocketMessage(socketMsgQueue[i]) * } * socketMsgQueue = [] * }) * * function sendSocketMessage(msg) { * if (socketOpen) { * swan.sendSocketMessage({ * data:msg * }) * } else { * socketMsgQueue.push(msg) * } * } * ``` * */ function sendSocketMessage(OBJECT: sendSocketMessage.Param): void namespace onSocketMessage { type Param = (res: ParamParam) => any type ParamParam = { /** * 服务器返回的消息 */ data: string | ArrayBuffer } } /** * 监听WebSocket接受到服务器的消息事件。 * * **示例代码:** * * ```javascript * swan.connectSocket({ * url: 'test.php' * }) * * swan.onSocketMessage(function(res) { * console.log('收到服务器内容:' + res.data) * }) * ``` * */ function onSocketMessage(CALLBACK: onSocketMessage.Param): void namespace closeSocket { type Param = { /** * 一个数字值表示关闭连接的状态号,表示连接被关闭的原因。如果这个参数没有被指定,默认的取值是1000 (表示正常连接关闭) * */ code?: number /** * 一个可读的字符串,表示连接被关闭的原因。这个字符串必须是不长于123字节的UTF-8 文本(不是字符) * */ reason?: string /** * 接口调用成功的回调函数 */ success?: ParamPropSuccess /** * 接口调用失败的回调函数 */ fail?: ParamPropFail /** * 接口调用结束的回调函数(调用成功、失败都会执行) */ complete?: ParamPropComplete } /** * 接口调用成功的回调函数 */ type ParamPropSuccess = (res: any) => any /** * 接口调用失败的回调函数 */ type ParamPropFail = (err: any) => any /** * 接口调用结束的回调函数(调用成功、失败都会执行) */ type ParamPropComplete = () => any } /** * 关闭 WebSocket 连接。 * */ function closeSocket(OBJECT: closeSocket.Param): void /** * 监听WebSocket关闭。 * * **返回值:** * * * 返回一个SocketTask * * * * **示例:** * * ```javascript * swan.connectSocket({ * url: 'test.php' * }) * * //注意这里有时序问题, * //如果 swan.connectSocket 还没回调 swan.onSocketOpen,而先调用 swan.closeSocket,那么就做不到关闭 WebSocket 的目的。 * //必须在 WebSocket 打开期间调用 swan.closeSocket 才能关闭。 * swan.onSocketOpen(function() { * swan.closeSocket() * }) * * swan.onSocketClose(function(res) { * console.log('WebSocket 已关闭!') * }) * ``` * */ function onSocketClose(CALLBACK: any): void namespace SocketTask { namespace send { type Param = { /** * 需要发送的内容 */ data: string | ArrayBuffer /** * 接口调用成功的回调函数 */ success?: ParamPropSuccess /** * 接口调用失败的回调函数 */ fail?: ParamPropFail /** * 接口调用结束的回调函数(调用成功、失败都会执行) */ complete?: ParamPropComplete } /** * 接口调用成功的回调函数 */ type ParamPropSuccess = (res: any) => any /** * 接口调用失败的回调函数 */ type ParamPropFail = (err: any) => any /** * 接口调用结束的回调函数(调用成功、失败都会执行) */ type ParamPropComplete = () => any } namespace close { type Param = { /** * 一个数字值表示关闭连接的状态号,表示连接被关闭的原因。如果这个参数没有被指定,默认的取值是1000 (表示正常连接关闭) */ code?: number /** * 一个可读的字符串,表示连接被关闭的原因。这个字符串必须是不长于123字节的UTF-8 文本(不是字符) */ reason?: string /** * 接口调用成功的回调函数 */ success?: ParamPropSuccess /** * 接口调用失败的回调函数 */ fail?: ParamPropFail /** * 接口调用结束的回调函数(调用成功、失败都会执行) */ complete?: ParamPropComplete } /** * 接口调用成功的回调函数 */ type ParamPropSuccess = (res: any) => any /** * 接口调用失败的回调函数 */ type ParamPropFail = (err: any) => any /** * 接口调用结束的回调函数(调用成功、失败都会执行) */ type ParamPropComplete = () => any } namespace onOpen { type Param = (res: ParamParam) => any type ParamParam = { /** * 连接成功的 HTTP 响应 Header * */ header: any } } namespace onError { type Param = (res: ParamParam) => any type ParamParam = { /** * 错误信息 */ errMsg: string } } namespace onMessage { type Param = (res: ParamParam) => any type ParamParam = { /** * 服务器返回的消息 */ data: string | ArrayBuffer } } } /** * * WebSocket 任务,可通过 swan.connectSocket()接口创建返回。 */ class SocketTask { /** * * **SocketTask.send(OBJECT):** * * 通过 WebSocket 连接发送数据。 */ send(OBJECT: SocketTask.send.Param): void /** * * **SocketTask.close(OBJECT):** * * 关闭 WebSocket 连接。 */ close(OBJECT: SocketTask.close.Param): void /** * * **SocketTask.onOpen(CALLBACK):** * * 监听 WebSocket 连接打开事件。 */ onOpen(CALLBACK: SocketTask.onOpen.Param): void /** * * **SocketTask.onClose(CALLBACK):** * * 监听 WebSocket 连接关闭事件。 */ onClose(CALLBACK: any): void /** * * **SocketTask.onError(CALLBACK):** * * 监听 WebSocket 错误。 */ onError(CALLBACK: SocketTask.onError.Param): void /** * * **SocketTask.onMessage(CALLBACK):** * * 监听WebSocket接受到服务器的消息事件。 */ onMessage(CALLBACK: SocketTask.onMessage.Param): void } namespace chooseImage { type Param = { /** * 最多可以选择的图片张数,默认9 */ count?: number /** * original 原图,compressed 压缩图,默认二者都有 */ sizeType?: string[] /** * album 从相册选图,camera 使用相机,默认二者都有 */ sourceType?: string[] /** * 成功则返回图片的本地文件路径列表 tempFilePaths */ success: ParamPropSuccess /** * 接口调用失败的回调函数 */ fail?: ParamPropFail /** * 接口调用结束的回调函数(调用成功、失败都会执行) */ complete?: ParamPropComplete } /** * 成功则返回图片的本地文件路径列表 tempFilePaths */ type ParamPropSuccess = (res: ParamPropSuccessParam) => any type ParamPropSuccessParam = { /** * 图片的本地文件路径列表 */ tempFilePaths: string[] /** * 图片的本地文件列表,每一项是一个 File 对象 * */ tempFiles: ParamPropSuccessParamPropTempFiles } /** * 图片的本地文件列表,每一项是一个 File 对象 */ type ParamPropSuccessParamPropTempFiles = ParamPropSuccessParamPropTempFilesItem[] type ParamPropSuccessParamPropTempFilesItem = { /** * 本地文件路径 */ path: string /** * 本地文件大小,单位:B */ size: number } /** * 接口调用失败的回调函数 */ type ParamPropFail = (err: any) => any /** * 接口调用结束的回调函数(调用成功、失败都会执行) */ type ParamPropComplete = () => any } /** * 从本地相册选择图片或使用相机拍照。 * * **示例代码:** * * ```javascript * swan.chooseImage({ * count: 1, // 默认9 * sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图,默认二者都有 * sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机,默认二者都有 * success: function (res) { * // 返回选定照片的本地文件路径列表,tempFilePath可以作为img标签的src属性显示图片 * var tempFilePaths = res.tempFilePaths * } * }) * ``` * */ function chooseImage(OBJECT: chooseImage.Param): void namespace previewImage { type Param = { /** * 当前显示图片的链接,不填则默认为 urls 的第一张 */ current?: string /** * 需要预览的图片链接列表 */ urls: string[] /** * 接口调用成功的回调函数 */ success?: ParamPropSuccess /** * 接口调用失败的回调函数 */ fail?: ParamPropFail /** * 接口调用结束的回调函数(调用成功、失败都会执行) */ complete?: ParamPropComplete } /** * 接口调用成功的回调函数 */ type ParamPropSuccess = (res: any) => any /** * 接口调用失败的回调函数 */ type ParamPropFail = (err: any) => any /** * 接口调用结束的回调函数(调用成功、失败都会执行) */ type ParamPropComplete = () => any } /** * 预览图片。 * * **示例代码:** * * ```javascript * swan.previewImage({ * current: '', // 当前显示图片的http链接 * urls: [] // 需要预览的图片http链接列表 * }) * ``` * */ function previewImage(OBJECT: previewImage.Param): void namespace getImageInfo { type Param = { /** * 图片的路径,可以是相对路径,临时文件路径,存储文件路径,网络图片路径 */ src: string /** * 接口调用成功的回调函数 */ success?: ParamPropSuccess /** * 接口调用失败的回调函数 */ fail?: ParamPropFail /** * 接口调用结束的回调函数(调用成功、失败都会执行) */ complete?: ParamPropComplete } /** * 接口调用成功的回调函数 */ type ParamPropSuccess = (res: ParamPropSuccessParam) => any type ParamPropSuccessParam = { /** * 图片宽度,单位px */ width: number /** * 图片高度,单位px */ height: number /** * 返回图片的本地路径 */ path: string /** * 返回图片的方向,有效值见下表 * * **orientation参数说明:** * * 枚举值 | 说明 * -------------------|----------------- * up | 默认 * down | 180度旋转 * left | 逆时针旋转90度 * right | 顺时针旋转90度 * up-mirrored | 同up,但水平翻转 * down-mirrored |同down,但水平翻转 * left-mirrored |同left,但垂直翻转 * right-mirrored |同right,但垂直翻转 * * */ orientation: string /** * 返回图片的格式 * * */ type: string } /** * 接口调用失败的回调函数 */ type ParamPropFail = (err: any) => any /** * 接口调用结束的回调函数(调用成功、失败都会执行) */ type ParamPropComplete = () => any } /** * 获取图片信息 * * **示例代码:** * * ```javascript * swan.getImageInfo({ * src: 'images/a.jpg', * success: function (res) { * console.log(res.width) * console.log(res.height) * } * }) * * swan.chooseImage({ * success: function (res) { * swan.getImageInfo({ * src: res.tempFilePaths[0], * success: function (res) { * console.log(res.width) * console.log(res.height) * } * }) * } * }) * ``` * */ function getImageInfo(OBJECT: getImageInfo.Param): void namespace saveImageToPhotosAlbum { type Param = { /** * 图片文件路径,可以是临时文件路径也可以是永久文件路径,不支持网络图片路径 */ filePath: string /** * 接口调用成功的回调函数 */ success?: ParamPropSuccess /** * 接口调用失败的回调函数 */ fail?: ParamPropFail /** * 接口调用结束的回调函数(调用成功、失败都会执行) */ complete?: ParamPropComplete } /** * 接口调用成功的回调函数 */ type ParamPropSuccess = (res: ParamPropSuccessParam) => any type ParamPropSuccessParam = { /** * 调用结果 */ errMsg: string } /** * 接口调用失败的回调函数 */ type ParamPropFail = (err: any) => any /** * 接口调用结束的回调函数(调用成功、失败都会执行) */ type ParamPropComplete = () => any } /** * * * 保存图片到系统相册。需要用户授权scope.writePhotosAlbum * * **示例代码:** * * ```javascript * swan.saveImageToPhotosAlbum({ * success(res) { * } * }) * ``` * */ function saveImageToPhotosAlbum(OBJECT: saveImageToPhotosAlbum.Param): void namespace startRecord { type Param = { /** * 录音成功后调用,返回录音文件的临时文件路径,res = {tempFilePath: '录音文件的临时路径'} */ success?: ParamPropSuccess /** * 接口调用失败的回调函数 */ fail?: ParamPropFail /** * 接口调用结束的回调函数(调用成功、失败都会执行) */ complete?: ParamPropComplete } /** * 录音成功后调用,返回录音文件的临时文件路径,res = {tempFilePath: '录音文件的临时路径'} */ type ParamPropSuccess = (res: ParamPropSuccessParam) => any type ParamPropSuccessParam = { /** * 录音文件的临时路径 */ tempFilePath: any } /** * 接口调用失败的回调函数 */ type ParamPropFail = (err: any) => any /** * 接口调用结束的回调函数(调用成功、失败都会执行) */ type ParamPropComplete = () => any } /** * **注意:1.6.0 版本开始,本接口不再维护。建议使用能力更强的swan.getRecorderManager接口** * * 开始录音。当主动调用`swan.stopRecord`,或者录音超过1分钟时自动结束录音,返回录音文件的临时文件路径。当用户离开小程序时,此接口无法调用。 * * 需要用户授权scope.record * */ function startRecord(OBJECT: startRecord.Param): void /** * ​主动调用停止录音。 * * **示例代码:** * * ```javascript * swan.startRecord({ * success: function(res) { * var tempFilePath = res.tempFilePath * }, * fail: function(res) { * //录音失败 * } * }) * setTimeout(function() { * //结束录音 * swan.stopRecord() * }, 10000) * ``` * */ function stopRecord(): void /** * * * 获取**全局唯一**的录音管理器 `recorderManager`。 * * **其中,采样率和码率有一定要求,具体有效值如下::** * * 采样率 | 编码码率 * ----------|------------------- * 8000 | 16000 ~ 48000 * 11025 | 16000 ~ 48000 * 12000 | 24000 ~ 64000 * 16000 | 24000 ~ 96000 * 22050 | 32000 ~ 128000 * 24000 | 32000 ~ 128000 * 32000 | 48000 ~ 192000 * 44100 | 64000 ~ 320000 * 48000 | 64000 ~ 320000 * * audioSource 有效值: * * **示例代码:** * * ```javascript * const recorderManager = swan.getRecorderManager() * * recorderManager.onStart(() => { * console.log('recorder start') * }) * recorderManager.onPause(() => { * console.log('recorder pause') * }) * recorderManager.onStop((res) => { * console.log('recorder stop', res) * const { tempFilePath } = res * }) * recorderManager.onFrameRecorded((res) => { * const { frameBuffer } = res * console.log('frameBuffer.byteLength', frameBuffer.byteLength) * }) * * const options = { * duration: 10000, * sampleRate: 44100, * numberOfChannels: 1, * encodeBitRate: 192000, * format: 'aac', * frameSize: 50 * } * * recorderManager.start(options) * ``` * */ function getRecorderManager(): RecorderManager namespace RecorderManager { namespace start { type Param = { /** * 指定录音的时长,单位 ms ,如果传入了合法的 duration ,在到达指定的 duration 后会自动停止录音,最大值 600000(10 分钟),默认值 60000(1 分钟) * * */ duration?: number /** * 采样率,有效值 8000/16000/44100 * * */ sampleRate?: number /** * 录音通道数,有效值 1/2 * * */ numberOfChannels?: number /** * 编码码率,有效值见下表格 * * */ encodeBitRate?: number /** * 音频格式,有效值 aac/mp3 * * */ format?: string /** * 指定帧大小,单位 KB。传入 frameSize 后,每录制指定帧大小的内容后,会回调录制的文件内容,不指定则不会回调。暂仅支持 mp3 格式。 * * */ frameSize?: number /** * 指定音频输入源,默认值为 'auto' * * */ audioSource?: string } } namespace onStop { type Param = (res: ParamParam) => any type ParamParam = { /** * 录音文件的临时路径 */ tempFilePath: string } } namespace onFrameRecorded { type Param = (res: ParamParam) => any type ParamParam = { /** * 录音分片结果数据 */ frameBuffer: ArrayBuffer /** * 当前帧是否正常录音结束前的最后一帧 */ isLastFrame: boolean } } namespace onError { type Param = (res: ParamParam) => any type ParamParam = { /** * 错误信息 */ errMsg: string } } } class RecorderManager { /** * 开始录音 */ start(options: RecorderManager.start.Param): any /** * 暂停录音 */ pause(): any /** * 继续录音 */ resume(): any /** * 停止录音 */ stop(): any /** * 录音开始事件 */ onStart(callback: any): any /** * 录音暂停事件 */ onPause(callback: any): any /** * 录音停止事件,会回调文件地址 */ onStop(callback: RecorderManager.onStop.Param): any /** * 已录制完指定帧大小的文件,会回调录音分片结果数据。如果设置了 frameSize ,则会回调此事件 */ onFrameRecorded(callback: RecorderManager.onFrameRecorded.Param): any /** * 录音错误事件, 会回调错误信息 */ onError(callback: RecorderManager.onError.Param): any } namespace playVoice { type Param = { /** * 需要播放的语音文件的文件路径 */ filePath: string /** * 指定录音时长,到达指定的录音时长后会自动停止录音,单位:秒,默认值:60 * * */ duration?: number /** * 接口调用成功的回调函数 */ success?: ParamPropSuccess /** * 接口调用失败的回调函数 */ fail?: ParamPropFail /** * 接口调用结束的回调函数(调用成功、失败都会执行) */ complete?: ParamPropComplete } /** * 接口调用成功的回调函数 */ type ParamPropSuccess = (res: any) => any /** * 接口调用失败的回调函数 */ type ParamPropFail = (err: any) => any /** * 接口调用结束的回调函数(调用成功、失败都会执行) */ type ParamPropComplete = () => any } /** * **注意:1.6.0 版本开始,本接口不再维护。建议使用能力更强的 [swan.createInnerAudioContext]接口** * * 开始播放语音,同时只允许一个语音文件正在播放,如果前一个语音文件还没播放完,将中断前一个语音播放。 * * **示例代码:** * * ```javascript * swan.startRecord({ * success: function(res) { * var tempFilePath = res.tempFilePath * swan.playVoice({ * filePath: tempFilePath, * complete: function(){ * } * }) * } * }) * ``` * */ function playVoice(OBJECT: playVoice.Param): void /** * 暂停正在播放的语音。再次调用swan.playVoice播放同一个文件时,会从暂停处开始播放。如果想从头开始播放,需要先调用 swan.stopVoice。 * * **示例代码:** * * ```javascript * swan.startRecord({ * success: function(res) { * var tempFilePath = res.tempFilePath * swan.playVoice({ * filePath: tempFilePath * }) * * setTimeout(function() { * //暂停播放 * swan.pauseVoice() * }, 5000) * } * }) * ``` * */ function pauseVoice(): void /** * 结束播放语音。 * * **示例代码:** * * ```javascript * swan.startRecord({ * success: function(res) { * var tempFilePath = res.tempFilePath * swan.playVoice({ * filePath:tempFilePath * }) * * setTimeout(function(){ * swan.stopVoice() * }, 5000) * } * }) * ``` * */ function stopVoice(): void namespace getBackgroundAudioPlayerState { type Param = { /** * 接口调用成功的回调函数 */ success?: ParamPropSuccess /** * 接口调用失败的回调函数 */ fail?: ParamPropFail /** * 接口调用结束的回调函数(调用成功、失败都会执行) */ complete?: ParamPropComplete } /** * 接口调用成功的回调函数 */ type ParamPropSuccess = (res: ParamPropSuccessParam) => any type ParamPropSuccessParam = { /** * 选定音频的长度(单位:s),只有在当前有音乐播放时返回 */ duration: any /** * 选定音频的播放位置(单位:s),只有在当前有音乐播放时返回 */ currentPosition: any /** * 播放状态(2:没有音乐在播放,1:播放中,0:暂停中) */ status: any /** * 音频的下载进度(整数,80 代表 80%),只有在当前有音乐播放时返回 */ downloadPercent: any /** * 歌曲数据链接,只有在当前有音乐播放时返回 */ dataUrl: any } /** * 接口调用失败的回调函数 */ type ParamPropFail = (err: any) => any /** * 接口调用结束的回调函数(调用成功、失败都会执行) */ type ParamPropComplete = () => any } /** * **注意:1.2.0 版本开始,本接口不再维护。建议使用能力更强的 swan.getBackgroundAudioManager接口** * * 获取后台音乐播放状态。 * * **示例代码:** * * ```javascript * swan.getBackgroundAudioPlayerState({ * success: function(res) { * var status = res.status * var dataUrl = res.dataUrl * var currentPosition = res.currentPosition * var duration = res.duration * var downloadPercent = res.downloadPercent * } * }) * ``` * */ function getBackgroundAudioPlayerState(OBJECT: getBackgroundAudioPlayerState.Param): void namespace playBackgroundAudio { type Param = { /** * 音乐链接,目前支持的格式有 m4a, aac, mp3, wav */ dataUrl: string /** * 音乐标题 */ title?: string /** * 封面URL */ coverImgUrl?: string /** * 接口调用成功的回调函数 */ success?: ParamPropSuccess /** * 接口调用失败的回调函数 */ fail?: ParamPropFail /** * 接口调用结束的回调函数(调用成功、失败都会执行) */ complete?: ParamPropComplete } /** * 接口调用成功的回调函数 */ type ParamPropSuccess = (res: any) => any /** * 接口调用失败的回调函数 */ type ParamPropFail = (err: any) => any /** * 接口调用结束的回调函数(调用成功、失败都会执行) */ type ParamPropComplete = () => any } /** * 使用后台播放器播放音乐,对于客户端来说,只能同时有一个后台音乐在播放。当用户离开小程序后,音乐将暂停播放;当用户点击“显示在聊天顶部”时,音乐不会暂停播放;当用户在其他小程序占用了音乐播放器,原有小程序内的音乐将停止播放。 * * **OBJECT参数说明:** * * ```javascript * swan.playBackgroundAudio({ * dataUrl: '', * title: '', * coverImgUrl: '' * }) * ``` * */ function playBackgroundAudio(OBJECT: playBackgroundAudio.Param): void /** * 暂停播放音乐。 * * **示例代码** * * **示例:** * * ```javascript * swan.pauseBackgroundAudio() * ``` * */ function pauseBackgroundAudio(): void namespace seekBackgroundAudio { type Param = { /** * 音乐位置,单位:秒 */ position: number /** * 接口调用成功的回调函数 */ success?: ParamPropSuccess /** * 接口调用失败的回调函数 */ fail?: ParamPropFail /** * 接口调用结束的回调函数(调用成功、失败都会执行) */ complete?: ParamPropComplete } /** * 接口调用成功的回调函数 */ type ParamPropSuccess = (res: any) => any /** * 接口调用失败的回调函数 */ type ParamPropFail = (err: any) => any /** * 接口调用结束的回调函数(调用成功、失败都会执行) */ type ParamPropComplete = () => any } /** * 控制音乐播放进度。 * * **OBJECT参数说明:** * * ```javascript * swan.seekBackgroundAudio({ * position: 30 * }) * ``` * */ function seekBackgroundAudio(OBJECT: seekBackgroundAudio.Param): void /** * 停止播放音乐。 * * **示例代码** * * **示例:** * * ```javascript * swan.stopBackgroundAudio() * ``` * */ function stopBackgroundAudio(): void /** * 监听音乐播放。 * */ function onBackgroundAudioPlay(CALLBACK: any): void /** * 监听音乐暂停。 * */ function onBackgroundAudioPause(CALLBACK: any): void /** * 监听音乐停止。 * * **bug & tip:** * * 1. `bug`: `iOS` `6.3.30` swan.seekBackgroundAudio 会有短暂延迟 * */ function onBackgroundAudioStop(CALLBACK: any): void /** * * * 获取**全局唯一**的背景音频管理器 `backgroundAudioManager`。 * * **errcode 说明:** * * errCode | 说明 * ------------|--------- * 10001 | 系统错误 * 10002 | 网络错误 * 10003 | 文件错误 * 10004 | 格式错误 * -1 | 未知错误 * * **示例代码:** * * ```javascript * const backgroundAudioManager = swan.getBackgroundAudioManager() * * backgroundAudioManager.title = '此时此刻' * backgroundAudioManager.epname = '此时此刻' * backgroundAudioManager.singer = '许巍' * backgroundAudioManager.coverImgUrl = 'http://y.gtimg.cn/music/photo_new/T002R300x300M000003rsKF44GyaSk.jpg?max_age=2592000' * backgroundAudioManager.src = 'http://ws.stream.qqmusic.qq.com/M500001VfvsJ21xFqb.mp3?guid=ffffffff82def4af4b12b3cd9337d5e7&uin=346897220&vkey=6292F51E1E384E061FF02C31F716658E5C81F5594D561F2E88B854E81CAAB7806D5E4F103E55D33C16F3FAC506D1AB172DE8600B37E43FAD&fromtag=46' // 设置了 src 之后会自动播放 * ``` * */ function getBackgroundAudioManager(): BackgroundAudioManager class BackgroundAudioManager { /** * 当前音频的长度(单位:s),只有在当前有合法的 src 时返回 * * @readonly */ duration: number /** * 当前音频的播放位置(单位:s),只有在当前有合法的 src 时返回 * * @readonly */ currentTime: number /** * 当前是是否暂停或停止状态,true 表示暂停或停止,false 表示正在播放 * * @readonly */ paused: boolean /** * 音频的数据源,默认为空字符串,**当设置了新的 src 时,会自动开始播放** ,目前支持的格式有 m4a, aac, mp3, wav */ src: string /** * 音频开始播放的位置(单位:s) */ startTime: number /** * 音频缓冲的时间点,仅保证当前播放时间点到此时间点内容已缓冲。 * * @readonly */ buffered: number /** * 音频标题,用于做原生音频播放器音频标题。原生音频播放器中的分享功能,分享出去的卡片标题,也将使用该值。 */ title: string /** * 专辑名,原生音频播放器中的分享功能,分享出去的卡片简介,也将使用该值。 */ epname: string /** * 歌手名,原生音频播放器中的分享功能,分享出去的卡片简介,也将使用该值。 */ singer: string /** * 封面图url,用于做原生音频播放器背景图。原生音频播放器中的分享功能,分享出去的卡片配图及背景也将使用该图。 */ coverImgUrl: string /** * 页面链接,原生音频播放器中的分享功能,分享出去的卡片简介,也将使用该值。 */ webUrl: string /** * 音频协议。默认值为 'http',设置 'hls' 可以支持播放 HLS 协议的直播音频 * * */ protocol: string /** * 播放 */ play(): any /** * 暂停 */ pause(): any /** * 停止 */ stop(): any /** * 跳转到指定位置,单位 s。精确到小数点后 3 位,即支持 ms 级别精确度 */ seek(position: any): any /** * 背景音频进入可以播放状态,但不保证后面可以流畅播放 */ onCanplay(callback: any): any /** * 背景音频播放事件 */ onPlay(callback: any): any /** * 背景音频暂停事件 */ onPause(callback: any): any /** * 背景音频停止事件 */ onStop(callback: any): any /** * 背景音频自然播放结束事件 */ onEnded(callback: any): any /** * 背景音频播放进度更新事件 */ onTimeUpdate(callback: any): any /** * 用户在系统音乐播放面板点击上一曲事件(iOS only) */ onPrev(callback: any): any /** * 用户在系统音乐播放面板点击下一曲事件(iOS only) */ onNext(callback: any): any /** * 背景音频播放错误事件 */ onError(callback: any): any /** * 音频加载中事件,当音频因为数据不足,需要停下来加载时会触发 */ onWaiting(callback: any): any } /** * **注意:1.6.0 版本开始,本接口不再维护。建议使用能力更强的 [swan.createInnerAudioContext]() 接口** * * 创建并返回 audio 上下文 `audioContext` 对象。在自定义组件下,第二个参数传入组件实例this,以操作组件内 `