如何在android上录制音频?在手机上工作,而不是在手表上

问题描述 投票:-1回答:1

此应用程序适用于Android手机,但当它在Android Wear手表上运行时,按下录制按钮后会崩溃。有人可以为录音应用发布示例代码吗?

以下是目前显示我目前进展情况的代码。

java活动:

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>

(我只添加了以下几行,因为它不允许我在没有它们的情况下提交编辑...)

我应该尝试一种完全不同的方法,还是可以通过手表上的方式转换手机应用程序?

android wear-os android-audiorecord
1个回答
1
投票

试试这个。它使用磨损麦克风录制音频然后播放。 https://github.com/googlesamples/android-WearSpeakerSample

这是apk建造的:https://drive.google.com/open?id=1z6lwVoWh89gbGz43c_6PNPVS4Os6s4nu

我也把它放在Google Play上,带有表盘:https://play.google.com/store/apps/details?id=com.jorc.android.wearable.watchface&hl=en

© www.soinside.com 2019 - 2024. All rights reserved.