Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

新增下载微信jssdk上传的高清语音素材的接口,格式为.speex #1287

Merged
merged 2 commits into from
Nov 19, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,22 @@ public interface WxMpMaterialService {
*/
File mediaDownload(String mediaId) throws WxErrorException;

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

/**
* <pre>
* 上传图文消息内的图片获取URL
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,14 @@ public File mediaDownload(String mediaId) throws WxErrorException {
"media_id=" + mediaId);
}

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

@Override
public WxMediaImgUploadResult mediaImgUpload(File file) throws WxErrorException {
return this.wxMpService.execute(MediaImgUploadRequestExecutor.create(this.wxMpService.getRequestHttp()), IMG_UPLOAD_URL, file);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -819,6 +819,10 @@ enum Material implements WxMpApiUrl {
* get.
*/
MEDIA_GET_URL(API_DEFAULT_HOST_URL, "/cgi-bin/media/get"),
/**
* jssdk media get.
*/
JSSDK_MEDIA_GET_URL(API_DEFAULT_HOST_URL, "/cgi-bin/media/get/jssdk"),
/**
* upload.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ public class WxMpMaterialServiceImplTest {
private WxMpMaterialCountResult wxMaterialCountResultBeforeTest;
// 以下为media接口的测试
private List<String> mediaIdsToDownload = new ArrayList<>();
// 以下为高清语音接口的测试
private List<String> voiceMediaIdsToDownload = new ArrayList<>();

@DataProvider
public Object[][] mediaFiles() {
Expand Down Expand Up @@ -289,6 +291,11 @@ public void testUploadMedia(String mediaType, String fileType, String fileName)
if (res.getMediaId() != null && !mediaType.equals(WxConsts.MediaFileType.VIDEO)) {
//video 不支持下载,所以不加入
this.mediaIdsToDownload.add(res.getMediaId());

// 音频media, 用于测试下载高清语音接口
if (mediaType.equals(WxConsts.MediaFileType.VOICE)) {
this.voiceMediaIdsToDownload.add(res.getMediaId());
}
}

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

@DataProvider
public Object[][] downloadJssdkMedia() {
Object[][] params = new Object[this.voiceMediaIdsToDownload.size()][];
for (int i = 0; i < this.voiceMediaIdsToDownload.size(); i++) {
params[i] = new Object[]{this.voiceMediaIdsToDownload.get(i)};
}
return params;
}

@Test(dependsOnMethods = {"testUploadMedia"}, dataProvider = "downloadMedia")
public void testDownloadMedia(String mediaId) throws WxErrorException {
File file = this.wxService.getMaterialService().mediaDownload(mediaId);
assertNotNull(file);
System.out.println(file.getAbsolutePath());
}

@Test(dependsOnMethods = {"testUploadMedia"}, dataProvider = "downloadJssdkMedia")
public void testDownloadJssdkMedia(String mediaId) throws WxErrorException {
File file = this.wxService.getMaterialService().jssdkMediaDownload(mediaId);
assertNotNull(file);
System.out.println(file.getAbsolutePath());
}
}