From 361ad0381b3aec314d5854d3fdd6f50d6bfa07d1 Mon Sep 17 00:00:00 2001 From: Noe Israel Miranda Franco Date: Fri, 3 Jul 2020 19:04:07 -0600 Subject: [PATCH 1/3] fix java.io.IOException: Prepare failed.: status=0x1 remove media player to play the sound --- .../example/MainActivity.java | 1 + .../AudioRecorderActivity.java | 67 +++++++++++--- .../androidaudiorecorder/PlayerManager.java | 90 +++++++++++++++++++ 3 files changed, 145 insertions(+), 13 deletions(-) create mode 100644 lib/src/main/java/cafe/adriel/androidaudiorecorder/PlayerManager.java diff --git a/app/src/main/java/cafe/adriel/androidaudiorecorder/example/MainActivity.java b/app/src/main/java/cafe/adriel/androidaudiorecorder/example/MainActivity.java index 177baa1..531de23 100644 --- a/app/src/main/java/cafe/adriel/androidaudiorecorder/example/MainActivity.java +++ b/app/src/main/java/cafe/adriel/androidaudiorecorder/example/MainActivity.java @@ -32,6 +32,7 @@ protected void onCreate(Bundle savedInstanceState) { Util.requestPermission(this, Manifest.permission.RECORD_AUDIO); Util.requestPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE); + } @Override diff --git a/lib/src/main/java/cafe/adriel/androidaudiorecorder/AudioRecorderActivity.java b/lib/src/main/java/cafe/adriel/androidaudiorecorder/AudioRecorderActivity.java index e5a7d9a..b54cbdb 100644 --- a/lib/src/main/java/cafe/adriel/androidaudiorecorder/AudioRecorderActivity.java +++ b/lib/src/main/java/cafe/adriel/androidaudiorecorder/AudioRecorderActivity.java @@ -3,10 +3,17 @@ import android.graphics.Color; import android.graphics.PorterDuff; import android.graphics.drawable.ColorDrawable; +import android.media.AudioFormat; +import android.media.AudioManager; +import android.media.AudioTrack; import android.media.MediaPlayer; +import android.net.Uri; import android.os.Bundle; +import android.os.Handler; +import android.os.Looper; import android.support.v4.content.ContextCompat; import android.support.v7.app.AppCompatActivity; +import android.util.Log; import android.view.Menu; import android.view.MenuItem; import android.view.View; @@ -19,6 +26,8 @@ import com.cleveroad.audiovisualization.GLAudioVisualizationView; import java.io.File; +import java.io.FileInputStream; +import java.io.IOException; import java.util.Timer; import java.util.TimerTask; @@ -41,7 +50,6 @@ public class AudioRecorderActivity extends AppCompatActivity private boolean autoStart; private boolean keepDisplayOn; - private MediaPlayer player; private Recorder recorder; private VisualizerHandler visualizerHandler; @@ -328,10 +336,45 @@ private void stopRecording(){ } private void startPlaying(){ - try { + + stopRecording(); + PlayerManager.getInstance().play(filePath, new PlayerManager.PlayerManagerPlayCallBack() { + @Override + public void onPlayFinished() { + stopPlaying(); + } + }); + + + + /*visualizerView.linkTo(DbmHandler.Factory.newVisualizerHandler(this, player)); + visualizerView.post(new Runnable() { + @Override + public void run() { + //player.setOnCompletionListener(AudioRecorderActivity.this); + + } + }); + * + */ + + timerView.setText("00:00:00"); + statusView.setText(R.string.aar_playing); + statusView.setVisibility(View.VISIBLE); + playView.setImageResource(R.drawable.aar_ic_stop); + + playerSecondsElapsed = 0; + startTimer(); + + /*try { stopRecording(); player = new MediaPlayer(); - player.setDataSource(filePath); + new File(filePath).setReadable(true, false); + + //Uri uri = Uri.parse("android.resource://"+getPackageName()+"/" + R.raw.my_movie); + player.setDataSource(this, Uri.parse(new File(filePath).toURI().toString())); + player.setAudioStreamType(AudioManager.STREAM_MUSIC); + //player.setDataSource(filePath); player.prepare(); player.start(); @@ -353,6 +396,8 @@ public void run() { } catch (Exception e){ e.printStackTrace(); } + + */ } private void stopPlaying(){ @@ -365,22 +410,17 @@ private void stopPlaying(){ visualizerHandler.stop(); } - if(player != null){ - try { - player.stop(); - player.reset(); - } catch (Exception e){ } - } + PlayerManager.getInstance().stop(); + stopTimer(); } private boolean isPlaying(){ - try { - return player != null && player.isPlaying() && !isRecording; - } catch (Exception e){ - return false; + if (PlayerManager.getInstance().isPlaying() && !isRecording) { + return true; } + return false; } private void startTimer(){ @@ -406,6 +446,7 @@ private void updateTimer() { runOnUiThread(new Runnable() { @Override public void run() { + Log.i("","is pala"+ isPlaying()); if(isRecording) { recorderSecondsElapsed++; timerView.setText(Util.formatSeconds(recorderSecondsElapsed)); diff --git a/lib/src/main/java/cafe/adriel/androidaudiorecorder/PlayerManager.java b/lib/src/main/java/cafe/adriel/androidaudiorecorder/PlayerManager.java new file mode 100644 index 0000000..a3e64f7 --- /dev/null +++ b/lib/src/main/java/cafe/adriel/androidaudiorecorder/PlayerManager.java @@ -0,0 +1,90 @@ +package cafe.adriel.androidaudiorecorder; + +import android.media.AudioFormat; +import android.media.AudioManager; +import android.media.AudioTrack; +import android.os.Handler; +import android.os.Looper; + +import java.io.File; +import java.io.FileInputStream; +import java.io.IOException; + +public class PlayerManager { + + interface PlayerManagerPlayCallBack{ + void onPlayFinished(); + } + + private boolean isPlaying = false; + private AudioTrack at = null; + + private static final PlayerManager ourInstance = new PlayerManager(); + + public static PlayerManager getInstance() { + return ourInstance; + } + + private PlayerManager() { + } + + public void play(final String filePath, final PlayerManagerPlayCallBack playerManagerPlayCallBack){ + new Thread(new Runnable() { + @Override + public void run() { + try { + isPlaying = true; + // just put here your wav file + File yourWavFile = new File(filePath); + FileInputStream fis = new FileInputStream(yourWavFile); + int minBufferSize = AudioTrack.getMinBufferSize(48000, + AudioFormat.CHANNEL_OUT_STEREO, AudioFormat.ENCODING_PCM_16BIT); + at = new AudioTrack(AudioManager.STREAM_MUSIC, 48000, + AudioFormat.CHANNEL_OUT_STEREO, AudioFormat.ENCODING_PCM_16BIT, + minBufferSize, AudioTrack.MODE_STREAM); + + int i = 0; + byte[] music = null; + try { + + music = new byte[512]; + at.play(); + + while ((i = fis.read(music)) != -1 && isPlaying){ + at.write(music, 0, i); + } + + + } catch (IOException e) { + e.printStackTrace(); + } + + at.stop(); + at.release(); + isPlaying = false; + new Handler(Looper.getMainLooper()).post(new Runnable() { + @Override + public void run() { + playerManagerPlayCallBack.onPlayFinished(); + } + }); + + } catch (IOException ex) { + ex.printStackTrace(); + } + } + }).start(); + } + + public void stop(){ + if (at == null){ + return; + } + isPlaying = false; + + } + + public boolean isPlaying() { + return isPlaying; + } +} From 80a4e540a1fa2111341f67322bf7b504b32b69a8 Mon Sep 17 00:00:00 2001 From: Noe Israel Miranda Franco Date: Fri, 3 Jul 2020 19:39:28 -0600 Subject: [PATCH 2/3] new version --- app/build.gradle | 2 ++ build.gradle | 1 + 2 files changed, 3 insertions(+) diff --git a/app/build.gradle b/app/build.gradle index 024db50..cad1965 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -1,4 +1,6 @@ apply plugin: 'com.android.application' +apply plugin: 'com.github.dcendents.android-maven' +group='com.github.josco007' android { compileSdkVersion 24 diff --git a/build.gradle b/build.gradle index ccb76fa..f907072 100644 --- a/build.gradle +++ b/build.gradle @@ -4,6 +4,7 @@ buildscript { } dependencies { classpath 'com.android.tools.build:gradle:2.2.0' + classpath 'com.github.dcendents:android-maven-gradle-plugin:1.4.1' } } From d78f20bec0f2e91beba0e94d93adc8b0b4a934b1 Mon Sep 17 00:00:00 2001 From: Noe Israel Miranda Franco Date: Fri, 3 Jul 2020 21:17:55 -0600 Subject: [PATCH 3/3] change playerManagerPlayCallback to public --- .../java/cafe/adriel/androidaudiorecorder/PlayerManager.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/src/main/java/cafe/adriel/androidaudiorecorder/PlayerManager.java b/lib/src/main/java/cafe/adriel/androidaudiorecorder/PlayerManager.java index a3e64f7..8d3603e 100644 --- a/lib/src/main/java/cafe/adriel/androidaudiorecorder/PlayerManager.java +++ b/lib/src/main/java/cafe/adriel/androidaudiorecorder/PlayerManager.java @@ -12,7 +12,7 @@ public class PlayerManager { - interface PlayerManagerPlayCallBack{ + public interface PlayerManagerPlayCallBack{ void onPlayFinished(); }