|
@@ -15,7 +15,10 @@ import android.media.AudioFormat;
|
|
|
import android.media.AudioManager;
|
|
import android.media.AudioManager;
|
|
|
import android.media.AudioRecord;
|
|
import android.media.AudioRecord;
|
|
|
import android.media.AudioTrack;
|
|
import android.media.AudioTrack;
|
|
|
|
|
+import android.media.MediaMetadataRetriever;
|
|
|
|
|
+import android.media.MediaPlayer;
|
|
|
import android.media.MediaRecorder;
|
|
import android.media.MediaRecorder;
|
|
|
|
|
+import android.net.Uri;
|
|
|
import android.os.Build;
|
|
import android.os.Build;
|
|
|
import android.os.Bundle;
|
|
import android.os.Bundle;
|
|
|
import android.os.Handler;
|
|
import android.os.Handler;
|
|
@@ -85,75 +88,51 @@ public class MainActivity extends ReactActivity {
|
|
|
super.onCreate(null);
|
|
super.onCreate(null);
|
|
|
checkNotification(getIntent());
|
|
checkNotification(getIntent());
|
|
|
// demo2();
|
|
// demo2();
|
|
|
- demo();
|
|
|
|
|
|
|
+// try {
|
|
|
|
|
+// demo();
|
|
|
|
|
+// } catch (IOException e) {
|
|
|
|
|
+// throw new RuntimeException(e);
|
|
|
|
|
+// }
|
|
|
// getLastKnownLocation();
|
|
// getLastKnownLocation();
|
|
|
// String action = getIntent().getAction();
|
|
// String action = getIntent().getAction();
|
|
|
// System.out.println("notification receive action action:"+action);
|
|
// System.out.println("notification receive action action:"+action);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
- private void demo() {
|
|
|
|
|
|
|
+ private void demo() throws IOException {
|
|
|
|
|
+ Uri audioUri1 = Uri.parse(Uri.parse("android.resource://")+getPackageName()+"/"+R.raw.sound1);
|
|
|
|
|
+ Uri audioUri2 = Uri.parse(Uri.parse("android.resource://")+getPackageName()+"/"+R.raw.sound2);
|
|
|
|
|
|
|
|
- }
|
|
|
|
|
|
|
+ // 创建MediaPlayer对象来读取音频文件
|
|
|
|
|
+ MediaPlayer mediaPlayer1 = new MediaPlayer();
|
|
|
|
|
+ mediaPlayer1.setDataSource(getApplicationContext(), audioUri1);
|
|
|
|
|
+ mediaPlayer1.prepare();
|
|
|
|
|
|
|
|
- public void mixAudio(File inputFile1, File inputFile2, File outputFile) throws IOException {
|
|
|
|
|
- int SAMPLE_RATE = 44100;
|
|
|
|
|
- int BUFFER_SIZE = 1024;
|
|
|
|
|
-
|
|
|
|
|
- int bufferSize = AudioRecord.getMinBufferSize(SAMPLE_RATE,
|
|
|
|
|
- AudioFormat.CHANNEL_IN_STEREO,
|
|
|
|
|
- AudioFormat.ENCODING_PCM_16BIT);
|
|
|
|
|
- if (ActivityCompat.checkSelfPermission(this, android.Manifest.permission.RECORD_AUDIO) != PackageManager.PERMISSION_GRANTED) {
|
|
|
|
|
- // TODO: Consider calling
|
|
|
|
|
- // ActivityCompat#requestPermissions
|
|
|
|
|
- // here to request the missing permissions, and then overriding
|
|
|
|
|
- // public void onRequestPermissionsResult(int requestCode, String[] permissions,
|
|
|
|
|
- // int[] grantResults)
|
|
|
|
|
- // to handle the case where the user grants the permission. See the documentation
|
|
|
|
|
- // for ActivityCompat#requestPermissions for more details.
|
|
|
|
|
- return;
|
|
|
|
|
- }
|
|
|
|
|
- AudioRecord audioRecord = new AudioRecord(MediaRecorder.AudioSource.MIC,
|
|
|
|
|
- SAMPLE_RATE,
|
|
|
|
|
- AudioFormat.CHANNEL_IN_STEREO,
|
|
|
|
|
- AudioFormat.ENCODING_PCM_16BIT,
|
|
|
|
|
- bufferSize);
|
|
|
|
|
-
|
|
|
|
|
- AudioTrack audioTrack = new AudioTrack(AudioManager.STREAM_MUSIC,
|
|
|
|
|
- SAMPLE_RATE,
|
|
|
|
|
- AudioFormat.CHANNEL_OUT_STEREO,
|
|
|
|
|
- AudioFormat.ENCODING_PCM_16BIT,
|
|
|
|
|
- bufferSize,
|
|
|
|
|
- AudioTrack.MODE_STREAM);
|
|
|
|
|
-
|
|
|
|
|
- short[] buffer = new short[bufferSize];
|
|
|
|
|
- FileOutputStream fos = new FileOutputStream(outputFile);
|
|
|
|
|
-
|
|
|
|
|
- audioRecord.startRecording();
|
|
|
|
|
- audioTrack.play();
|
|
|
|
|
-
|
|
|
|
|
- while (/* condition to keep mixing */) {
|
|
|
|
|
- // Read audio from microphone
|
|
|
|
|
- int bufferReadResult = audioRecord.read(buffer, 0, bufferSize);
|
|
|
|
|
- // Process audio (if needed)
|
|
|
|
|
- // ...
|
|
|
|
|
-
|
|
|
|
|
- // Write to output file
|
|
|
|
|
- fos.write(buffer, 0, bufferReadResult);
|
|
|
|
|
-
|
|
|
|
|
- // Optionally, read from another AudioRecord or file and mix with buffer
|
|
|
|
|
- // ...
|
|
|
|
|
-
|
|
|
|
|
- // Pass to AudioTrack
|
|
|
|
|
- audioTrack.write(buffer, 0, bufferReadResult);
|
|
|
|
|
- }
|
|
|
|
|
|
|
+ MediaPlayer mediaPlayer2 = new MediaPlayer();
|
|
|
|
|
+ mediaPlayer2.setDataSource(getApplicationContext(), audioUri2);
|
|
|
|
|
+ mediaPlayer2.prepare();
|
|
|
|
|
+
|
|
|
|
|
+ MediaMetadataRetriever retriever1 = new MediaMetadataRetriever();
|
|
|
|
|
+ retriever1.setDataSource(getApplicationContext(), audioUri1);
|
|
|
|
|
+ long duration1 = Long.parseLong(retriever1.extractMetadata(MediaMetadataRetriever.METADATA_KEY_DURATION));
|
|
|
|
|
|
|
|
- audioRecord.stop();
|
|
|
|
|
- audioTrack.stop();
|
|
|
|
|
- fos.close();
|
|
|
|
|
|
|
+ MediaMetadataRetriever retriever2 = new MediaMetadataRetriever();
|
|
|
|
|
+ retriever2.setDataSource(getApplicationContext(), audioUri2);
|
|
|
|
|
+ long duration2 = Long.parseLong(retriever2.extractMetadata(MediaMetadataRetriever.METADATA_KEY_DURATION));
|
|
|
|
|
+
|
|
|
|
|
+ // 创建MediaPlayer对象来合成音频
|
|
|
|
|
+ MediaPlayer compositeMediaPlayer = new MediaPlayer();
|
|
|
|
|
+ compositeMediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
|
|
|
|
|
+
|
|
|
|
|
+// // 创建 MediaComposition 对象
|
|
|
|
|
+// MediaComposition composition = new MediaComposition();
|
|
|
|
|
+//
|
|
|
|
|
+//// 创建音频轨道
|
|
|
|
|
+// MediaCompositionTrack audioTrack = composition.addAudioTrack();
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
+
|
|
|
public static void deleteIfExists(String filePath) {
|
|
public static void deleteIfExists(String filePath) {
|
|
|
File file = new File(filePath);
|
|
File file = new File(filePath);
|
|
|
|
|
|