package com.longtu.sample;

import android.app.Activity;
import android.content.Intent;
import android.content.res.Configuration;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.bh.sdk.Interface.LTBaseSDK;
import com.longtu.sdk.base.LTBaseParam;
import com.longtu.sdk.base.multiMedia.LTMultiMediaCallback;
import com.longtugame.globalsample.R;


/**
 * 语音功能
 * 作者：liqingshuo on 2019-12-02 19:35
 *
 * @author jinmingqiang
 */
public class LTBase_Demo_MultiMedia extends Activity implements OnClickListener {
	private Button StartRecording, StopRecording, PlayRecordedFileVoice, DownloadRecordedFile, PlayRecordedFile,
			SpeechToText;
	static Activity activity;
	private EditText editText;
	private String playUrl;
	
	public static String TAG = "LTBaseMultiMedia.demo";

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		super.onCreate(savedInstanceState);
		getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
		getWindow().setFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON, WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
		activity = this;
		setContentView(R.layout.longtu_demo_multimedia);
		StartRecording = (Button) findViewById(R.id.StartRecording);
		StopRecording = (Button) findViewById(R.id.StopRecording);
		PlayRecordedFileVoice = (Button) findViewById(R.id.PlayRecordedFileVoice);
		//DownloadRecordedFile = (Button) findViewById(RhelperUtil.getIdResIDByName(activity, "DownloadRecordedFile"));

		PlayRecordedFile = (Button) findViewById(R.id.PlayRecordedFile);
		//SpeechToText = (Button) findViewById(RhelperUtil.getIdResIDByName(activity, "SpeechToText"));
		editText = (EditText) findViewById(R.id.textView1);
		StartRecording.setOnClickListener(this);
		StopRecording.setOnClickListener(this);
		PlayRecordedFileVoice.setOnClickListener(this);
		//DownloadRecordedFile.setOnClickListener(this);
		PlayRecordedFile.setOnClickListener(this);
		//SpeechToText.setOnClickListener(this);
		editText.setOnClickListener(this);

		Log.d(TAG, "语音SDK设置回调");
		LTBaseSDK.getInstance(activity).LTBaseMultiMediaListener(new LTMultiMediaCallback() {
			@Override
			public void LTBaseMultiMediaSuccess(int i, Intent intent) {
				final  Intent data = intent;
				Log.d(TAG,"code=" + i);
				switch (i) {
					case LTBaseParam.CODE_PTT_STREAMINGRECOGNITION_SUCCESS: {//录音成功
						//语音转文字识别的文本
						String textString = data.getStringExtra("text");
						//录音在后台的 url 地址，录音在服务器存放90天
						playUrl = data.getStringExtra("file_id");
						editText.setText(textString);
						Toast.makeText(activity, "录音成功", Toast.LENGTH_LONG).show();
						break;
					}

					case LTBaseParam.CODE_PTT_STREAMINGRECOGNITION_FAIL: {//录音失败
						Log.d(TAG, "录音失败");
						Toast.makeText(activity, "录音失败", Toast.LENGTH_LONG).show();
						break;

					}

					case LTBaseParam.CODE_PTT_PLAY_SUCCESS: {
						Log.d(TAG, "播放语音成功");
						break;
					}

					case LTBaseParam.CODE_PTT_PLAY_FAIL: {
						Log.d(TAG, "播放语音失败");
						break;
					}
					default:

				}
			}

			@Override
			public void LTBaseMultiMediaFail(int i, String s) {
				Log.d(TAG, "语音SDK异常");
			}
		});


	}

	
	@Override
	public void onClick(View view) {
		switch (view.getId()) {
		case R.id.StartRecording:
			Log.d(TAG, "StartRecording=====");
			//Toast.makeText(this, "开始录音了，停止请点击停止录音", Toast.LENGTH_LONG).show();
			LTBaseSDK.getInstance(activity).LTBaseStartRecordingWithStreamingRecognition();
			break;
		case R.id.StopRecording:
			Log.d(TAG, "StopRecording=====");
			LTBaseSDK.getInstance(activity).LTBaseStopRecording();
			//Toast.makeText(this, "停止录音了，播放请点击播放录音", Toast.LENGTH_LONG).show();
			break;

		case R.id.PlayRecordedFile:
			Log.d(TAG, "PlayRecordedFile=====");
			//Toast.makeText(this, "播放语音", Toast.LENGTH_LONG).show();
			LTBaseSDK.getInstance(activity).LTBasePlayRecordedFile(playUrl);
			break;

		case R.id.PlayRecordedFileVoice:
			Log.d(TAG, "PlayRecordedFileVoice=====");
			//Toast.makeText(this, "播放变声语音", Toast.LENGTH_LONG).show();
			LTBaseSDK.getInstance(activity).LTBasePlayRecordedFileVoice(playUrl, 1);

		default:
			break;
		}
	}
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {

		super.onActivityResult(requestCode, resultCode, data);
	}
	@Override
	protected void onPause() {

		super.onPause();
	}
	@Override
	protected void onStart() {

		super.onStart();
	}
	@Override
	protected void onResume() {

		super.onResume();
	}
	@Override
	protected void onNewIntent(Intent newIntent) {

		super.onNewIntent(newIntent);
	}
	@Override
	protected void onStop() {

		super.onStop();
	}
	@Override
	protected void onDestroy() {

		super.onDestroy();

	}
	@Override
	protected void onRestart() {

		super.onRestart();
	}
	@Override
	public void onBackPressed() {

		super.onBackPressed();
	}
	
	@Override
	protected void onSaveInstanceState(Bundle outState) {

		super.onSaveInstanceState(outState);
	}
	
	@Override
	public void onConfigurationChanged(Configuration newConfig) {

		super.onConfigurationChanged(newConfig);
	}
	
	@Override
	public void onRequestPermissionsResult(int requestCode,
			String[] permissions, int[] grantResults) {
		Log.d(TAG, "onRequestPermissionsResult");
		LTBaseSDK.getInstance(this).onRequestPermissionsResult(requestCode, permissions, grantResults);
		super.onRequestPermissionsResult(requestCode, permissions, grantResults);
		
	}
	
	
}
