Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Public play callback #72

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions app/build.gradle
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
apply plugin: 'com.android.application'
apply plugin: 'com.github.dcendents.android-maven'
group='com.github.josco007'

android {
compileSdkVersion 24
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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'
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;

Expand All @@ -41,7 +50,6 @@ public class AudioRecorderActivity extends AppCompatActivity
private boolean autoStart;
private boolean keepDisplayOn;

private MediaPlayer player;
private Recorder recorder;
private VisualizerHandler visualizerHandler;

Expand Down Expand Up @@ -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();

Expand All @@ -353,6 +396,8 @@ public void run() {
} catch (Exception e){
e.printStackTrace();
}

*/
}

private void stopPlaying(){
Expand All @@ -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(){
Expand All @@ -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));
Expand Down
Original file line number Diff line number Diff line change
@@ -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 {

public 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;
}
}