8000 #708 企业微信增加获取高清语音素材接口 · linlinjava/WxJava@3c391c5 · GitHub
[go: up one dir, main page]

Skip to content

Commit 3c391c5

Browse files
committed
binarywang#708 企业微信增加获取高清语音素材接口
1 parent d59dff2 commit 3c391c5

File tree

3 files changed

+40
-7
lines changed

3 files changed

+40
-7
lines changed

weixin-java-cp/src/main/java/me/chanjar/weixin/cp/api/WxCpMediaService.java

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ public interface WxCpMediaService {
1919
String MEDIA_GET_URL = "https://qyapi.weixin.qq.com/cgi-bin/media/get";
2020
String MEDIA_UPLOAD_URL = "https://qyapi.weixin.qq.com/cgi-bin/media/upload?type=";
2121
String IMG_UPLOAD_URL = "https://qyapi.weixin.qq.com/cgi-bin/media/uploadimg";
22+
String JSSDK_MEDIA_GET_URL = "https://qyapi.weixin.qq.com/cgi-bin/media/get/jssdk";
2223

2324
/**
2425
* <pre>
@@ -59,6 +60,21 @@ WxMediaUploadResult upload(String mediaType, String fileType, InputStream inputS
5960
*/
6061
File download(String mediaId) throws WxErrorException;
6162

63+
/**
64+
* <pre>
65+
* 获取高清语音素材.
66+
* 可以使用本接口获取从JSSDK的uploadVoice接口上传的临时语音素材,格式为speex,16K采样率。该音频比上文的临时素材获取接口(格式为amr,8K采样率)更加清晰,适合用作语音识别等对音质要求较高的业务。
67+
* 请求方式:GET(HTTPS)
68+
* 请求地址:https://qyapi.weixin.qq.com/cgi-bin/media/get/jssdk?access_token=ACCESS_TOKEN&media_id=MEDIA_ID
69+
* 仅企业微信2.4及以上版本支持。
70+
* 文档地址:https://work.weixin.qq.com/api/doc#90000/90135/90255
71+
* </pre>
72+
*
73+
* @param mediaId 媒体id
74+
* @return 保存到本地的临时文件
75+
*/
76+
File getJssdkFile(String mediaId) throws WxErrorException;
77+
6278
/**
6379
* <pre>
6480
* 上传图片.
@@ -69,7 +85,7 @@ WxMediaUploadResult upload(String mediaType, String fileType, InputStream inputS
6985
* </pre>
7086
*
7187
* @param file 上传的文件对象
72-
* @return 返回图片url
88+
* @return 返回图片url
7389
*/
7490
String uploadImg(File file) throws WxErrorException;
7591
}

weixin-java-cp/src/main/java/me/chanjar/weixin/cp/api/impl/WxCpMediaServiceImpl.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,14 @@ public File download(String mediaId) throws WxErrorException {
4848
MEDIA_GET_URL, "media_id=" + mediaId);
4949
}
5050

51+
@Override
52+
public File getJssdkFile(String mediaId) throws WxErrorException {
53+
return this.mainService.execute(
54+
BaseMediaDownloadRequestExecutor.create(this.mainService.getRequestHttp(),
55+
this.mainService.getWxCpConfigStorage().getTmpDirFile()),
56+
JSSDK_MEDIA_GET_URL, "media_id=" + mediaId);
57+
}
58+
5159
@Override
5260
public String uploadImg(File file) throws WxErrorException {
5361
final WxMediaUploadResult result = this.mainService

weixin-java-cp/src/test/java/me/chanjar/weixin/cp/api/impl/WxCpMediaServiceImplTest.java

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,8 @@ public class WxCpMediaServiceImplTest {
3636
public Object[][] mediaData() {
3737
return new Object[][]{
3838
new Object[]{WxConsts.MediaFileType.IMAGE, TestConstants.FILE_JPG, "mm.jpeg"},
39-
new Object[]{WxConsts.MediaFileType.VOICE, TestConstants.FILE_MP3, "mm.mp3"},//{"errcode":301017,"errmsg":"voice file only support amr like myvoice.amr"}
39+
//new Object[]{WxConsts.MediaFileType.VOICE, TestConstants.FILE_MP3, "mm.mp3"},
40+
// {"errcode":301017,"errmsg":"voice file only support amr like myvoice.amr"}
4041
new Object[]{WxConsts.MediaFileType.VOICE, TestConstants.FILE_AMR, "mm.amr"},
4142
new Object[]{WxConsts.MediaFileType.VIDEO, TestConstants.FILE_MP4, "mm.mp4"},
4243
new Object[]{WxConsts.MediaFileType.FILE, TestConstants.FILE_JPG, "mm.jpeg"}
@@ -47,8 +48,9 @@ public Object[][] mediaData() {
4748
public void testUploadMedia(String mediaType, String fileType, String fileName) throws WxErrorException, IOException {
4849
try (InputStream inputStream = ClassLoader.getSystemResourceAsStream(fileName)) {
4950
WxMediaUploadResult res = this.wxService.getMediaService().upload(mediaType, fileType, inputStream);
50-
assertNotNull(res.getType());
51-
assertNotNull(res.getCreatedAt());
51+
assertThat(res).isNotNull();
52+
assertThat(res.getType()).isNotEmpty();
53+
assertThat(res.getCreatedAt()).isGreaterThan(0);
5254
assertTrue(res.getMediaId() != null || res.getThumbMediaId() != null);
5355

5456
if (res.getMediaId() != null) {
@@ -70,9 +72,9 @@ public Object[][] downloadMedia() {
7072
}
7173

7274
@Test(dependsOnMethods = {"testUploadMedia"}, dataProvider = "downloadMedia")
73-
public void testDownloadMedia(String media_id) throws WxErrorException {
74-
File file = this.wxService.getMediaService().download(media_id);
75-
assertNotNull(file);
75+
public void testDownload(String mediaId) throws WxErrorException {
76+
File file = this.wxService.getMediaService().download(mediaId);
77+
assertThat(file).isNotNull();
7678
System.out.println(file);
7779
}
7880

@@ -82,4 +84,11 @@ public void testUploadImg() throws WxErrorException {
8284
String res = this.wxService.getMediaService().uploadImg(new File(url.getFile()));
8385
assertThat(res).isNotEmpty();
8486
}
87+
88+
@Test
89+
public void testGetJssdkFile() throws WxErrorException {
90+
File file = this.wxService.getMediaService().getJssdkFile("....");
91+
assertThat(file).isNotNull();
92+
System.out.println(file);
93+
}
8594
}

0 commit comments

Comments
 (0)
0