java - 文本转语音 - Android Studio 中的 Google map 和标记

标签 java android maps

我试图创建带有 Google map 和一些标记的 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)));

所以我希望大声朗读该 fragment 中的文本。

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

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

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

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

最佳答案

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

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 来获取标记单击事件,然后构建您设置为标记作为标题和/或代码 fragment 的字符串。

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

如果有效,请竖起大拇指!

干杯!

关于java - 文本转语音 - Android Studio 中的 Google map 和标记,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54969359/

相关文章:

java - 当某些配置文件处于 Activity 状态时,Maven 引用本地创意项目作为依赖项

java - 验证不在服务层执行

java - 对 HashMap 进行排序,同时保留重复项

android - Room 无法验证数据完整性。如何在不编写迁移步骤的情况下修复它?

android - 如何在android中检索存储在外部USB设备中的数据?

android - 覆盖 MapView 瓦片源?

python - 用匀称的多面体填充洞 - 荷兰 2 位邮政编码

java - 在运行时访问对象!

java - 非法状态异常 : Expected BEGIN_OBJECT but was BEGIN_ARRAY

r - 如何在没有绘图边界线的情况下为 map 中的区域(例如德国)着色?