Skip to content

Commit

Permalink
#708 企业微信增加获取高清语音素材接口
Browse files Browse the repository at this point in the history
  • Loading branch information
binarywang committed Dec 8, 2018
1 parent d59dff2 commit 3c391c5
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ public interface WxCpMediaService {
String MEDIA_GET_URL = "https://qyapi.weixin.qq.com/cgi-bin/media/get";
String MEDIA_UPLOAD_URL = "https://qyapi.weixin.qq.com/cgi-bin/media/upload?type=";
String IMG_UPLOAD_URL = "https://qyapi.weixin.qq.com/cgi-bin/media/uploadimg";
String JSSDK_MEDIA_GET_URL = "https://qyapi.weixin.qq.com/cgi-bin/media/get/jssdk";

/**
* <pre>
Expand Down Expand Up @@ -59,6 +60,21 @@ WxMediaUploadResult upload(String mediaType, String fileType, InputStream inputS
*/
File download(String mediaId) throws WxErrorException;

/**
* <pre>
* 获取高清语音素材.
* 可以使用本接口获取从JSSDK的uploadVoice接口上传的临时语音素材,格式为speex,16K采样率。该音频比上文的临时素材获取接口(格式为amr,8K采样率)更加清晰,适合用作语音识别等对音质要求较高的业务。
* 请求方式:GET(HTTPS)
* 请求地址:https://qyapi.weixin.qq.com/cgi-bin/media/get/jssdk?access_token=ACCESS_TOKEN&media_id=MEDIA_ID
* 仅企业微信2.4及以上版本支持。
* 文档地址:https://work.weixin.qq.com/api/doc#90000/90135/90255
* </pre>
*
* @param mediaId 媒体id
* @return 保存到本地的临时文件
*/
File getJssdkFile(String mediaId) throws WxErrorException;

/**
* <pre>
* 上传图片.
Expand All @@ -69,7 +85,7 @@ WxMediaUploadResult upload(String mediaType, String fileType, InputStream inputS
* </pre>
*
* @param file 上传的文件对象
* @return 返回图片url
* @return 返回图片url
*/
String uploadImg(File file) throws WxErrorException;
}
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,14 @@ public File download(String mediaId) throws WxErrorException {
MEDIA_GET_URL, "media_id=" + mediaId);
}

@Override
public File getJssdkFile(String mediaId) throws WxErrorException {
return this.mainService.execute(
BaseMediaDownloadRequestExecutor.create(this.mainService.getRequestHttp(),
this.mainService.getWxCpConfigStorage().getTmpDirFile()),
JSSDK_MEDIA_GET_URL, "media_id=" + mediaId);
}

@Override
public String uploadImg(File file) throws WxErrorException {
final WxMediaUploadResult result = this.mainService
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@ public class WxCpMediaServiceImplTest {
public Object[][] mediaData() {
return new Object[][]{
new Object[]{WxConsts.MediaFileType.IMAGE, TestConstants.FILE_JPG, "mm.jpeg"},
new Object[]{WxConsts.MediaFileType.VOICE, TestConstants.FILE_MP3, "mm.mp3"},//{"errcode":301017,"errmsg":"voice file only support amr like myvoice.amr"}
//new Object[]{WxConsts.MediaFileType.VOICE, TestConstants.FILE_MP3, "mm.mp3"},
// {"errcode":301017,"errmsg":"voice file only support amr like myvoice.amr"}
new Object[]{WxConsts.MediaFileType.VOICE, TestConstants.FILE_AMR, "mm.amr"},
new Object[]{WxConsts.MediaFileType.VIDEO, TestConstants.FILE_MP4, "mm.mp4"},
new Object[]{WxConsts.MediaFileType.FILE, TestConstants.FILE_JPG, "mm.jpeg"}
Expand All @@ -47,8 +48,9 @@ public Object[][] mediaData() {
public void testUploadMedia(String mediaType, String fileType, String fileName) throws WxErrorException, IOException {
try (InputStream inputStream = ClassLoader.getSystemResourceAsStream(fileName)) {
WxMediaUploadResult res = this.wxService.getMediaService().upload(mediaType, fileType, inputStream);
assertNotNull(res.getType());
assertNotNull(res.getCreatedAt());
assertThat(res).isNotNull();
assertThat(res.getType()).isNotEmpty();
assertThat(res.getCreatedAt()).isGreaterThan(0);
assertTrue(res.getMediaId() != null || res.getThumbMediaId() != null);

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

@Test(dependsOnMethods = {"testUploadMedia"}, dataProvider = "downloadMedia")
public void testDownloadMedia(String media_id) throws WxErrorException {
File file = this.wxService.getMediaService().download(media_id);
assertNotNull(file);
public void testDownload(String mediaId) throws WxErrorException {
File file = this.wxService.getMediaService().download(mediaId);
assertThat(file).isNotNull();
System.out.println(file);
}

Expand All @@ -82,4 +84,11 @@ public void testUploadImg() throws WxErrorException {
String res = this.wxService.getMediaService().uploadImg(new File(url.getFile()));
assertThat(res).isNotEmpty();
}

@Test
public void testGetJssdkFile() throws WxErrorException {
File file = this.wxService.getMediaService().getJssdkFile("....");
assertThat(file).isNotNull();
System.out.println(file);
}
}

0 comments on commit 3c391c5

Please sign in to comment.