java - Java中播放Azure输出格式 "audio-16khz-128kbitrate-mono-mp3"的一种方法

标签 java azure text-to-speech javasound audioformat

我曾经从 Azure Test-to-Speech API 服务获取输出格式为“riff-24khz-16bit-mono-pcm”。由于一些技术变化,我们现在获得的音频文本格式为audio-16khz-128kbitrate-mono-mp3。

在此更改之前,我们曾经执行以下操作来播放音频文本中的音频:

    String stepTitle=soundData; // audioText output from Azure
    byte[] bytes = stepTitle.getBytes();
    Base64.Decoder decoder = Base64.getDecoder();

    byte[] decoded = decoder.decode(bytes);

    InputStream input = new ByteArrayInputStream(decoded);
    AudioInputStream audioInput = null;
    try {
        ///////// This line is giving exception ////////////////////////
        audioInput = AudioSystem.getAudioInputStream(input);
    } catch (UnsupportedAudioFileException | IOException e) {
        e.printStackTrace();
    } 

    AudioFormat audioFormats = new AudioFormat(
            AudioFormat.Encoding.PCM_SIGNED,
            24000,
            16,
            1,
            1 * 2,
            24000,
            false);

如上所述,在获取音频输入流时,我收到UnsupportedAudioFileException

我尝试过使用 mp3plugin.jar。但我认为我无法让它正常工作。 请帮忙!

最佳答案

引用官方TTS REST API在文档中,我将用 key 交换 token ,并使用该 token 将文本转换为语音。我将获取音频数据的字节数组,然后使用 JLayer播放音频。

这是我的示例:

public class TestTTS {

    static String key = "f1a0ea***********fa5e35";
    static String tokenEndpoint = "https://southeastasia.api.cognitive.microsoft.com/sts/v1.0/issueToken";
    static String serviceEndpoint = "https://southeastasia.tts.speech.microsoft.com/cognitiveservices/v1";

    public static String GetToken(){

        Map<String, String> headers = new HashMap<>();

        headers.put("Ocp-Apim-Subscription-Key",key);
        headers.put("Content-Type","application/x-www-form-urlencoded");

        try(CloseableHttpClient client = HttpClients.createDefault()){
            HttpPost post = new HttpPost(new URI(tokenEndpoint));
            for (String key : headers.keySet()) {
                post.setHeader(key, headers.get(key));
            }
            post.setEntity(new StringEntity(""));
            CloseableHttpResponse result = client.execute(post);
            return EntityUtils.toString(result.getEntity());
        } catch (URISyntaxException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return "";
    }

    public static byte[] GetAudio(String token) {
        String content = "<speak version='1.0' xml:lang='en-US'><voice xml:lang='en-US' xml:gender='Female' name='en-US-JessaRUS'>Microsoft Speech Service Text-to-Speech API</voice></speak>";
        Map<String, String> headers = new HashMap<>();
        headers.put("Authorization","Bearer " + token);
        headers.put("Content-Type","application/ssml+xml");
        headers.put("X-Microsoft-OutputFormat"," audio-16khz-64kbitrate-mono-mp3");

        try(CloseableHttpClient client = HttpClients.createDefault()){
            HttpPost post = new HttpPost(new URI(serviceEndpoint));
            for (String key : headers.keySet()) {
                post.setHeader(key, headers.get(key));
            }
            post.setEntity(new StringEntity(content));
            CloseableHttpResponse result = client.execute(post);
            byte[] bytes = EntityUtils.toByteArray(result.getEntity());
            return bytes;
        } catch (URISyntaxException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return new byte[0];
    }

    public static void main(String[] args) {
        byte[] bytes = GetAudio(GetToken());
        System.out.println(bytes.length);
        try(InputStream is = new ByteArrayInputStream(bytes)){
            Player player=new Player(is);
            player.play();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (JavaLayerException e) {
            e.printStackTrace();
        }
    }
}
<小时/>

对于带有 mp3Plugin 的 JMF,我将音频数据保存到临时文件,然后播放它。

    public static void PlayWithJMF(){
        Format input1 = new AudioFormat(AudioFormat.MPEGLAYER3);
        Format input2 = new AudioFormat(AudioFormat.MPEG);
        Format output = new AudioFormat(AudioFormat.LINEAR);
        PlugInManager.addPlugIn(
                "com.sun.media.codec.audio.mp3.JavaDecoder",
                new Format[]{input1, input2},
                new Format[]{output},
                PlugInManager.CODEC
        );

        try{
            FileOutputStream fos = new FileOutputStream("tmp.mp3");
            byte[] bytes = GetAudio(GetToken());
            System.out.println(bytes.length);
            fos.write(bytes);
            fos.flush();
            fos.close();
            File file = new File("tmp.mp3");
            javax.media.Player player = Manager.createPlayer(new MediaLocator(file.toURI().toURL()));
            player.start();
            while(true){
                if(player.getState() == 500){
                    player.close();
                    break;
                }
                Thread.sleep(500);
            }
        }catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (NoPlayerException e) {
            e.printStackTrace();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

关于java - Java中播放Azure输出格式 "audio-16khz-128kbitrate-mono-mp3"的一种方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58323218/

相关文章:

visual-studio-2010 - Azure 部署中的插件子目录

c# - .net-core-2.0 azure应用程序服务502.5错误

android - 更改我的应用程序中语音识别的默认语言

android - Android 中的声音管理语音识别/TTS

java - 将 Instant 从大纪元时间转换为微秒

java - 动态报告中的异常

azure - 使用 azure 数据工厂事件将 dta 从 azure blob 容器发送到 azure 函数

ios - 如何使用 SSML 减慢文本到语音的速度 - 语音听起来扭曲/变形/ghaSTLy

java - jsp页面导入时出错

javascript - Wicket AjaxTabbedPanel 选项卡导航需要第二次点击