Java 录制的文件名错误
首先,我将解释应用程序的预期行为 这个应用程序做什么? 当我运行应用程序时,我看到以下视图: 现在,如果点击“+”按钮,您将看到以下对话框: 单击添加,此条目将添加到列表视图中。我增加了两项。这看起来像这样: 预期结果 现在,当我触摸并按住列表项的麦克风图标时,它应该开始通过设备麦克风录制音频,然后用粗体条目的名称保存此录制,这意味着,例如,第一次录制将获得名称A.3gp,第二次录制将获得名称B.3gp,依此类推 实际结果(2017年12月7日更新) 目前的状态是:Java 录制的文件名错误,java,android,xml,Java,Android,Xml,首先,我将解释应用程序的预期行为 这个应用程序做什么? 当我运行应用程序时,我看到以下视图: 现在,如果点击“+”按钮,您将看到以下对话框: 单击添加,此条目将添加到列表视图中。我增加了两项。这看起来像这样: 预期结果 现在,当我触摸并按住列表项的麦克风图标时,它应该开始通过设备麦克风录制音频,然后用粗体条目的名称保存此录制,这意味着,例如,第一次录制将获得名称A.3gp,第二次录制将获得名称B.3gp,依此类推 实际结果(2017年12月7日更新) 目前的状态是: 列表中的每一项都有自
- 列表中的每一项都有自己的记录。这意味着,如果我按住列表中第一项的麦克风图标,它正在做它应该做的事情。清单上的所有其他项目也是如此
- 当我添加第一个项目A,然后触摸其记录图标时,将创建一个名为A.3gp的文件(这是正确的行为)
- 当我添加第二个项目B然后什么也不做,然后添加第三个项目C并触摸B的记录图标时,将创建一个名为C.3gp的文件(这是不是一个正确的行为,应该是B.3gp)
activity\u add\u new\u words.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.yousef.mustafa.speakmylanguage.View.MyWordsActivity">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:title="@string/app_name"
app:titleTextColor="@color/colorWhite" />
<ListView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/myWordsList"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@+id/toolbar"
android:drawSelectorOnTop="true"
android:orientation="vertical"
tools:context=".View.MyWordsActivity" />
<android.support.design.widget.FloatingActionButton
android:id="@+id/fabAddNewWord"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_gravity="bottom|center"
android:layout_marginBottom="32dp"
android:tint="@color/colorWhite"
app:backgroundTint="@color/colorGrey"
app:srcCompat="@drawable/icon_add" />
<TextView
android:id="@+id/hintTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="155dp"
android:gravity="center"
android:text="@string/hint"
android:textSize="24sp"
android:textStyle="bold" />
</RelativeLayout>
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/colorDialogBackground">
<LinearLayout
android:id="@+id/addNewWordLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginBottom="8dp"
android:layout_marginTop="12dp"
android:text="@string/add_word_title"
android:textColor="@color/colorBlack"
android:textSize="24sp"
android:textStyle="bold" />
<EditText
android:id="@+id/addForeignWordEditText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginBottom="4dp"
android:layout_marginEnd="12dp"
android:layout_marginStart="12dp"
android:layout_weight="5"
android:gravity="center_horizontal"
android:hint="@string/enter_foreign_word"
android:inputType="textPersonName|textCapWords"
android:textSize="24sp" />
<EditText
android:id="@+id/addDefaultWordEditText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginEnd="12dp"
android:layout_marginStart="12dp"
android:layout_marginTop="4dp"
android:layout_weight="5"
android:gravity="center_horizontal"
android:hint="@string/enter_default_word"
android:inputType="textPersonName|textCapWords"
android:textSize="24sp" />
<Button
android:id="@+id/addNewWordButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="16dp"
android:layout_marginEnd="16dp"
android:layout_marginStart="16dp"
android:layout_marginTop="16dp"
android:text="@string/button_add"
android:textAllCaps="false"
android:textSize="18sp" />
</LinearLayout>
</RelativeLayout>
我将感谢任何帮助。提前感谢。在活动的
startAudioRecording()
方法中,您可以从foreignWord
字段设置输出文件名。问题在于,每当用户在“添加单词”对话框中提交单词对时,外来词
字段被设置为最近添加的外来词。因此,每次用户开始录制时,输出文件的名称都将设置为添加的最后一个外来词
与使用录制图像视图
、外来词
、默认词
、外来词
、音频保存路径
和媒体录制器
的字段不同,您应该将这些字段设置为本地变量,并将它们作为参数传递给录制()、开始录制()
,stopAudioRecording()
方法,包括以下步骤:
只需从类的开头删除这些字段,就可以在使用这些字段的地方看到一些编译错误
如果在赋值的左侧看到未定义的变量,则将该变量设为本地变量(Android Studio中的Ctrl+V)
如果在别处看到未定义的变量,请将该变量作为方法的参数(Ctrl+p)
在这些变化之后,它应该会起作用
`请移动此片段:
recordingImageView.setOnTouchListener(new View.OnTouchListener()
{
@Override public boolean onTouch(View view, MotionEvent motionEvent)
{
switch (motionEvent.getAction())
{
case MotionEvent.ACTION_DOWN:
{ recordingImageView.setImageResource(g startAudioRecording(position);
break;
}
case MotionEvent.ACTION_UP:
{
recordingImageView.setImageResource(R.drawable.mic_black);
stopAudioRecording();
break;
}
}
return true;
}
});
private void startAudioRecording()
{
if (checkPermission())
{
if (wordsArrayList != null)
{
audioSavePath = Environment.getExternalStorageDirectory().getAbsolutePath() + "/" + wordsArrayList.get(position) + ".3gp";
getMediaRecorderReady();
}
}
}
在适配器中,访问recordImageView下面的getView(…)。
并将startAudioRecording()、stopAudioRecording()放在同一适配器中,或将所有相关的录音方法放在适配器中
因此,您将在每个单独的图像视图中单击。以前,当您获取时,它将只获取任何一个图像视图的实例
现在将“startAudioRecording()”上的代码更新为:
recordingImageView.setOnTouchListener(new View.OnTouchListener()
{
@Override public boolean onTouch(View view, MotionEvent motionEvent)
{
switch (motionEvent.getAction())
{
case MotionEvent.ACTION_DOWN:
{ recordingImageView.setImageResource(g startAudioRecording(position);
break;
}
case MotionEvent.ACTION_UP:
{
recordingImageView.setImageResource(R.drawable.mic_black);
stopAudioRecording();
break;
}
}
return true;
}
});
private void startAudioRecording()
{
if (checkPermission())
{
if (wordsArrayList != null)
{
audioSavePath = Environment.getExternalStorageDirectory().getAbsolutePath() + "/" + wordsArrayList.get(position) + ".3gp";
getMediaRecorderReady();
}
}
}
现在您的第二点是,以任意名称保存其名称。将由上述代码覆盖。
谢谢并祝您编码愉快如果我的代码为您所用,您应该在adapterhey@mustafa内处理recordingImageView.setOnTouchListener。请接受它,它帮了我很多,谢谢你。我还是有些崩溃。必须首先找到问题并解决它们。@ankitpatidar您是否有机会看看我的更改?谢谢:)是的,我会的,但是有什么问题吗?你能看看我的变化吗?谢谢:)
recordingImageView.setOnTouchListener(new View.OnTouchListener()
{
@Override public boolean onTouch(View view, MotionEvent motionEvent)
{
switch (motionEvent.getAction())
{
case MotionEvent.ACTION_DOWN:
{ recordingImageView.setImageResource(g startAudioRecording(position);
break;
}
case MotionEvent.ACTION_UP:
{
recordingImageView.setImageResource(R.drawable.mic_black);
stopAudioRecording();
break;
}
}
return true;
}
});
private void startAudioRecording()
{
if (checkPermission())
{
if (wordsArrayList != null)
{
audioSavePath = Environment.getExternalStorageDirectory().getAbsolutePath() + "/" + wordsArrayList.get(position) + ".3gp";
getMediaRecorderReady();
}
}
}