Selaa lähdekoodia

1.添加语音合成表

王鹏鹏 2 vuotta sitten
vanhempi
commit
c224a2b185

+ 34 - 2
baselib/schemas/com.yingyangfly.baselib.db.AppDataBase/1.json

@@ -2,7 +2,7 @@
   "formatVersion": 1,
   "database": {
     "version": 1,
-    "identityHash": "f5a3f79b1fa4c5026224a07f7dec48bd",
+    "identityHash": "474960d2bdaae4563ccbe0029c7e2b91",
     "entities": [
       {
         "tableName": "Questions",
@@ -301,12 +301,44 @@
         },
         "indices": [],
         "foreignKeys": []
+      },
+      {
+        "tableName": "VoicePlayer",
+        "createSql": "CREATE TABLE IF NOT EXISTS `${TABLE_NAME}` (`id` INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, `words` TEXT, `url` TEXT)",
+        "fields": [
+          {
+            "fieldPath": "id",
+            "columnName": "id",
+            "affinity": "INTEGER",
+            "notNull": true
+          },
+          {
+            "fieldPath": "words",
+            "columnName": "words",
+            "affinity": "TEXT",
+            "notNull": false
+          },
+          {
+            "fieldPath": "url",
+            "columnName": "url",
+            "affinity": "TEXT",
+            "notNull": false
+          }
+        ],
+        "primaryKey": {
+          "columnNames": [
+            "id"
+          ],
+          "autoGenerate": true
+        },
+        "indices": [],
+        "foreignKeys": []
       }
     ],
     "views": [],
     "setupQueries": [
       "CREATE TABLE IF NOT EXISTS room_master_table (id INTEGER PRIMARY KEY,identity_hash TEXT)",
-      "INSERT OR REPLACE INTO room_master_table (id,identity_hash) VALUES(42, 'f5a3f79b1fa4c5026224a07f7dec48bd')"
+      "INSERT OR REPLACE INTO room_master_table (id,identity_hash) VALUES(42, '474960d2bdaae4563ccbe0029c7e2b91')"
     ]
   }
 }

+ 3 - 1
baselib/src/main/java/com/yingyangfly/baselib/db/AppDataBase.kt

@@ -5,13 +5,15 @@ import androidx.room.Database
 import androidx.room.Room
 import androidx.room.RoomDatabase
 
-@Database(entities = [QuestionsBean::class, DoctorBean::class], version = 1)
+@Database(entities = [QuestionsBean::class, DoctorBean::class, VoicePlayerBean::class], version = 1)
 abstract class AppDataBase : RoomDatabase() {
 
     abstract fun getQuestionsDao(): QuestionsDao
 
     abstract fun getDoctorsDao(): DoctorDao
 
+    abstract fun getVoicePlayerDao(): VoicePlayerDao
+
     companion object {
         @Volatile
         private var sInstance: AppDataBase? = null

+ 42 - 0
baselib/src/main/java/com/yingyangfly/baselib/db/VoicePlayerBean.java

@@ -0,0 +1,42 @@
+package com.yingyangfly.baselib.db;
+
+import androidx.room.Entity;
+import androidx.room.PrimaryKey;
+
+/**
+ * 合成语音bean
+ * @author 王鹏鹏
+ */
+@Entity(tableName = "VoicePlayer")
+public class VoicePlayerBean {
+
+    @PrimaryKey(autoGenerate = true)
+    private int id;//待合成词语
+
+    private String words;//待合成词语
+    private String url;//合成连接
+
+    public int getId() {
+        return id;
+    }
+
+    public void setId(int id) {
+        this.id = id;
+    }
+
+    public String getWords() {
+        return words;
+    }
+
+    public void setWords(String words) {
+        this.words = words;
+    }
+
+    public String getUrl() {
+        return url;
+    }
+
+    public void setUrl(String url) {
+        this.url = url;
+    }
+}

+ 29 - 0
baselib/src/main/java/com/yingyangfly/baselib/db/VoicePlayerDao.kt

@@ -0,0 +1,29 @@
+package com.yingyangfly.baselib.db
+
+import androidx.room.*
+
+@Dao
+interface VoicePlayerDao : BaseDao<VoicePlayerBean> {
+
+    @Insert(onConflict = OnConflictStrategy.REPLACE)
+    fun insert(element: VoicePlayerBean)
+
+    @Insert(onConflict = OnConflictStrategy.REPLACE)
+    override fun insertAll(list: MutableList<VoicePlayerBean>)
+
+    @Query("select * from VoicePlayer")
+    fun getAllVoicePlayerBean(): MutableList<VoicePlayerBean>
+
+    @Query("select * from VoicePlayer where id = :id")
+    fun getQuestion(id: Int): VoicePlayerBean
+
+    @Query("select * from VoicePlayer order by id desc ")
+    fun getAllByIdDesc(): MutableList<VoicePlayerBean>
+
+    @Query("delete from VoicePlayer")
+    fun deleteAll()
+
+    @Update
+    override fun update(element: VoicePlayerBean)
+
+}

+ 103 - 0
baselib/src/main/java/com/yingyangfly/baselib/player/VoicePlayer.java

@@ -0,0 +1,103 @@
+package com.yingyangfly.baselib.player;
+
+import android.content.Context;
+import android.media.AudioManager;
+import android.media.MediaPlayer;
+import android.text.TextUtils;
+
+import java.io.IOException;
+
+/**
+ * @author 王鹏鹏
+ */
+public class VoicePlayer {
+
+    private static volatile VoicePlayer instance = null;
+    private AudioManager audioManager;
+    private MediaPlayer mediaPlayer;
+    private boolean isPause;
+    private boolean isLoading;
+    private MediaPlayer.OnCompletionListener onCompletionListener;
+
+    public static VoicePlayer getInstance(Context context) {
+        if (instance == null) {
+            synchronized (VoicePlayer.class) {
+                if (instance == null) {
+                    instance = new VoicePlayer(context);
+                }
+            }
+        }
+        return instance;
+    }
+
+    private VoicePlayer(Context cxt) {
+        Context baseContext = cxt.getApplicationContext();
+        audioManager = (AudioManager) baseContext.getSystemService(Context.AUDIO_SERVICE);
+        mediaPlayer = new MediaPlayer();
+    }
+
+    public MediaPlayer getPlayer() {
+        return mediaPlayer;
+    }
+
+    public boolean isPlaying() {
+        return mediaPlayer.isPlaying();
+    }
+
+    public void play(final String url, final MediaPlayer.OnCompletionListener listener) {
+        if (mediaPlayer.isPlaying()) {
+            stop();
+        }
+        isPause = false;
+        onCompletionListener = listener;
+        try {
+            setSpeaker();
+            if (!TextUtils.isEmpty(url)) {
+                mediaPlayer.setDataSource(url);
+                mediaPlayer.prepareAsync();
+                isLoading = true;
+                mediaPlayer.setOnPreparedListener(mp -> {
+                    isLoading = false;
+                    mediaPlayer.start();
+                });
+                mediaPlayer.setOnCompletionListener(mp -> {
+                    stop();
+                    onCompletionListener = null;
+                });
+            }
+        } catch (IOException e) {
+            e.printStackTrace();
+        }
+    }
+
+    public void stop() {
+        isPause = false;
+        isLoading = false;
+        mediaPlayer.stop();
+        mediaPlayer.reset();
+        if (onCompletionListener != null) {
+            onCompletionListener.onCompletion(mediaPlayer);
+        }
+    }
+
+    public void pause() {
+        isPause = true;
+        mediaPlayer.pause();
+    }
+
+    public void start() {
+        isPause = false;
+        mediaPlayer.start();
+    }
+
+    public boolean isPause() {
+        return isPause;
+    }
+
+    private void setSpeaker() {
+        audioManager.setMode(AudioManager.MODE_NORMAL);
+        audioManager.setSpeakerphoneOn(true);
+        mediaPlayer.setAudioStreamType(AudioManager.STREAM_RING);
+    }
+
+}