Browse Source

#708 企业微信增加获取高清语音素材接口

master
Binary Wang 6 years ago
parent
commit
3c391c5778
3 changed files with 40 additions and 7 deletions
  1. +17
    -1
      weixin-java-cp/src/main/java/me/chanjar/weixin/cp/api/WxCpMediaService.java
  2. +8
    -0
      weixin-java-cp/src/main/java/me/chanjar/weixin/cp/api/impl/WxCpMediaServiceImpl.java
  3. +15
    -6
      weixin-java-cp/src/test/java/me/chanjar/weixin/cp/api/impl/WxCpMediaServiceImplTest.java

+ 17
- 1
weixin-java-cp/src/main/java/me/chanjar/weixin/cp/api/WxCpMediaService.java View File

@@ -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>
@@ -59,6 +60,21 @@ public interface WxCpMediaService {
*/
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>
* 上传图片.
@@ -69,7 +85,7 @@ public interface WxCpMediaService {
* </pre>
*
* @param file 上传的文件对象
* @return 返回图片url
* @return 返回图片url
*/
String uploadImg(File file) throws WxErrorException;
}

+ 8
- 0
weixin-java-cp/src/main/java/me/chanjar/weixin/cp/api/impl/WxCpMediaServiceImpl.java View File

@@ -48,6 +48,14 @@ public class WxCpMediaServiceImpl implements WxCpMediaService {
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


+ 15
- 6
weixin-java-cp/src/test/java/me/chanjar/weixin/cp/api/impl/WxCpMediaServiceImplTest.java View File

@@ -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"}
@@ -47,8 +48,9 @@ public class WxCpMediaServiceImplTest {
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) {
@@ -70,9 +72,9 @@ public class WxCpMediaServiceImplTest {
}

@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);
}

@@ -82,4 +84,11 @@ public class WxCpMediaServiceImplTest {
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);
}
}

Loading…
Cancel
Save