English 中文(简体)
广播接收处
原标题:Start Android TTS from Broadcast Receiver or Service

我试图从这个背景中掌握技术。 但是,我从来没有听说过。 我有一个广播接收器,开始服务。 我把我的《特工守则》放在其中,但从未讲过。 我知道这一方法正在被称作“(我把它放在一个空白点上),但它仍然没有工作。

这里是我的标志,但它似乎没有包含任何关于多边贸易体系服务的内容。

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

事先感谢大家!

最佳回答

这将有助于看到贵国的《多边贸易体系法》,使人民更容易帮助你们。 自2006年以来 我已经在广播电视公司工作,这里就是一个从我手法中删除的例子。

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;
    }
}

1. 在您的广播接收人希望发表讲话时,开始提供技术服务:

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

我希望这能帮助某个人,如果不是帮忙的话。

问题回答

如果要发言的发言稿来自广播听众,你也可以尝试这样做。 首先是创建一种服务

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);

    }
}

确保你在使用提款机之前开始服务,并检查是否有提款发动机

它为我工作(在主要灾情上加一)。

public class TES extends Service implements TextToSpeech.OnInitListener {

  private TextToSpeech tts;

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

  @Override
  public void onCreate() {
    super.onCreate();
  }

  @Override
  public void onDestroy() {
    // TODO Auto-generated method stub
    if (tts != null) {
        tts.stop();
        tts.shutdown();
    }
    super.onDestroy();
  }

  @Override
  public void onStart(Intent intent, int startId) {
    tts = new TextToSpeech(this, this);
    speakOut();
  }

  @Override
  public void onInit(int status) {
    if (status == TextToSpeech.SUCCESS) {
      int result = tts.setLanguage(Locale.US);
      if (result == TextToSpeech.LANG_MISSING_DATA
        || result == TextToSpeech.LANG_NOT_SUPPORTED) {
          Log.e("TTS", "This Language is not supported");
      }
      speakOut();
    } else {
      Log.e("TTS", "Initilization Failed!");
    }
  }

  private void speakOut() {
    tts.speak("its working", TextToSpeech.QUEUE_FLUSH, null);
  }
}

Android TTS is a bounded service. Broadcast receiver has a limited context and can t bind himself to any service. However, It can START a service. All the examples shown here are of services that starting the TTS engine and of receiver that starts them. You can also do it with activity but if you don t need UI a service is better. I just think it s a good idea to understand how it works and why is works. Good luck.

采用科特林,上述答案可以改写为:

www.un.org/Depts/DGACM/index_spanish.htm

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

www.un.org/Depts/DGACM/index_spanish.htm

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
    }
}

并在<代码>中:

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

<service android:name=".TTS" />




相关问题
Android - ListView fling gesture triggers context menu

I m relatively new to Android development. I m developing an app with a ListView. I ve followed the info in #1338475 and have my app recognizing the fling gesture, but after the gesture is complete, ...

AsyncTask and error handling on Android

I m converting my code from using Handler to AsyncTask. The latter is great at what it does - asynchronous updates and handling of results in the main UI thread. What s unclear to me is how to handle ...

Android intent filter for a particular file extension?

I want to be able to download a file with a particular extension from the net, and have it passed to my application to deal with it, but I haven t been able to figure out the intent filter. The ...

Android & Web: What is the equivalent style for the web?

I am quite impressed by the workflow I follow when developing Android applications: Define a layout in an xml file and then write all the code in a code-behind style. Is there an equivalent style for ...

TiledLayer equivalent in Android [duplicate]

To draw landscapes, backgrounds with patterns etc, we used TiledLayer in J2ME. Is there an android counterpart for that. Does android provide an option to set such tiled patterns in the layout XML?

Using Repo with Msysgit

When following the Android Open Source Project instructions on installing repo for use with Git, after running the repo init command, I run into this error: /c/Users/Andrew Rabon/bin/repo: line ...

Android "single top" launch mode and onNewIntent method

I read in the Android documentation that by setting my Activity s launchMode property to singleTop OR by adding the FLAG_ACTIVITY_SINGLE_TOP flag to my Intent, that calling startActivity(intent) would ...

From Web Development to Android Development

I have pretty good skills in PHP , Mysql and Javascript for a junior developer. If I wanted to try my hand as Android Development do you think I might find it tough ? Also what new languages would I ...

热门标签