Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to realize text-to-Voice Toolbox with Java

2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly explains "how to convert Java into speech toolbox". Interested friends may wish to have a look at it. The method introduced in this paper is simple, fast and practical. Next let the editor to take you to learn "Java how to achieve text-to-voice toolbox"!

Integration

Here we use Baidu Cloud's artificial intelligence speech synthesis API, individual users have a certain amount of free, basically enough, online speech synthesis supports rich SDK, basically popular languages can be easily accessed. Java, the world's most popular language, is used here.

Pom.xml introduces:

Com.baidu.aip

Java-sdk

4.12.0

Application.properties introduces:

# = =

# Baidu artificial Intelligence, apply for Baidu Cloud account and create an application

# = =

Bai-du.appId = *

Bai-du.apiKey = *

Bai-du.accessKeySecret = *

BaiDuProperties.java configuration entity:

@ Data

@ ConfigurationProperties (prefix = "bai-du")

Public class BaiDuProperties {

Private String appId

Private String apiKey

Private String accessKeySecret

}

Make a tool class:

/ * *

* Baidu Intelligent AI

, /

@ Component

@ Configuration

@ EnableConfigurationProperties ({BaiDuProperties.class})

Public class BaiDuUtils {

Private BaiDuProperties baiDu

Public BaiDuUtils (BaiDuProperties baiDu) {

This.baiDu = baiDu

}

Private AipSpeech instance

@ Value ("${file.path}")

Private String filePath

@ PostConstruct

Public void init () {

Try {

FileUtil.mkdir (filePath+SystemConstant.SF_FILE_SEPARATOR+ "voice")

Instance = new AipSpeech (baiDu.getAppId (), baiDu.getApiKey (), baiDu.getAccessKeySecret ())

/ / optional: set network connection parameters

Instance.setConnectionTimeoutInMillis (2000)

Instance.setSocketTimeoutInMillis (60000)

} catch (Exception e) {

E.printStackTrace ()

}

}

/ * *

* speech synthesis

* there may be problems with https authentication in local testing. Just call the ignoreSsl method.

* @ param text composite text, using UTF-8 encoding, please note that the length of the text must be less than 1024 bytes

, /

Public String text2Voice (String text,Boolean per) {

SslUtils.ignoreSsl ()

HashMap options = new HashMap ()

If (per) {

Options.put ("per", "3")

} else {

Options.put ("per", "4")

}

TtsResponse res = instance.synthesis (text, "zh", 1, options)

Byte [] data = res.getData ()

If (data! = null) {

Try {

String file = "voice" + SystemConstant.SF_FILE_SEPARATOR+UUID.randomUUID () + ".mp3"

Util.writeBytesToFileSystem (data, filePath + SystemConstant.SF_FILE_SEPARATOR + file)

Return file

} catch (IOException e) {

E.printStackTrace ()

}

}

Return null

}

}

Be careful

Please note that the text length must be less than 1024 bytes

There may be problems with https authentication in local testing. Just call the ignoreSsl method.

Screenshot

At this point, I believe you have a deeper understanding of "Java how to achieve text-to-voice toolbox". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report