安卓实现语音播放数字(不调用第三方接口)

仅仅调用安卓手机自带的语音工具,不需要调用诸如讯飞、百度等第三方接口

下面提供一个demo,有需要的同学就可以自行修改复用。

1. 新建一个安卓项目,布局,添加输入框(用来输入要播放的数字),添加一个按钮(触发事件)

   

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <Button
        android:id="@+id/btn_video"
        android:layout_width="174dp"
        android:layout_height="114dp"
        android:text="Button"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.513" />

    <EditText
        android:id="@+id/editText"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:ems="10"
        android:inputType="textPersonName"
        android:text=""
        app:layout_constraintBottom_toTopOf="@+id/btn_video"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.542"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>

2. 主体实现类(主要用了自带的SoundPool类)

 

 package com.example.videodemo;
   
   import androidx.appcompat.app.AppCompatActivity;
   
   import android.media.AudioManager;
   import android.media.SoundPool;
   import android.os.Bundle;
   import android.view.View;
   import android.widget.Button;
   import android.widget.EditText;
   
   import java.util.HashMap;
   
   
   public class MainActivity extends AppCompatActivity implements View.OnClickListener{
   
       private Button button1;
       private EditText mEt;
       private SoundPool mSoundPool = null;
       private HashMap<Integer, Integer> soundID = new HashMap<Integer, Integer>();
   
       @Override
       protected void onCreate(Bundle savedInstanceState) {
           super.onCreate(savedInstanceState);
           setContentView(R.layout.activity_main);
   
           try{
               initSP();
           } catch (Exception e) {
               e.printStackTrace();
           }
           bindViews();
       }
   
   
       private void initSP() {
           //设置最多可以容纳的音频流
           mSoundPool = new SoundPool(11,AudioManager.STREAM_SYSTEM, 2);
           soundID.put(1, mSoundPool.load(this, R.raw.sound_1, 1));
           soundID.put(2, mSoundPool.load(this, R.raw.sound_2, 1));
           soundID.put(3, mSoundPool.load(this, R.raw.sound_3, 1));
           soundID.put(4, mSoundPool.load(this, R.raw.sound_4, 1));
           soundID.put(5, mSoundPool.load(this, R.raw.sound_5, 1));
           soundID.put(6, mSoundPool.load(this, R.raw.sound_6, 1));
           soundID.put(7, mSoundPool.load(this, R.raw.sound_7, 1));
           soundID.put(8, mSoundPool.load(this, R.raw.sound_8, 1));
           soundID.put(9, mSoundPool.load(this, R.raw.sound_9, 1));
           soundID.put(0, mSoundPool.load(this, R.raw.sound_0, 1));
           soundID.put(11, mSoundPool.load(this, R.raw.sound_bobao, 1));
       }
   
       //根据 输入的数字 选择需要播放的语音
      public void soundPick(){
           mEt = (EditText) findViewById(R.id.editText);
           String s = mEt.getText().toString();
           String[] str = new String[s.length()];
           for (int i = 0; i < s.length(); i++){
               str[i] = s.substring(i, i+1);
               try {
                   Thread.sleep(600);
                   mSoundPool.play(soundID.get(Integer.parseInt(str[i])),1,1,0,0,1);
               } catch (InterruptedException e) {
                   e.printStackTrace();
               }
           }
       }
   
       //按钮监听
       private void bindViews() {
           button1 = (Button) findViewById(R.id.btn_video);
           button1.setOnClickListener(MainActivity.this);
       }
   
       public void onClick(View v) {
           soundPick();
           try {
               Thread.sleep(600);
           } catch (InterruptedException e) {
               e.printStackTrace();
           }
           mSoundPool.play(soundID.get(11),1,1,0,0,1);
       }
   }

PS:

        这个实现方法需要在资源文件中导入相关的音频文件,从0~9十个数字的独立音频(资源我会放到我的主页里面,有需要的可以自己去download)。

        没有用到外部的语音接口(商用的话,外部语音接口要钱……)。

        主要思想就是拼接,把输入的一串字符拆成一个一个的数字,然后拿每一个数字去匹配对应的音频文件,播放。其中用到了线程休眠,免得一段还没说完,另一段就抢着说的情况。

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
实现上传视频并且利用百度语音识别API给视频添加字幕,需要完成以下几个步骤: 1. 在 Android Studio 中创建一个上传视频的功能,可以使用第三方库,例如 retrofit 或者 volley,实现上传视频到服务器。 2. 调用百度语音识别 API,将上传的视频进行语音识别,得到视频中的语音内容。 3. 将语音内容转化为字幕格式,例如 SRT 格式。 4. 将字幕文件与上传的视频进行合并,生成新的视频文件。 下面是大致的代码实现步骤: 1. 上传视频到服务器 ``` // 创建 Retrofit 实例 Retrofit retrofit = new Retrofit.Builder() .baseUrl("http://your.server.url/") .build(); // 创建 API 接口 VideoUploadApi api = retrofit.create(VideoUploadApi.class); // 创建 FileRequestBody,用于上传视频文件 FileRequestBody fileRequestBody = new FileRequestBody(videoFile); // 调用 API 接口上传视频 Call<UploadResult> call = api.uploadVideo(fileRequestBody); call.enqueue(new Callback<UploadResult>() { @Override public void onResponse(Call<UploadResult> call, Response<UploadResult> response) { // 上传成功 UploadResult result = response.body(); // 处理上传结果 } @Override public void onFailure(Call<UploadResult> call, Throwable t) { // 上传失败 // 处理上传失败的情况 } }); ``` 2. 调用百度语音识别 API ``` // 创建百度语音识别 API 实例 BaiduSpeechApi api = new BaiduSpeechApi(appId, appKey, secretKey); // 调用语音识别 API,获取语音内容 String speechContent = api.recognizeSpeech(videoFile); ``` 3. 将语音内容转化为字幕格式 ``` // 将语音内容转化为 SRT 格式字幕 String srtSubtitle = SrtSubtitleConverter.convertToSrt(speechContent); ``` 4. 将字幕文件与上传的视频进行合并 ``` // 创建 FFmpeg 实例 FFmpeg ffmpeg = FFmpeg.getInstance(context); // 设置 FFmpeg 命令行 String command = String.format("ffmpeg -i %s -vf subtitles=%s %s", videoFile.getAbsolutePath(), subtitleFile.getAbsolutePath(), outputVideoFile.getAbsolutePath()); // 执行 FFmpeg 命令行 ffmpeg.execute(command, new ExecuteBinaryResponseHandler() { @Override public void onSuccess(String message) { // 合并成功 } @Override public void onFailure(String message) { // 合并失败 } }); ``` 以上是大致的实现步骤,具体实现细节还需要根据具体情况进行调整。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值