Reputation: 29
This application works on an Android phone, but when it is running on an Android Wear watch, it crashes after pressing the record button. Can someone post example code for an audio recording app?
Here is the current code showing my progress so far.
The java Activity:
import android.app.*;
import android.os.*;
import android.widget.*;
import android.app.*;
import android.os.*;
import android.content.*;
import android.Manifest;
import android.content.pm.PackageManager;
import android.util.*;
import android.view.*;
import java.io.*;
import android.media.*;
import android.provider.*;
public class WearableActivity extends Activity {
private Button play;
private Button record;
private Button stop;
private MediaRecorder myAudioRecorder;
private String outputFile;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_wearable);
play = (Button) findViewById(R.id.play);
stop = (Button) findViewById(R.id.stop);
record = (Button) findViewById(R.id.record);
//stop.setEnabled(false);
// play.setEnabled(false);
outputFile = Environment.getExternalStorageDirectory().getAbsolutePath() + "/recording.3gp";
myAudioRecorder = new MediaRecorder();
myAudioRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
myAudioRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
myAudioRecorder.setAudioEncoder(MediaRecorder.OutputFormat.AMR_NB);
myAudioRecorder.setOutputFile(outputFile);
record.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
try {
myAudioRecorder.prepare();
myAudioRecorder.start();
} catch (IllegalStateException ise) {
// make somthing
} catch (IOException ioe) {
// make somthing
}
record.setEnabled(false);
stop.setEnabled(true);
Toast.makeText(getApplicationContext(), "Recording started", Toast.LENGTH_LONG).show();
}
});
stop.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
myAudioRecorder.stop();
myAudioRecorder.release();
myAudioRecorder = null;
record.setEnabled(true);
stop.setEnabled(false);
play.setEnabled(true);
Toast.makeText(getApplicationContext(), "Audio Recorder successfully", Toast.LENGTH_LONG).show();
}
});
play.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
MediaPlayer mediaPlayer = new MediaPlayer();
try {
mediaPlayer.setDataSource(outputFile);
mediaPlayer.prepare();
mediaPlayer.start();
Toast.makeText(getApplicationContext(), "Playing Audio", Toast.LENGTH_LONG).show();
} catch (Exception e) {
// make something
}
}
});
}
}
wearable.xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical">
<Button
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="record"
android:id="@+id/record" />
<Button
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="stop"
android:id="@+id/stop" />
<Button
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="play1"
android:id="@+id/play" />
</LinearLayout>
Manifest.xml:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.mycompany.Zer0">
<uses-permission
android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission
android:name="android.permission.RECORD_AUDIO" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@android:style/Theme.DeviceDefault.Light">
<activity
android:name=".WearableActivity"
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>
(I only added the following lines because it wouldn't let me submit the edit without them...)
Should I try a completely different approach, or can the phone app be transformed in a way that will work on the watch?
Upvotes: 1
Views: 1128
Reputation: 1683
Try this. It records an audio using the wear microphone and then plays it. https://github.com/googlesamples/android-WearSpeakerSample
This is the apk built: https://drive.google.com/open?id=1z6lwVoWh89gbGz43c_6PNPVS4Os6s4nu
I have put it also in the Google Play with a watchface: https://play.google.com/store/apps/details?id=com.jorc.android.wearable.watchface&hl=en
Upvotes: 1