Last active
December 19, 2015 03:28
-
-
Save daichan4649/5890112 to your computer and use it in GitHub Desktop.
録音/録画/音声検索
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" | |
xmlns:tools="http://schemas.android.com/tools" | |
android:layout_width="match_parent" | |
android:layout_height="match_parent" | |
android:paddingBottom="@dimen/activity_vertical_margin" | |
android:paddingLeft="@dimen/activity_horizontal_margin" | |
android:paddingRight="@dimen/activity_horizontal_margin" | |
android:paddingTop="@dimen/activity_vertical_margin" | |
tools:context=".MainActivity" > | |
<Button | |
android:id="@+id/button1" | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:layout_alignParentLeft="true" | |
android:layout_alignParentTop="true" | |
android:layout_marginLeft="14dp" | |
android:layout_marginTop="28dp" | |
android:text="録音(音声)" /> | |
<Button | |
android:id="@+id/button2" | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:layout_alignLeft="@+id/button1" | |
android:layout_below="@+id/button1" | |
android:layout_marginTop="34dp" | |
android:text="録画(動画)" /> | |
<TextView | |
android:id="@+id/speak_result" | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:layout_centerVertical="true" | |
android:layout_toRightOf="@+id/button2" /> | |
<ImageButton | |
android:id="@+id/speak" | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:layout_alignRight="@+id/button1" | |
android:layout_centerVertical="true" | |
android:src="@android:drawable/ic_btn_speak_now" /> | |
</RelativeLayout> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="utf-8"?> | |
<manifest xmlns:android="http://schemas.android.com/apk/res/android" | |
package="com.example.recordtest" | |
android:versionCode="1" | |
android:versionName="1.0" > | |
<uses-sdk | |
android:minSdkVersion="8" | |
android:targetSdkVersion="17" /> | |
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> | |
<application | |
android:allowBackup="true" | |
android:icon="@drawable/ic_launcher" | |
android:label="@string/app_name" | |
android:theme="@style/AppTheme" > | |
<activity | |
android:name="com.example.recordtest.MainActivity" | |
android:label="@string/app_name" > | |
<intent-filter> | |
<action android:name="android.intent.action.MAIN" /> | |
<category android:name="android.intent.category.LAUNCHER" /> | |
</intent-filter> | |
</activity> | |
</application> | |
</manifest> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.example.recordtest; | |
import java.io.File; | |
import java.io.IOException; | |
import java.util.List; | |
import android.app.Activity; | |
import android.app.AlertDialog; | |
import android.content.ActivityNotFoundException; | |
import android.content.Context; | |
import android.content.DialogInterface; | |
import android.content.Intent; | |
import android.database.Cursor; | |
import android.net.Uri; | |
import android.os.Bundle; | |
import android.provider.MediaStore; | |
import android.speech.RecognizerIntent; | |
import android.view.View; | |
import android.view.View.OnClickListener; | |
import android.widget.TextView; | |
import com.google.common.io.Files; | |
public class MainActivity extends Activity { | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.activity_main); | |
findViewById(R.id.button1).setOnClickListener(new OnClickListener() { | |
@Override | |
public void onClick(View v) { | |
startAudioRecorder(); | |
} | |
}); | |
findViewById(R.id.button2).setOnClickListener(new OnClickListener() { | |
@Override | |
public void onClick(View v) { | |
startMovieRecorder(); | |
} | |
}); | |
findViewById(R.id.speak).setOnClickListener(new OnClickListener() { | |
@Override | |
public void onClick(View v) { | |
startSpeakSearch(); | |
} | |
}); | |
} | |
private enum RequestType { | |
LAUNCH_MIC(0), | |
LAUNCH_MOVIE(1), | |
START_SEARCH_VOICE(2); | |
private int requestCode; | |
RequestType(int requestCode) { | |
this.requestCode = requestCode; | |
} | |
public int getRequestCode() { | |
return requestCode; | |
} | |
public static RequestType fromRequestCode(int requestCode) { | |
for (RequestType type : RequestType.values()) { | |
if (type.requestCode == requestCode) { | |
return type; | |
} | |
} | |
throw new RuntimeException("invalid code: " + requestCode); | |
} | |
} | |
private void startAudioRecorder() { | |
Intent intent = new Intent(MediaStore.Audio.Media.RECORD_SOUND_ACTION); | |
try { | |
startActivityForResult(intent, RequestType.LAUNCH_MIC.getRequestCode()); | |
} catch (Exception e) { | |
e.printStackTrace(); | |
} | |
} | |
private void startMovieRecorder() { | |
Intent intent = new Intent(MediaStore.ACTION_VIDEO_CAPTURE); | |
try { | |
startActivityForResult(intent, RequestType.LAUNCH_MOVIE.getRequestCode()); | |
} catch (Exception e) { | |
e.printStackTrace(); | |
} | |
} | |
private void startSpeakSearch() { | |
try { | |
Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH); | |
intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_FREE_FORM); | |
intent.putExtra(RecognizerIntent.EXTRA_PROMPT, "音声変換"); | |
startActivityForResult(intent, RequestType.START_SEARCH_VOICE.getRequestCode()); | |
} catch (ActivityNotFoundException e) { | |
e.printStackTrace(); | |
} | |
} | |
@Override | |
protected void onActivityResult(int requestCode, int resultCode, Intent data) { | |
super.onActivityResult(requestCode, resultCode, data); | |
switch (RequestType.fromRequestCode(requestCode)) { | |
case LAUNCH_MIC: | |
case LAUNCH_MOVIE: | |
back2MicAndMovieProcess(data); | |
break; | |
case START_SEARCH_VOICE: | |
back2VoiceSearchProcess(data); | |
break; | |
default: | |
return; | |
} | |
} | |
private void back2MicAndMovieProcess(Intent data) { | |
final String savedPath = getSavedPath(this, data); | |
// 同階層に別名保存 | |
move(savedPath, savedPath + "_"); | |
} | |
private void back2VoiceSearchProcess(Intent data) { | |
// 結果文字列リスト | |
if (data == null) { | |
return null; | |
} | |
final List<String> resultList = data.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS); | |
if (resultList == null || resultList.size() == 0) { | |
return; | |
} | |
showVoiceSearchConfirmDialog(resultList); | |
} | |
private static String getSavedPath(Context context, Intent data) { | |
// 保存先パス取得 | |
if (data == null) { | |
return null; | |
} | |
Uri uri = data.getData(); | |
if (uri == null) { | |
return null; | |
} | |
return getFilePathFromUri(context, uri); | |
} | |
private static String getFilePathFromUri(Context context, Uri uri) { | |
Cursor cursor = context.getContentResolver().query(uri, null, null, null, null); | |
cursor.moveToFirst(); | |
int index = cursor.getColumnIndex(MediaStore.Audio.AudioColumns.DATA); | |
return cursor.getString(index); | |
} | |
private static void move(final String fromPath, String toPath) { | |
try { | |
// move(copy+delete) | |
File from = new File(fromPath); | |
File to = new File(toPath); | |
Files.copy(from, to); | |
from.delete(); | |
} catch (IOException e) { | |
e.printStackTrace(); | |
} | |
} | |
private void showVoiceSearchConfirmDialog(final List<String> resultList) { | |
// ダイアログ表示 | |
final String[] results = resultList.toArray(new String[0]); | |
AlertDialog.Builder dialog = new AlertDialog.Builder(this); | |
dialog.setTitle("検索結果"); | |
dialog.setItems(results, new DialogInterface.OnClickListener() { | |
@Override | |
public void onClick(DialogInterface dialog, int index) { | |
TextView resultView = (TextView) findViewById(R.id.speak_result); | |
resultView.setText(resultList.get(index)); | |
} | |
}); | |
dialog.show(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment