当前位置: 代码迷 >> java >> 文字转语音-Android Studio中的Google地图和标记
  详细解决方案

文字转语音-Android Studio中的Google地图和标记

热度:10   发布时间:2023-07-25 19:57:12.0

我试图创建包含Google Maps和一些标记的Android应用。 问题是我希望具有从这些标记中读取文本的功能。 因此,我添加了第一个标记:

@Override
public void onMapReady(GoogleMap map) {
    map.setMapType(GoogleMap.MAP_TYPE_TERRAIN);
map.addMarker(new MarkerOptions()
            .position(new LatLng(52.4774762, 13.4245084))
            .title("Sahara Imbiss")
            .snippet("Very nice food here")
            .icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_GREEN)));

因此,我想大声朗读摘录中的文字。

我找到了一些带有文字转语音的模板,例如

t1 = new TextToSpeech(getApplicationContext(), new TextToSpeech.OnInitListener() {
        @Override
        public void onInit(int status) {
            if (status != TextToSpeech.ERROR) {
                t1.setLanguage(Locale.ENGLISH);
            }
        }
    });

在onCreate然后是阅读部分:

final Handler handler = new Handler();
    handler.postDelayed(new Runnable() {
        @Override
        public void run() {
            t1.speak("welcome to my app", TextToSpeech.QUEUE_FLUSH, null);
        }
    }, 100);

但我不确定如何使用它来阅读我的摘录文本。 我想添加更多标记,例如在单击它们之后(这是一个好主意)以从中读取文本。

你知道我该怎么做吗? 提前致谢。

这是实现此目的的演示实现。

package demo.maps.texttospeech;

import android.speech.tts.TextToSpeech;
import android.support.v4.app.FragmentActivity;
import android.os.Bundle;
import android.util.Log;
import android.widget.Toast;

import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.OnMapReadyCallback;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.Marker;
import com.google.android.gms.maps.model.MarkerOptions;

import java.util.Locale;

public class MapsActivity extends FragmentActivity implements OnMapReadyCallback {

    private GoogleMap mMap;
    private TextToSpeech textToSpeech;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_maps);
        // Obtain the SupportMapFragment and get notified when the map is ready to be used.
        SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
                .findFragmentById(R.id.map);
        mapFragment.getMapAsync(this);

        textToSpeech = new TextToSpeech(getApplicationContext(), new TextToSpeech.OnInitListener() {
            @Override
            public void onInit(int status) {
                if (status == TextToSpeech.SUCCESS) {
                    int ttsLang = textToSpeech.setLanguage(Locale.US);

                    if (ttsLang == TextToSpeech.LANG_MISSING_DATA
                            || ttsLang == TextToSpeech.LANG_NOT_SUPPORTED) {
                        Log.e("TTS", "The Language is not supported!");
                    } else {
                        Log.i("TTS", "Language Supported.");
                    }
                    Log.i("TTS", "Initialization success.");
                } else {
                    Toast.makeText(getApplicationContext(), "TTS Initialization failed!", Toast.LENGTH_SHORT).show();
                }
            }
        });
    }


    /**
     * Manipulates the map once available.
     * This callback is triggered when the map is ready to be used.
     * This is where we can add markers or lines, add listeners or move the camera. In this case,
     * we just add a marker near Sydney, Australia.
     * If Google Play services is not installed on the device, the user will be prompted to install
     * it inside the SupportMapFragment. This method will only be triggered once the user has
     * installed Google Play services and returned to the app.
     */
    @Override
    public void onMapReady(GoogleMap googleMap) {
        mMap = googleMap;

        // Add a marker in Sydney and move the camera
        LatLng sydney = new LatLng(-34, 151);
        mMap.addMarker(new MarkerOptions().position(sydney).title("Marker in Sydney")).setSnippet("Very nice food here");
        mMap.moveCamera(CameraUpdateFactory.newLatLng(sydney));

        mMap.setOnMarkerClickListener(new GoogleMap.OnMarkerClickListener() {
            @Override
            public boolean onMarkerClick(Marker marker) {

                String data = marker.getTitle() + " " + marker.getSnippet();
                Log.i("TTS", "button clicked: " + data);
                int speechStatus = textToSpeech.speak(data, TextToSpeech.QUEUE_FLUSH, null);

                if (speechStatus == TextToSpeech.ERROR) {
                    Log.e("TTS", "Error in converting Text to Speech!");
                }

                return false;
            }
        });
    }
}

重要的部分是添加setOnMarkerClickListener以获得标记单击事件,然后构建设置为标记的字符串作为标题和/或代码片段。

然后在onCreate中初始化TextToSpeech对象,并最终调用textToSpeech.speak(data, TextToSpeech.QUEUE_FLUSH, null); 在标记的点击监听器中构建字符串之后。

竖起大拇指,如果这行得通!

干杯!

  相关解决方案