下面的这两段代码位于我的 Android Studio 项目中的同一个 MainActivity 中
1)从智能手机录制音频并在按下播放按钮时播放:
public class Main2Activity extends AppCompatActivity {
//Declare variables
Button btnRecord, btnStopRecord, btnPlay, btnStop;
//String pathSave ="";
private static String pathSave;
MediaRecorder mediaRecorder;
MediaPlayer mediaPlayer;
final int REQUEST_PERMISSION_CODE=1000;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
//Request Runtime permission
if(!checkPermissionFromDevice()){
requestPermission();
}
//Init View
btnPlay = (Button) findViewById(R.id.btnPlay);
btnRecord = (Button) findViewById(R.id.btnStartRecord);
btnStop = (Button) findViewById(R.id.btnStop);
btnStopRecord = (Button) findViewById(R.id.btnStopRecord);
btnRecord.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick (View view){
if (checkPermissionFromDevice())
{
pathSave = Environment.getExternalStorageDirectory().getAbsolutePath()+"/"+ UUID.randomUUID().toString()+"audio_record.3gp";
setupMediaRecorder();
try{
mediaRecorder.prepare();
mediaRecorder.start();
} catch (IOException e){
e.printStackTrace();
}
btnPlay.setEnabled(false);
btnStop.setEnabled(false);
Toast.makeText(Main2Activity.this, "Recording...", Toast.LENGTH_SHORT).show();
}
else{
requestPermission();
}
}
});
btnStopRecord.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
mediaRecorder.stop();
btnStopRecord.setEnabled(false);
btnPlay.setEnabled(true);
btnRecord.setEnabled(true);
btnStop.setEnabled(false);
}
});
btnPlay.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
btnStop.setEnabled(true);
btnStopRecord.setEnabled(false);
btnRecord.setEnabled(false);
mediaPlayer = new MediaPlayer();
try{
mediaPlayer.setDataSource(pathSave);
mediaPlayer.prepare();
} catch (IOException e){
e.printStackTrace();
}
mediaPlayer.start();
Toast.makeText(Main2Activity.this, "Playing...", Toast.LENGTH_SHORT).show();
}
});
btnStop.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
btnStopRecord.setEnabled(false);
btnRecord.setEnabled(true);
btnStop.setEnabled(false);
btnPlay.setEnabled(true);
if (mediaPlayer != null){
mediaPlayer.stop();
mediaPlayer.release();
setupMediaRecorder();
}
}
});
}
private void setupMediaRecorder(){
mediaRecorder = new MediaRecorder();
mediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
mediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
mediaRecorder.setAudioEncoder(MediaRecorder.OutputFormat.AMR_NB);
mediaRecorder.setOutputFile(pathSave);
}
private void requestPermission() {
ActivityCompat.requestPermissions(this, new String[]{
Manifest.permission.WRITE_EXTERNAL_STORAGE,
Manifest.permission.RECORD_AUDIO
} ,REQUEST_PERMISSION_CODE);
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
switch (requestCode)
{
case REQUEST_PERMISSION_CODE:
{
if(grantResults.length>0 && grantResults[0]== PackageManager.PERMISSION_GRANTED) {
Toast.makeText(this, "Permission Granted", Toast.LENGTH_SHORT).show();
}
else {
Toast.makeText( this, "Permission Denied", Toast.LENGTH_SHORT).show();
}
}
break;
}
}
private boolean checkPermissionFromDevice(){
int write_external_storage_result = ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE);
int record_audio_result = ContextCompat.checkSelfPermission(this, Manifest.permission.RECORD_AUDIO);
return write_external_storage_result == PackageManager.PERMISSION_GRANTED &&
record_audio_result == PackageManager.PERMISSION_GRANTED;
}
2)保存创建音频文件的目录并将其转换为 vector :
String path = Environment.getExternalStorageDirectory().getAbsolutePath()+"/"+UUID.randomUUID().toString()+"audio_record.3gp";
public byte[] convert(String path) throws IOException {
FileInputStream fis = new FileInputStream(path);
ByteArrayOutputStream bos = new ByteArrayOutputStream();
byte[] b = new byte[1024];
for (int readNum; (readNum = fis.read(b)) != -1; ) {
bos.write(b, 0, readNum);
}
byte[] bytes = bos.toByteArray();
return bytes;
}
问题
构建输出:
构建:构建失败
AAPT 错误:(1 个错误)
Android资源链接失败
错误消息示例: (1) AAPT: C:\Users\cmorais.gradle\caches\transforms-2\files-2.1\cb634fe4b4d1fdfcd5255e485d30a0c1\material- 1.0.0\res\anim-v21\design_bottom_sheet_slide_in.xml:17:错误:找不到资源整数/bottom_sheet_slide_duration(又名 com.example.appsom:integer/bottom_sheet_slide_duration)。 (2) C:\Users\cmorais.gradle\caches\transforms-2\files-2.1\cb634fe4b4d1fdfcd5255e485d30a0c1\material-1.0.0\res\anim-v21\design_bottom_sheet_slide_out.xml:17:错误:找不到资源integer/bottom_sheet_slide_duration(又名com.example.appsom:integer/bottom_sheet_slide_duration)。 – moraiscarolinav 2天前
(3) C:\Users\cmorais.gradle\caches\transforms-2\files-2.1\cb634fe4b4d1fdfcd5255e485d30a0c1\material-1.0.0\res\animator-v21\design_appbar_state_list_animator.xml:19:错误:未找到属性 state_liftable(又名 com.example.appsom:state_liftable)。 (4) C:\Users\cmorais.gradle\caches\transforms-2\files-2.1\cb634fe4b4d1fdfcd5255e485d30a0c1\material-1.0.0\res\animator-v21\design_appbar_state_list_animator.xml:19:错误:未找到属性 state_lifted(又名 com.example.appsom:state_lifted)。 ... 该项目有 (20) 错误消息,末尾带有 “未找到”。
3) 我的 MainActivity XML 文件(文本):
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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:background="@color/maxwellBackground"
android:orientation="vertical"
tools:context=".MainActivity">
<Button
android:id="@+id/btnStartRecord"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="60dp"
android:background="#FC9B03"
android:text="@string/button_start"
android:textColor="@android:color/background_light"
android:textStyle="bold"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textView2" />
<Button
android:id="@+id/btnStopRecord"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="55dp"
android:background="#FC9B03"
android:text="@string/button_stop"
android:textColor="@android:color/background_light"
android:textStyle="bold"
app:layout_constraintBottom_toTopOf="@+id/textView3"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/btnStartRecord"
app:layout_constraintVertical_bias="0.161" />
<Button
android:id="@+id/btnPlay"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@color/ColorYellow"
android:text="@string/button_play"
android:textColor="@android:color/background_light"
android:textStyle="bold"
app:layout_constraintBottom_toTopOf="@+id/btnStop"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.507"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textView3"
app:layout_constraintVertical_bias="0.504" />
<Button
android:id="@+id/btnStop"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="128dp"
android:background="@color/ColorYellow"
android:text="@string/button_stop_play"
android:textColor="@android:color/background_light"
android:textStyle="bold"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.507"
app:layout_constraintStart_toStartOf="parent" />
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/record_area"
android:textColor="@android:color/background_light"
android:textSize="24sp"
android:textStyle="bold"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.088" />
<TextView
android:id="@+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/play_area"
android:textColor="@android:color/background_light"
android:textSize="24sp"
android:textStyle="bold"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.498"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.573" />
</androidx.constraintlayout.widget.ConstraintLayout>
关联按钮(XML 设计)
最佳答案
这不是名为 com.example.appsom:integer/bottom_sheet_slide_duration 的文件或资源。 在项目的\res\values 文件夹中创建 integers.xml 文件。在文件中创建您的整数:
<resources>
<integer name="bottom_sheet_slide_duration">value</integer></resources>
关于java - 如何解决这个错误: Android resource linking failed?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59683290/