Skip to content

Commit 4f280bb

Browse files
kennywgxbinarywang
authored andcommitted
#1287 公众号模块新增下载微信jssdk上传的高清语音素材的接口
* 新增下载微信jssdk上传的高清语音素材的接口,格式为.speex * 添加高清语音接口测试代码
1 parent 0c18330 commit 4f280bb

File tree

4 files changed

+51
-0
lines changed

4 files changed

+51
-0
lines changed

weixin-java-mp/src/main/java/me/chanjar/weixin/mp/api/WxMpMaterialService.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,22 @@ public interface WxMpMaterialService {
8888
*/
8989
File mediaDownload(String mediaId) throws WxErrorException;
9090

91+
/**
92+
* <pre>
93+
* 获取高清语音素材
94+
* 公众号可以使用本接口获取从JSSDK的uploadVoice接口上传的临时语音素材,格式为speex,16K采样率。
95+
* 该音频比上文的临时素材获取接口(格式为amr,8K采样率)更加清晰,适合用作语音识别等对音质要求较高的业务。
96+
* 详情请见: <a href="https://developers.weixin.qq.com/doc/offiaccount/Asset_Management/Get_temporary_materials.html">
97+
* 获取高清语音素材</a>
98+
* 接口url格式:https://api.weixin.qq.com/cgi-bin/media/get/jssdk?access_token=ACCESS_TOKEN&media_id=MEDIA_ID
99+
* </pre>
100+
*
101+
* @param mediaId 媒体文件Id
102+
* @return 保存到本地的临时文件
103+
* @throws WxErrorException
104+
*/
105+
File jssdkMediaDownload(String mediaId) throws WxErrorException;
106+
91107
/**
92108
* <pre>
93109
* 上传图文消息内的图片获取URL

weixin-java-mp/src/main/java/me/chanjar/weixin/mp/api/impl/WxMpMaterialServiceImpl.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,14 @@ public File mediaDownload(String mediaId) throws WxErrorException {
6565
"media_id=" + mediaId);
6666
}
6767

68+
@Override
69+
public File jssdkMediaDownload(String mediaId) throws WxErrorException {
70+
return this.wxMpService.execute(
71+
BaseMediaDownloadRequestExecutor.create(this.wxMpService.getRequestHttp(), this.wxMpService.getWxMpConfigStorage().getTmpDirFile()),
72+
JSSDK_MEDIA_GET_URL,
73+
"media_id=" + mediaId);
74+
}
75+
6876
@Override
6977
public WxMediaImgUploadResult mediaImgUpload(File file) throws WxErrorException {
7078
return this.wxMpService.execute(MediaImgUploadRequestExecutor.create(this.wxMpService.getRequestHttp()), IMG_UPLOAD_URL, file);

weixin-java-mp/src/main/java/me/chanjar/weixin/mp/enums/WxMpApiUrl.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -819,6 +819,10 @@ enum Material implements WxMpApiUrl {
819819
* get.
820820
*/
821821
MEDIA_GET_URL(API_DEFAULT_HOST_URL, "/cgi-bin/media/get"),
822+
/**
823+
* jssdk media get.
824+
*/
825+
JSSDK_MEDIA_GET_URL(API_DEFAULT_HOST_URL, "/cgi-bin/media/get/jssdk"),
822826
/**
823827
* upload.
824828
*/

weixin-java-mp/src/test/java/me/chanjar/weixin/mp/api/impl/WxMpMaterialServiceImplTest.java

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,8 @@ public class WxMpMaterialServiceImplTest {
4444
private WxMpMaterialCountResult wxMaterialCountResultBeforeTest;
4545
// 以下为media接口的测试
4646
private List<String> mediaIdsToDownload = new ArrayList<>();
47+
// 以下为高清语音接口的测试
48+
private List<String> voiceMediaIdsToDownload = new ArrayList<>();
4749

4850
@DataProvider
4951
public Object[][] mediaFiles() {
@@ -289,6 +291,11 @@ public void testUploadMedia(String mediaType, String fileType, String fileName)
289291
if (res.getMediaId() != null && !mediaType.equals(WxConsts.MediaFileType.VIDEO)) {
290292
//video 不支持下载,所以不加入
291293
this.mediaIdsToDownload.add(res.getMediaId());
294+
295+
// 音频media, 用于测试下载高清语音接口
296+
if (mediaType.equals(WxConsts.MediaFileType.VOICE)) {
297+
this.voiceMediaIdsToDownload.add(res.getMediaId());
298+
}
292299
}
293300

294301
if (res.getThumbMediaId() != null) {
@@ -308,10 +315,26 @@ public Object[][] downloadMedia() {
308315
return params;
309316
}
310317

318+
@DataProvider
319+
public Object[][] downloadJssdkMedia() {
320+
Object[][] params = new Object[this.voiceMediaIdsToDownload.size()][];
321+
for (int i = 0; i < this.voiceMediaIdsToDownload.size(); i++) {
322+
params[i] = new Object[]{this.voiceMediaIdsToDownload.get(i)};
323+
}
324+
return params;
325+
}
326+
311327
@Test(dependsOnMethods = {"testUploadMedia"}, dataProvider = "downloadMedia")
312328
public void testDownloadMedia(String mediaId) throws WxErrorException {
313329
File file = this.wxService.getMaterialService().mediaDownload(mediaId);
314330
assertNotNull(file);
315331
System.out.println(file.getAbsolutePath());
316332
}
333+
334+
@Test(dependsOnMethods = {"testUploadMedia"}, dataProvider = "downloadJssdkMedia")
335+
public void testDownloadJssdkMedia(String mediaId) throws WxErrorException {
336+
File file = this.wxService.getMaterialService().jssdkMediaDownload(mediaId);
337+
assertNotNull(file);
338+
System.out.println(file.getAbsolutePath());
339+
}
317340
}

0 commit comments

Comments
 (0)