2

I'm trying to get TTS to run in the background. But, I never get any sound. I have a broadcast receiver which starts a service. I put my TTS code in both of those, but it never speaks. I know the method is being called (I put a breakpoint on it), but it still doesn't work.

これが私のログですが、TTS サービスについては何も含まれていないようです。

10-04 22:45:30.663: WARN/InputManagerService(209): Window already focused, ignoring focus gain of: com.android.internal.view.IInputMethodClient$Stub$Proxy@4423df40
10-04 22:45:37.363: INFO/PollingManager(449): calculateShortestInterval(): shortest interval is 540000
10-04 22:45:37.413: INFO/TLSStateManager(449): org.apache.harmony.nio.internal.SocketChannelImpl@4400ece0: Wrote out 29 bytes of data with 0 bytes remaining.
10-04 22:45:38.043: ERROR/IMAPEmailService(480): Can't create default IMAP system folder Trash. Please reconfigure the folder names.
10-04 22:45:40.123: ERROR/EONS(303): EF_PNN: No short Name
10-04 22:45:41.543: ERROR/WMSTS(171): Month is invalid: 0
10-04 22:45:42.043: WARN/AudioFlinger(172): write blocked for 212 msecs, 24 delayed writes, thread 0xb998

よろしくお願いします!

4

6 に答える 6

13

TTS コードを確認すると、人々があなたを助けやすくなります。私はすでに BroadcastReceiver で TTS を使用しているので、コードから抜粋した例を次に示します。

public static class TTS extends Service implements TextToSpeech.OnInitListener, OnUtteranceCompletedListener {
    private TextToSpeech mTts;
    private String spokenText;

    @Override
    public void onCreate() {
        mTts = new TextToSpeech(this, this);
        // This is a good place to set spokenText
    }

    @Override
    public void onInit(int status) {
        if (status == TextToSpeech.SUCCESS) {
            int result = mTts.setLanguage(Locale.US);
            if (result != TextToSpeech.LANG_MISSING_DATA && result != TextToSpeech.LANG_NOT_SUPPORTED) {
                mTts.speak(spokenText, TextToSpeech.QUEUE_FLUSH, null);
            }
        }
    }

    @Override
    public void onUtteranceCompleted(String uttId) {
        stopSelf();
    }

    @Override
    public void onDestroy() {
        if (mTts != null) {
            mTts.stop();
            mTts.shutdown();
        }
        super.onDestroy();
    }

    @Override
    public IBinder onBind(Intent arg0) {
        return null;
    }
}

BroadcastReceiver で話したい場所で TTS サービスを開始します。

context.startService(new Intent(context, TTS.class));

質問者ではないにしても、これが誰かの助けになることを願っています(彼は今ではうまくいっていると確信しています)。

于 2011-04-13T09:02:50.657 に答える
2

話されるテキストがブロードキャストリスナーから来ている場合は、これを試すこともできます.最初にサービスを作成します

public class MyTell extends Service implements OnInitListener{
   public MyTell() {
   }

   public static TextToSpeech mTts;

   @Override
   public IBinder onBind(Intent intent) {
       return null;
   }

   public void onStart(Intent intent, int startId) {
       // TODO Auto-generated method stub
       mPreferences = getSharedPreferences(Mysettings.PREF_NAME, Service.MODE_PRIVATE);

       pit = Float.parseFloat(mPreferences.getString("pit","0.8"));
       rate = Float.parseFloat(mPreferences.getString("rate","1.1"));
       mTts = new TextToSpeech(this, this);
       super.onStart(intent, startId);
   }

public void onInit(int status) {
    // TODO Auto-generated method stub
    if (status == TextToSpeech.SUCCESS) {
        if (mTts.isLanguageAvailable(Locale.UK) >= 0)

        Toast.makeText( MyTell.this,
                "Sucessfull intialization of Text-To-Speech engine Mytell ",
                Toast.LENGTH_LONG).show();
        mTts.setLanguage(Locale.UK);

        mTts.setPitch(pit);
        mTts.setSpeechRate(rate);

    } else if (status == TextToSpeech.ERROR) {
        Toast.makeText(MyTell.this,
                "Unable to initialize Text-To-Speech engine",
                Toast.LENGTH_LONG).show();
    }
}}

次に、テキストを挿入するリスナーを作成します

public class MyBroadCast extends BroadcastReceiver {
    public MyPop() {
    }

    @Override
    public void onReceive(Context context, Intent intent) {
        // TODO: This method is called when the BroadcastReceiver is receiving
        // an Intent broadcast.
        //here is where you're use the service you created to speak the text
        MyTell.mTts.speak("Text to be spoken", TextToSpeech.QUEUE_FLUSH,null);

    }
}

tts エンジンを使用する前に必ずサービスを開始し、tts エンジンが利用可能かどうかも確認してください。

于 2013-03-21T19:26:17.897 に答える
0

このようなサービスを使用する Android-O 以降には、バックグラウンドの制限があります。JobIntentService を使用して、ここに示されているのと同じことを達成できます。

于 2019-03-30T08:13:52.677 に答える
0

Kotlin を使用すると、上記の回答は次のように書き直すことができます。

Receiver:

class MyReceiver : BroadcastReceiver() {
    val ttsService = Intent(context, TTS::class.java)
    context.startService(ttsService)
}

Service:

class TTS : Service(), TextToSpeech.OnInitListener {
    private var mTts: TextToSpeech? = null
    private var spokenText: String? = null

    override fun onCreate() {
        mTts = TextToSpeech(this, this)
        // This is a good place to set spokenText
        spokenText = "Hello!.."
    }

    override fun onInit(status: Int) {
        if (status == TextToSpeech.SUCCESS) {
           val result = mTts!!.setLanguage(Locale.US)
            if (result != TextToSpeech.LANG_MISSING_DATA && result != TextToSpeech.LANG_NOT_SUPPORTED) {
                Thread().run {
                    mTts!!.apply {
                        speak(spokenText, TextToSpeech.QUEUE_FLUSH, null, null)
                    }
                    Thread.sleep(10000)
                    stopSelf()
                }
            }
        } else if (status == TextToSpeech.ERROR) {
           stopSelf()
        }
    }

    override fun onDestroy() {
       if (mTts != null) {
            mTts!!.stop()
            mTts!!.shutdown()
        }
        super.onDestroy()
    }

    override fun onBind(arg0: Intent): IBinder? {
        return null
    }
}

そしてでManifest

<receiver
    android:name=".MyReceiver">
    <intent-filter>
        <action android:name="android.intent.action.xxxx" />
    </intent-filter>
</receiver>

<service android:name=".TTS" />
于 2018-04-27T21:36:29.063 に答える