Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.sriyanksiddhartha.speechtotext;
- import android.content.Intent;
- import android.os.Bundle;
- import android.os.Environment;
- import android.os.Handler;
- import android.speech.RecognizerIntent;
- import androidx.appcompat.app.AppCompatActivity;
- import android.util.Log;
- import android.view.View;
- import android.widget.ArrayAdapter;
- import android.widget.EditText;
- import android.widget.ListView;
- import android.widget.TextView;
- import android.widget.Toast;
- import com.android.volley.Request;
- import com.android.volley.RequestQueue;
- import com.android.volley.Response;
- import com.android.volley.VolleyError;
- import com.android.volley.toolbox.JsonObjectRequest;
- import com.android.volley.toolbox.Volley;
- import com.mapzen.speakerbox.Speakerbox;
- import org.json.JSONArray;
- import org.json.JSONException;
- import org.json.JSONObject;
- import java.io.BufferedReader;
- import java.io.File;
- import java.io.FileInputStream;
- import java.io.FileNotFoundException;
- import java.io.FileOutputStream;
- import java.io.IOException;
- import java.io.InputStream;
- import java.io.InputStreamReader;
- import java.io.PrintWriter;
- import java.util.ArrayList;
- import java.util.Iterator;
- import java.util.List;
- import java.util.Locale;
- public class MainActivity extends AppCompatActivity {
- private TextView TextViewResult;
- private TextView TextViewPanduan;
- private ListView ListViewMsg;
- private View ViewButtonSend;
- private EditText EditText1;
- private EditText EditTextMsg;
- //--
- private RequestQueue RequestQueue1;
- //--
- private static final String StringTAG = "MEDIA";
- String StringKey="",StringFind="";
- List<String> ListItems=new ArrayList<String>();
- boolean booleanisMine = true;
- private List<ChatMessage> ListChatMessages;
- private ArrayAdapter<ChatMessage> ArrayAdapterMessage;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- TextViewResult = (TextView) findViewById(R.id.txvResult);
- TextViewPanduan = (TextView) findViewById(R.id.txtPanduan);
- TextViewResult.setVisibility(View.INVISIBLE);
- TextViewPanduan.setText("Panduan Disini");
- ListChatMessages = new ArrayList<>();
- ListViewMsg = (ListView) findViewById(R.id.list_msg);
- ViewButtonSend = findViewById(R.id.btn_chat_send);
- EditTextMsg = (EditText) findViewById(R.id.msg_type);
- //set ListView adapter first
- adapter = new MessageAdapter(this, R.layout.item_chat_left, ListChatMessages);
- ListViewMsg.setAdapter(adapter);
- }
- public void getSpeechInput(View view) {
- Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
- intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
- intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE, Locale.getDefault());
- if (intent.resolveActivity(getPackageManager()) != null) {
- startActivityForResult(intent, 10);
- } else {
- Toast.makeText
- (
- this,
- "Perangkat Kamu Tidak Kompatible !!!",
- Toast.LENGTH_SHORT
- ).show();
- }
- }
- @Override
- protected void onActivityResult(int IntrequestCode, int IntresultCode, Intent IntentData) {
- super.onActivityResult(IntrequestCode, IntResultCode, IntentData);
- switch (IntRequestCode) {
- case 10:
- if (IntresultCode == RESULT_OK && IntentData != null)
- {
- ListViewMsg.setVisibility(View.VISIBLE);
- TextViewPanduan.setVisibility(View.GONE);
- ArrayList<String> result = IntentData.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS); //merubah suara ke text
- TextViewResult.setText(result.get(0)); //menampilkan hasil suara yang kita ucapkan dalam bentuk text
- StringKey=result.get(0).toLowerCase();
- if(StringKey.equals("terima kasih")
- || StringKey.equals("makasih") ||
- (
- (StringKey.contains("terima kasih") || StringKey.contains("makasih")
- )
- && !StringKey.contains("berjudul")) )
- {
- StringFind="sama-sama";
- }
- else if(!StringKey.isEmpty()){
- RequestQueue1 = Volley.newRequestQueue(getApplicationContext()); //volley untuk mencari data buku
- FunctionJsonParse(); // fungsi untuk mencari buku
- if(StringFind.isEmpty()){
- StringFind="Keyword yang anda masukan belum tersedia, "
- + "Silahkan bertanya dengan format 'buku berjudul "
- + "<judul buku> pengarang <nama pengarang>' atau "
- + "'judul <judul buku> karangan <nama pengarang>'";
- }
- }
- ChatMessage ChatMessage1 = new ChatMessage(StringKey, booleanisMine);
- ListChatMessages.add(ChatMessage1);
- ArrayAdapterMessage.notifyDataSetChanged();
- booleanisMine = false;
- Runnable r = new Runnable() {
- @Override
- public void run(){
- Speakerbox speakerbox = new Speakerbox(getApplication());
- speakerbox.play(StringFind); //perintah untuk mengucapkan buku yang dicari
- ChatMessage ChatMessage1 = new ChatMessage(StringFind, booleanisMine);
- ListChatMessages.add(ChatMessage1);
- ArrayAdapterMessage.notifyDataSetChanged();
- booleanisMine = true;
- StringFind="";
- }
- };
- Handler handler1 = new Handler();
- handler1.postDelayed(r, 2500);
- }
- break;
- }
- }
- private void checkExternalMedia(){
- boolean booleanExternalStorageAvailable = false;
- boolean booleanExternalStorageWriteable = false;
- String state = Environment.getExternalStorageState();
- if (Environment.MEDIA_MOUNTED.equals(state))
- {
- booleanExternalStorageAvailable = booleanExternalStorageWriteable = true;
- }
- else if (Environment.MEDIA_MOUNTED_READ_ONLY.equals(state))
- {
- booleanExternalStorageAvailable = true;
- booleanExternalStorageWriteable = false;
- }
- else
- {
- booleanExternalStorageAvailable = booleanExternalStorageWriteable = false;
- }
- Toast.makeText(
- getApplicationContext(),
- "\n\nExternal Media: readable="
- + booleanExternalStorageAvailable
- + " writable="+booleanExternalStorageWriteable,
- Toast.LENGTH_SHORT
- ).show();
- }
- private void writeToSDFile(String tmp){
- File FileRoot = android.os.Environment.getExternalStorageDirectory();
- Toast.makeText(
- getApplicationContext(),
- "External file system root: "
- +FileRoot,Toast.LENGTH_LONG
- ).show();
- File FileDir = new File (
- FileRoot.getAbsolutePath()
- + "/download");
- FileDir.mkdirs();
- File File1 = new File(
- FileDir,
- "myData.txt");
- try {
- FileOutputStream f = new FileOutputStream(File1,true);
- PrintWriter pw = new PrintWriter(f);
- pw.println(tmp);
- pw.flush();
- pw.close();
- f.close();
- } catch (FileNotFoundException e) {
- e.printStackTrace();
- Log.i(StringTAG,
- "******* File not found. Did you"
- + " add a WRITE_EXTERNAL_STORAGE permission to the manifest?");
- }
- catch (IOException e)
- {
- e.printStackTrace();
- }
- Toast.makeText(
- getApplicationContext(),
- "\n" +
- "\n" +
- "File written to " +
- file,Toast.LENGTH_SHORT).show();
- }
- private void readRaw(){
- File FileIs = new File(Environment.getExternalStorageDirectory()
- .getAbsolutePath()+"/download", "myData.txt");
- if (FileIs.exists())
- {
- try
- { FileInputStream fIn = new FileInputStream(FileIs);
- BufferedReader myReader = new BufferedReader(
- new InputStreamReader(fIn));
- String StringaDataRow = "";
- String aBuffer = "";
- while ((StringaDataRow = myReader.readLine()) != null) {
- aBuffer += StringaDataRow + "\n";}
- TextViewResult.setText(aBuffer);
- myReader.close();
- is.createNewFile();
- }
- catch (IOException e)
- {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }else{
- Toast.makeText
- (
- getApplicationContext(),
- "File Tidak Ditemukan",
- Toast.LENGTH_SHORT
- ).show();
- }
- }
- private static ReadIP ReadIP1 = new ReadIP();
- private static final String StringReadIP=ReadIP1.text;
- //fungsi untuk mencari data buku berdasarkan key
- private void FunctionJsonParse(){
- //Toast.makeText(getApplicationContext(),"JSON",Toast.LENGTH_SHORT).show();
- String StringURL = StringReadIP+"reportBuku.php?id="+StringKey;
- Toast.makeText(getApplicationContext(),StringURL,Toast.LENGTH_SHORT).show();
- JsonObjectRequest JsonObjectRequest1 = new JsonObjectRequest(Request.Method.GET, StringURL, null,
- new Response.Listener<JSONObject>() {
- @Override
- public void onResponse(JSONObject JSONObjectResponse) {
- try{
- JSONArray JSONArray1= JSONObjectResponse.getJSONArray("data");
- for (int i=0; i < JSONArray1.length(); i++){
- JSONObject data = JSONArray1.getJSONObject(i);
- String
- StringArrayColumn1 = data.getString("0"),
- StringArrayColumn2 = data.getString("1"),
- StringArrayColumn3 = data.getString("2"),
- StringArrayColumn4 = data.getString("3");
- StringFind="Buku yang berjudul "
- + StringArrayColumn1
- + " dengan pengarang "
- + StringArrayColumn2
- + " Berada di "
- + StringArrayColumn3
- + " "
- + StringArrayColumn4;
- ;
- }
- }catch (JSONException e) {
- e.printStackTrace();
- }
- }
- }, new Response.ErrorListener() {
- @Override
- public void onErrorResponse(VolleyError error) {
- error.printStackTrace();
- }
- });
- RequestQueue1.add(JsonObjectRequest1);
- }
- }
Add Comment
Please, Sign In to add comment