Show waveform of audio











up vote
17
down vote

favorite
5












I am making one music application in android.In this music list coming from server side. I don'tknow how to show waveform of audio in android ? like in soundcloud website. I have attached image below.enter image description here










share|improve this question




















  • 1




    hello Have you got any solution with this i am also stuck on same problem in recording app. please help if you got anything about it.
    – Android Geek
    Aug 4 '16 at 6:10















up vote
17
down vote

favorite
5












I am making one music application in android.In this music list coming from server side. I don'tknow how to show waveform of audio in android ? like in soundcloud website. I have attached image below.enter image description here










share|improve this question




















  • 1




    hello Have you got any solution with this i am also stuck on same problem in recording app. please help if you got anything about it.
    – Android Geek
    Aug 4 '16 at 6:10













up vote
17
down vote

favorite
5









up vote
17
down vote

favorite
5






5





I am making one music application in android.In this music list coming from server side. I don'tknow how to show waveform of audio in android ? like in soundcloud website. I have attached image below.enter image description here










share|improve this question















I am making one music application in android.In this music list coming from server side. I don'tknow how to show waveform of audio in android ? like in soundcloud website. I have attached image below.enter image description here







android audio waveform






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Aug 3 '16 at 13:17

























asked Aug 3 '16 at 13:07









Alpha

157110




157110








  • 1




    hello Have you got any solution with this i am also stuck on same problem in recording app. please help if you got anything about it.
    – Android Geek
    Aug 4 '16 at 6:10














  • 1




    hello Have you got any solution with this i am also stuck on same problem in recording app. please help if you got anything about it.
    – Android Geek
    Aug 4 '16 at 6:10








1




1




hello Have you got any solution with this i am also stuck on same problem in recording app. please help if you got anything about it.
– Android Geek
Aug 4 '16 at 6:10




hello Have you got any solution with this i am also stuck on same problem in recording app. please help if you got anything about it.
– Android Geek
Aug 4 '16 at 6:10












1 Answer
1






active

oldest

votes

















up vote
14
down vote



+50










Perhaps, you can implements this feature without libraries, of course if you want only visualisation of audio sample.
For example:



public class PlayerVisualizerView extends View {

/**
* constant value for Height of the bar
*/
public static final int VISUALIZER_HEIGHT = 28;

/**
* bytes array converted from file.
*/
private byte bytes;

/**
* Percentage of audio sample scale
* Should updated dynamically while audioPlayer is played
*/
private float denseness;

/**
* Canvas painting for sample scale, filling played part of audio sample
*/
private Paint playedStatePainting = new Paint();
/**
* Canvas painting for sample scale, filling not played part of audio sample
*/
private Paint notPlayedStatePainting = new Paint();

private int width;
private int height;

public PlayerVisualizerView(Context context) {
super(context);
init();
}

public PlayerVisualizerView(Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
init();
}

private void init() {
bytes = null;

playedStatePainting.setStrokeWidth(1f);
playedStatePainting.setAntiAlias(true);
playedStatePainting.setColor(ContextCompat.getColor(getContext(), R.color.gray));
notPlayedStatePainting.setStrokeWidth(1f);
notPlayedStatePainting.setAntiAlias(true);
notPlayedStatePainting.setColor(ContextCompat.getColor(getContext(), R.color.colorAccent));
}

/**
* update and redraw Visualizer view
*/
public void updateVisualizer(byte bytes) {
this.bytes = bytes;
invalidate();
}

/**
* Update player percent. 0 - file not played, 1 - full played
*
* @param percent
*/
public void updatePlayerPercent(float percent) {
denseness = (int) Math.ceil(width * percent);
if (denseness < 0) {
denseness = 0;
} else if (denseness > width) {
denseness = width;
}
invalidate();
}

@Override
protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
super.onLayout(changed, left, top, right, bottom);
width = getMeasuredWidth();
height = getMeasuredHeight();
}

@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
if (bytes == null || width == 0) {
return;
}
float totalBarsCount = width / dp(3);
if (totalBarsCount <= 0.1f) {
return;
}
byte value;
int samplesCount = (bytes.length * 8 / 5);
float samplesPerBar = samplesCount / totalBarsCount;
float barCounter = 0;
int nextBarNum = 0;

int y = (height - dp(VISUALIZER_HEIGHT)) / 2;
int barNum = 0;
int lastBarNum;
int drawBarCount;

for (int a = 0; a < samplesCount; a++) {
if (a != nextBarNum) {
continue;
}
drawBarCount = 0;
lastBarNum = nextBarNum;
while (lastBarNum == nextBarNum) {
barCounter += samplesPerBar;
nextBarNum = (int) barCounter;
drawBarCount++;
}

int bitPointer = a * 5;
int byteNum = bitPointer / Byte.SIZE;
int byteBitOffset = bitPointer - byteNum * Byte.SIZE;
int currentByteCount = Byte.SIZE - byteBitOffset;
int nextByteRest = 5 - currentByteCount;
value = (byte) ((bytes[byteNum] >> byteBitOffset) & ((2 << (Math.min(5, currentByteCount) - 1)) - 1));
if (nextByteRest > 0) {
value <<= nextByteRest;
value |= bytes[byteNum + 1] & ((2 << (nextByteRest - 1)) - 1);
}

for (int b = 0; b < drawBarCount; b++) {
int x = barNum * dp(3);
float left = x;
float top = y + dp(VISUALIZER_HEIGHT - Math.max(1, VISUALIZER_HEIGHT * value / 31.0f));
float right = x + dp(2);
float bottom = y + dp(VISUALIZER_HEIGHT);
if (x < denseness && x + dp(2) < denseness) {
canvas.drawRect(left, top, right, bottom, notPlayedStatePainting);
} else {
canvas.drawRect(left, top, right, bottom, playedStatePainting);
if (x < denseness) {
canvas.drawRect(left, top, right, bottom, notPlayedStatePainting);
}
}
barNum++;
}
}
}

public int dp(float value) {
if (value == 0) {
return 0;
}
return (int) Math.ceil(getContext().getResources().getDisplayMetrics().density * value);
}
}


Sorry, code with a small amount of comments, but it is working visualizer. You can attach it to any players you want.



How you can use it: add this view in your xml layout, then you have to update visualizer state with methods



public void updateVisualizer(byte bytes) {
playerVisualizerView.updateVisualizer(bytes);
}

public void updatePlayerProgress(float percent) {
playerVisualizerView.updatePlayerPercent(percent);
}


In updateVisualizer you pass bytes array with you audio sample, and in updatePlayerProgress you dynamically pass percentage, while audio sample is playing.



for converting file to bytes you can use this helper method



public static byte fileToBytes(File file) {
int size = (int) file.length();
byte bytes = new byte[size];
try {
BufferedInputStream buf = new BufferedInputStream(new FileInputStream(file));
buf.read(bytes, 0, bytes.length);
buf.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return bytes;
}


and for example(very shortly), how it looks like with Mosby library:



public class AudioRecorderPresenter extends MvpBasePresenter<AudioRecorderView> {

public void onStopRecord() {
// stopped and released MediaPlayer
// ...
// some preparation and saved audio file in audioFileName variable.

getView().updateVisualizer(FileUtils.fileToBytes(new File(audioFileName)));
}
}
}





share|improve this answer



















  • 1




    Tanx, How can I use PlayerVisualizerView?
    – Hamed Gh
    Oct 21 '17 at 7:34






  • 1




    I think you give the best answer and give +50 award to your answer, but please add more detail and show the usage.
    – Hamed Gh
    Oct 21 '17 at 10:18










  • @HamedGh updated. Is it enough?
    – Scrobot
    Oct 22 '17 at 11:08










  • Would be nice if you comment the code over each line and explain it, specially the bytes movement
    – Diego Fernando Murillo Valenci
    Sep 27 at 16:19






  • 1




    @KathanShah hi! I'm working on it )) I took a lot of feedbacks, and decide to create library for this feature) So, the android library will be published soon. )) In particular, negative waves will be 'in box' too ))
    – Scrobot
    Oct 31 at 10:10











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f38744579%2fshow-waveform-of-audio%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
14
down vote



+50










Perhaps, you can implements this feature without libraries, of course if you want only visualisation of audio sample.
For example:



public class PlayerVisualizerView extends View {

/**
* constant value for Height of the bar
*/
public static final int VISUALIZER_HEIGHT = 28;

/**
* bytes array converted from file.
*/
private byte bytes;

/**
* Percentage of audio sample scale
* Should updated dynamically while audioPlayer is played
*/
private float denseness;

/**
* Canvas painting for sample scale, filling played part of audio sample
*/
private Paint playedStatePainting = new Paint();
/**
* Canvas painting for sample scale, filling not played part of audio sample
*/
private Paint notPlayedStatePainting = new Paint();

private int width;
private int height;

public PlayerVisualizerView(Context context) {
super(context);
init();
}

public PlayerVisualizerView(Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
init();
}

private void init() {
bytes = null;

playedStatePainting.setStrokeWidth(1f);
playedStatePainting.setAntiAlias(true);
playedStatePainting.setColor(ContextCompat.getColor(getContext(), R.color.gray));
notPlayedStatePainting.setStrokeWidth(1f);
notPlayedStatePainting.setAntiAlias(true);
notPlayedStatePainting.setColor(ContextCompat.getColor(getContext(), R.color.colorAccent));
}

/**
* update and redraw Visualizer view
*/
public void updateVisualizer(byte bytes) {
this.bytes = bytes;
invalidate();
}

/**
* Update player percent. 0 - file not played, 1 - full played
*
* @param percent
*/
public void updatePlayerPercent(float percent) {
denseness = (int) Math.ceil(width * percent);
if (denseness < 0) {
denseness = 0;
} else if (denseness > width) {
denseness = width;
}
invalidate();
}

@Override
protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
super.onLayout(changed, left, top, right, bottom);
width = getMeasuredWidth();
height = getMeasuredHeight();
}

@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
if (bytes == null || width == 0) {
return;
}
float totalBarsCount = width / dp(3);
if (totalBarsCount <= 0.1f) {
return;
}
byte value;
int samplesCount = (bytes.length * 8 / 5);
float samplesPerBar = samplesCount / totalBarsCount;
float barCounter = 0;
int nextBarNum = 0;

int y = (height - dp(VISUALIZER_HEIGHT)) / 2;
int barNum = 0;
int lastBarNum;
int drawBarCount;

for (int a = 0; a < samplesCount; a++) {
if (a != nextBarNum) {
continue;
}
drawBarCount = 0;
lastBarNum = nextBarNum;
while (lastBarNum == nextBarNum) {
barCounter += samplesPerBar;
nextBarNum = (int) barCounter;
drawBarCount++;
}

int bitPointer = a * 5;
int byteNum = bitPointer / Byte.SIZE;
int byteBitOffset = bitPointer - byteNum * Byte.SIZE;
int currentByteCount = Byte.SIZE - byteBitOffset;
int nextByteRest = 5 - currentByteCount;
value = (byte) ((bytes[byteNum] >> byteBitOffset) & ((2 << (Math.min(5, currentByteCount) - 1)) - 1));
if (nextByteRest > 0) {
value <<= nextByteRest;
value |= bytes[byteNum + 1] & ((2 << (nextByteRest - 1)) - 1);
}

for (int b = 0; b < drawBarCount; b++) {
int x = barNum * dp(3);
float left = x;
float top = y + dp(VISUALIZER_HEIGHT - Math.max(1, VISUALIZER_HEIGHT * value / 31.0f));
float right = x + dp(2);
float bottom = y + dp(VISUALIZER_HEIGHT);
if (x < denseness && x + dp(2) < denseness) {
canvas.drawRect(left, top, right, bottom, notPlayedStatePainting);
} else {
canvas.drawRect(left, top, right, bottom, playedStatePainting);
if (x < denseness) {
canvas.drawRect(left, top, right, bottom, notPlayedStatePainting);
}
}
barNum++;
}
}
}

public int dp(float value) {
if (value == 0) {
return 0;
}
return (int) Math.ceil(getContext().getResources().getDisplayMetrics().density * value);
}
}


Sorry, code with a small amount of comments, but it is working visualizer. You can attach it to any players you want.



How you can use it: add this view in your xml layout, then you have to update visualizer state with methods



public void updateVisualizer(byte bytes) {
playerVisualizerView.updateVisualizer(bytes);
}

public void updatePlayerProgress(float percent) {
playerVisualizerView.updatePlayerPercent(percent);
}


In updateVisualizer you pass bytes array with you audio sample, and in updatePlayerProgress you dynamically pass percentage, while audio sample is playing.



for converting file to bytes you can use this helper method



public static byte fileToBytes(File file) {
int size = (int) file.length();
byte bytes = new byte[size];
try {
BufferedInputStream buf = new BufferedInputStream(new FileInputStream(file));
buf.read(bytes, 0, bytes.length);
buf.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return bytes;
}


and for example(very shortly), how it looks like with Mosby library:



public class AudioRecorderPresenter extends MvpBasePresenter<AudioRecorderView> {

public void onStopRecord() {
// stopped and released MediaPlayer
// ...
// some preparation and saved audio file in audioFileName variable.

getView().updateVisualizer(FileUtils.fileToBytes(new File(audioFileName)));
}
}
}





share|improve this answer



















  • 1




    Tanx, How can I use PlayerVisualizerView?
    – Hamed Gh
    Oct 21 '17 at 7:34






  • 1




    I think you give the best answer and give +50 award to your answer, but please add more detail and show the usage.
    – Hamed Gh
    Oct 21 '17 at 10:18










  • @HamedGh updated. Is it enough?
    – Scrobot
    Oct 22 '17 at 11:08










  • Would be nice if you comment the code over each line and explain it, specially the bytes movement
    – Diego Fernando Murillo Valenci
    Sep 27 at 16:19






  • 1




    @KathanShah hi! I'm working on it )) I took a lot of feedbacks, and decide to create library for this feature) So, the android library will be published soon. )) In particular, negative waves will be 'in box' too ))
    – Scrobot
    Oct 31 at 10:10















up vote
14
down vote



+50










Perhaps, you can implements this feature without libraries, of course if you want only visualisation of audio sample.
For example:



public class PlayerVisualizerView extends View {

/**
* constant value for Height of the bar
*/
public static final int VISUALIZER_HEIGHT = 28;

/**
* bytes array converted from file.
*/
private byte bytes;

/**
* Percentage of audio sample scale
* Should updated dynamically while audioPlayer is played
*/
private float denseness;

/**
* Canvas painting for sample scale, filling played part of audio sample
*/
private Paint playedStatePainting = new Paint();
/**
* Canvas painting for sample scale, filling not played part of audio sample
*/
private Paint notPlayedStatePainting = new Paint();

private int width;
private int height;

public PlayerVisualizerView(Context context) {
super(context);
init();
}

public PlayerVisualizerView(Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
init();
}

private void init() {
bytes = null;

playedStatePainting.setStrokeWidth(1f);
playedStatePainting.setAntiAlias(true);
playedStatePainting.setColor(ContextCompat.getColor(getContext(), R.color.gray));
notPlayedStatePainting.setStrokeWidth(1f);
notPlayedStatePainting.setAntiAlias(true);
notPlayedStatePainting.setColor(ContextCompat.getColor(getContext(), R.color.colorAccent));
}

/**
* update and redraw Visualizer view
*/
public void updateVisualizer(byte bytes) {
this.bytes = bytes;
invalidate();
}

/**
* Update player percent. 0 - file not played, 1 - full played
*
* @param percent
*/
public void updatePlayerPercent(float percent) {
denseness = (int) Math.ceil(width * percent);
if (denseness < 0) {
denseness = 0;
} else if (denseness > width) {
denseness = width;
}
invalidate();
}

@Override
protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
super.onLayout(changed, left, top, right, bottom);
width = getMeasuredWidth();
height = getMeasuredHeight();
}

@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
if (bytes == null || width == 0) {
return;
}
float totalBarsCount = width / dp(3);
if (totalBarsCount <= 0.1f) {
return;
}
byte value;
int samplesCount = (bytes.length * 8 / 5);
float samplesPerBar = samplesCount / totalBarsCount;
float barCounter = 0;
int nextBarNum = 0;

int y = (height - dp(VISUALIZER_HEIGHT)) / 2;
int barNum = 0;
int lastBarNum;
int drawBarCount;

for (int a = 0; a < samplesCount; a++) {
if (a != nextBarNum) {
continue;
}
drawBarCount = 0;
lastBarNum = nextBarNum;
while (lastBarNum == nextBarNum) {
barCounter += samplesPerBar;
nextBarNum = (int) barCounter;
drawBarCount++;
}

int bitPointer = a * 5;
int byteNum = bitPointer / Byte.SIZE;
int byteBitOffset = bitPointer - byteNum * Byte.SIZE;
int currentByteCount = Byte.SIZE - byteBitOffset;
int nextByteRest = 5 - currentByteCount;
value = (byte) ((bytes[byteNum] >> byteBitOffset) & ((2 << (Math.min(5, currentByteCount) - 1)) - 1));
if (nextByteRest > 0) {
value <<= nextByteRest;
value |= bytes[byteNum + 1] & ((2 << (nextByteRest - 1)) - 1);
}

for (int b = 0; b < drawBarCount; b++) {
int x = barNum * dp(3);
float left = x;
float top = y + dp(VISUALIZER_HEIGHT - Math.max(1, VISUALIZER_HEIGHT * value / 31.0f));
float right = x + dp(2);
float bottom = y + dp(VISUALIZER_HEIGHT);
if (x < denseness && x + dp(2) < denseness) {
canvas.drawRect(left, top, right, bottom, notPlayedStatePainting);
} else {
canvas.drawRect(left, top, right, bottom, playedStatePainting);
if (x < denseness) {
canvas.drawRect(left, top, right, bottom, notPlayedStatePainting);
}
}
barNum++;
}
}
}

public int dp(float value) {
if (value == 0) {
return 0;
}
return (int) Math.ceil(getContext().getResources().getDisplayMetrics().density * value);
}
}


Sorry, code with a small amount of comments, but it is working visualizer. You can attach it to any players you want.



How you can use it: add this view in your xml layout, then you have to update visualizer state with methods



public void updateVisualizer(byte bytes) {
playerVisualizerView.updateVisualizer(bytes);
}

public void updatePlayerProgress(float percent) {
playerVisualizerView.updatePlayerPercent(percent);
}


In updateVisualizer you pass bytes array with you audio sample, and in updatePlayerProgress you dynamically pass percentage, while audio sample is playing.



for converting file to bytes you can use this helper method



public static byte fileToBytes(File file) {
int size = (int) file.length();
byte bytes = new byte[size];
try {
BufferedInputStream buf = new BufferedInputStream(new FileInputStream(file));
buf.read(bytes, 0, bytes.length);
buf.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return bytes;
}


and for example(very shortly), how it looks like with Mosby library:



public class AudioRecorderPresenter extends MvpBasePresenter<AudioRecorderView> {

public void onStopRecord() {
// stopped and released MediaPlayer
// ...
// some preparation and saved audio file in audioFileName variable.

getView().updateVisualizer(FileUtils.fileToBytes(new File(audioFileName)));
}
}
}





share|improve this answer



















  • 1




    Tanx, How can I use PlayerVisualizerView?
    – Hamed Gh
    Oct 21 '17 at 7:34






  • 1




    I think you give the best answer and give +50 award to your answer, but please add more detail and show the usage.
    – Hamed Gh
    Oct 21 '17 at 10:18










  • @HamedGh updated. Is it enough?
    – Scrobot
    Oct 22 '17 at 11:08










  • Would be nice if you comment the code over each line and explain it, specially the bytes movement
    – Diego Fernando Murillo Valenci
    Sep 27 at 16:19






  • 1




    @KathanShah hi! I'm working on it )) I took a lot of feedbacks, and decide to create library for this feature) So, the android library will be published soon. )) In particular, negative waves will be 'in box' too ))
    – Scrobot
    Oct 31 at 10:10













up vote
14
down vote



+50







up vote
14
down vote



+50




+50




Perhaps, you can implements this feature without libraries, of course if you want only visualisation of audio sample.
For example:



public class PlayerVisualizerView extends View {

/**
* constant value for Height of the bar
*/
public static final int VISUALIZER_HEIGHT = 28;

/**
* bytes array converted from file.
*/
private byte bytes;

/**
* Percentage of audio sample scale
* Should updated dynamically while audioPlayer is played
*/
private float denseness;

/**
* Canvas painting for sample scale, filling played part of audio sample
*/
private Paint playedStatePainting = new Paint();
/**
* Canvas painting for sample scale, filling not played part of audio sample
*/
private Paint notPlayedStatePainting = new Paint();

private int width;
private int height;

public PlayerVisualizerView(Context context) {
super(context);
init();
}

public PlayerVisualizerView(Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
init();
}

private void init() {
bytes = null;

playedStatePainting.setStrokeWidth(1f);
playedStatePainting.setAntiAlias(true);
playedStatePainting.setColor(ContextCompat.getColor(getContext(), R.color.gray));
notPlayedStatePainting.setStrokeWidth(1f);
notPlayedStatePainting.setAntiAlias(true);
notPlayedStatePainting.setColor(ContextCompat.getColor(getContext(), R.color.colorAccent));
}

/**
* update and redraw Visualizer view
*/
public void updateVisualizer(byte bytes) {
this.bytes = bytes;
invalidate();
}

/**
* Update player percent. 0 - file not played, 1 - full played
*
* @param percent
*/
public void updatePlayerPercent(float percent) {
denseness = (int) Math.ceil(width * percent);
if (denseness < 0) {
denseness = 0;
} else if (denseness > width) {
denseness = width;
}
invalidate();
}

@Override
protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
super.onLayout(changed, left, top, right, bottom);
width = getMeasuredWidth();
height = getMeasuredHeight();
}

@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
if (bytes == null || width == 0) {
return;
}
float totalBarsCount = width / dp(3);
if (totalBarsCount <= 0.1f) {
return;
}
byte value;
int samplesCount = (bytes.length * 8 / 5);
float samplesPerBar = samplesCount / totalBarsCount;
float barCounter = 0;
int nextBarNum = 0;

int y = (height - dp(VISUALIZER_HEIGHT)) / 2;
int barNum = 0;
int lastBarNum;
int drawBarCount;

for (int a = 0; a < samplesCount; a++) {
if (a != nextBarNum) {
continue;
}
drawBarCount = 0;
lastBarNum = nextBarNum;
while (lastBarNum == nextBarNum) {
barCounter += samplesPerBar;
nextBarNum = (int) barCounter;
drawBarCount++;
}

int bitPointer = a * 5;
int byteNum = bitPointer / Byte.SIZE;
int byteBitOffset = bitPointer - byteNum * Byte.SIZE;
int currentByteCount = Byte.SIZE - byteBitOffset;
int nextByteRest = 5 - currentByteCount;
value = (byte) ((bytes[byteNum] >> byteBitOffset) & ((2 << (Math.min(5, currentByteCount) - 1)) - 1));
if (nextByteRest > 0) {
value <<= nextByteRest;
value |= bytes[byteNum + 1] & ((2 << (nextByteRest - 1)) - 1);
}

for (int b = 0; b < drawBarCount; b++) {
int x = barNum * dp(3);
float left = x;
float top = y + dp(VISUALIZER_HEIGHT - Math.max(1, VISUALIZER_HEIGHT * value / 31.0f));
float right = x + dp(2);
float bottom = y + dp(VISUALIZER_HEIGHT);
if (x < denseness && x + dp(2) < denseness) {
canvas.drawRect(left, top, right, bottom, notPlayedStatePainting);
} else {
canvas.drawRect(left, top, right, bottom, playedStatePainting);
if (x < denseness) {
canvas.drawRect(left, top, right, bottom, notPlayedStatePainting);
}
}
barNum++;
}
}
}

public int dp(float value) {
if (value == 0) {
return 0;
}
return (int) Math.ceil(getContext().getResources().getDisplayMetrics().density * value);
}
}


Sorry, code with a small amount of comments, but it is working visualizer. You can attach it to any players you want.



How you can use it: add this view in your xml layout, then you have to update visualizer state with methods



public void updateVisualizer(byte bytes) {
playerVisualizerView.updateVisualizer(bytes);
}

public void updatePlayerProgress(float percent) {
playerVisualizerView.updatePlayerPercent(percent);
}


In updateVisualizer you pass bytes array with you audio sample, and in updatePlayerProgress you dynamically pass percentage, while audio sample is playing.



for converting file to bytes you can use this helper method



public static byte fileToBytes(File file) {
int size = (int) file.length();
byte bytes = new byte[size];
try {
BufferedInputStream buf = new BufferedInputStream(new FileInputStream(file));
buf.read(bytes, 0, bytes.length);
buf.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return bytes;
}


and for example(very shortly), how it looks like with Mosby library:



public class AudioRecorderPresenter extends MvpBasePresenter<AudioRecorderView> {

public void onStopRecord() {
// stopped and released MediaPlayer
// ...
// some preparation and saved audio file in audioFileName variable.

getView().updateVisualizer(FileUtils.fileToBytes(new File(audioFileName)));
}
}
}





share|improve this answer














Perhaps, you can implements this feature without libraries, of course if you want only visualisation of audio sample.
For example:



public class PlayerVisualizerView extends View {

/**
* constant value for Height of the bar
*/
public static final int VISUALIZER_HEIGHT = 28;

/**
* bytes array converted from file.
*/
private byte bytes;

/**
* Percentage of audio sample scale
* Should updated dynamically while audioPlayer is played
*/
private float denseness;

/**
* Canvas painting for sample scale, filling played part of audio sample
*/
private Paint playedStatePainting = new Paint();
/**
* Canvas painting for sample scale, filling not played part of audio sample
*/
private Paint notPlayedStatePainting = new Paint();

private int width;
private int height;

public PlayerVisualizerView(Context context) {
super(context);
init();
}

public PlayerVisualizerView(Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
init();
}

private void init() {
bytes = null;

playedStatePainting.setStrokeWidth(1f);
playedStatePainting.setAntiAlias(true);
playedStatePainting.setColor(ContextCompat.getColor(getContext(), R.color.gray));
notPlayedStatePainting.setStrokeWidth(1f);
notPlayedStatePainting.setAntiAlias(true);
notPlayedStatePainting.setColor(ContextCompat.getColor(getContext(), R.color.colorAccent));
}

/**
* update and redraw Visualizer view
*/
public void updateVisualizer(byte bytes) {
this.bytes = bytes;
invalidate();
}

/**
* Update player percent. 0 - file not played, 1 - full played
*
* @param percent
*/
public void updatePlayerPercent(float percent) {
denseness = (int) Math.ceil(width * percent);
if (denseness < 0) {
denseness = 0;
} else if (denseness > width) {
denseness = width;
}
invalidate();
}

@Override
protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
super.onLayout(changed, left, top, right, bottom);
width = getMeasuredWidth();
height = getMeasuredHeight();
}

@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
if (bytes == null || width == 0) {
return;
}
float totalBarsCount = width / dp(3);
if (totalBarsCount <= 0.1f) {
return;
}
byte value;
int samplesCount = (bytes.length * 8 / 5);
float samplesPerBar = samplesCount / totalBarsCount;
float barCounter = 0;
int nextBarNum = 0;

int y = (height - dp(VISUALIZER_HEIGHT)) / 2;
int barNum = 0;
int lastBarNum;
int drawBarCount;

for (int a = 0; a < samplesCount; a++) {
if (a != nextBarNum) {
continue;
}
drawBarCount = 0;
lastBarNum = nextBarNum;
while (lastBarNum == nextBarNum) {
barCounter += samplesPerBar;
nextBarNum = (int) barCounter;
drawBarCount++;
}

int bitPointer = a * 5;
int byteNum = bitPointer / Byte.SIZE;
int byteBitOffset = bitPointer - byteNum * Byte.SIZE;
int currentByteCount = Byte.SIZE - byteBitOffset;
int nextByteRest = 5 - currentByteCount;
value = (byte) ((bytes[byteNum] >> byteBitOffset) & ((2 << (Math.min(5, currentByteCount) - 1)) - 1));
if (nextByteRest > 0) {
value <<= nextByteRest;
value |= bytes[byteNum + 1] & ((2 << (nextByteRest - 1)) - 1);
}

for (int b = 0; b < drawBarCount; b++) {
int x = barNum * dp(3);
float left = x;
float top = y + dp(VISUALIZER_HEIGHT - Math.max(1, VISUALIZER_HEIGHT * value / 31.0f));
float right = x + dp(2);
float bottom = y + dp(VISUALIZER_HEIGHT);
if (x < denseness && x + dp(2) < denseness) {
canvas.drawRect(left, top, right, bottom, notPlayedStatePainting);
} else {
canvas.drawRect(left, top, right, bottom, playedStatePainting);
if (x < denseness) {
canvas.drawRect(left, top, right, bottom, notPlayedStatePainting);
}
}
barNum++;
}
}
}

public int dp(float value) {
if (value == 0) {
return 0;
}
return (int) Math.ceil(getContext().getResources().getDisplayMetrics().density * value);
}
}


Sorry, code with a small amount of comments, but it is working visualizer. You can attach it to any players you want.



How you can use it: add this view in your xml layout, then you have to update visualizer state with methods



public void updateVisualizer(byte bytes) {
playerVisualizerView.updateVisualizer(bytes);
}

public void updatePlayerProgress(float percent) {
playerVisualizerView.updatePlayerPercent(percent);
}


In updateVisualizer you pass bytes array with you audio sample, and in updatePlayerProgress you dynamically pass percentage, while audio sample is playing.



for converting file to bytes you can use this helper method



public static byte fileToBytes(File file) {
int size = (int) file.length();
byte bytes = new byte[size];
try {
BufferedInputStream buf = new BufferedInputStream(new FileInputStream(file));
buf.read(bytes, 0, bytes.length);
buf.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return bytes;
}


and for example(very shortly), how it looks like with Mosby library:



public class AudioRecorderPresenter extends MvpBasePresenter<AudioRecorderView> {

public void onStopRecord() {
// stopped and released MediaPlayer
// ...
// some preparation and saved audio file in audioFileName variable.

getView().updateVisualizer(FileUtils.fileToBytes(new File(audioFileName)));
}
}
}






share|improve this answer














share|improve this answer



share|improve this answer








edited Oct 22 '17 at 11:17

























answered Oct 21 '17 at 5:36









Scrobot

646718




646718








  • 1




    Tanx, How can I use PlayerVisualizerView?
    – Hamed Gh
    Oct 21 '17 at 7:34






  • 1




    I think you give the best answer and give +50 award to your answer, but please add more detail and show the usage.
    – Hamed Gh
    Oct 21 '17 at 10:18










  • @HamedGh updated. Is it enough?
    – Scrobot
    Oct 22 '17 at 11:08










  • Would be nice if you comment the code over each line and explain it, specially the bytes movement
    – Diego Fernando Murillo Valenci
    Sep 27 at 16:19






  • 1




    @KathanShah hi! I'm working on it )) I took a lot of feedbacks, and decide to create library for this feature) So, the android library will be published soon. )) In particular, negative waves will be 'in box' too ))
    – Scrobot
    Oct 31 at 10:10














  • 1




    Tanx, How can I use PlayerVisualizerView?
    – Hamed Gh
    Oct 21 '17 at 7:34






  • 1




    I think you give the best answer and give +50 award to your answer, but please add more detail and show the usage.
    – Hamed Gh
    Oct 21 '17 at 10:18










  • @HamedGh updated. Is it enough?
    – Scrobot
    Oct 22 '17 at 11:08










  • Would be nice if you comment the code over each line and explain it, specially the bytes movement
    – Diego Fernando Murillo Valenci
    Sep 27 at 16:19






  • 1




    @KathanShah hi! I'm working on it )) I took a lot of feedbacks, and decide to create library for this feature) So, the android library will be published soon. )) In particular, negative waves will be 'in box' too ))
    – Scrobot
    Oct 31 at 10:10








1




1




Tanx, How can I use PlayerVisualizerView?
– Hamed Gh
Oct 21 '17 at 7:34




Tanx, How can I use PlayerVisualizerView?
– Hamed Gh
Oct 21 '17 at 7:34




1




1




I think you give the best answer and give +50 award to your answer, but please add more detail and show the usage.
– Hamed Gh
Oct 21 '17 at 10:18




I think you give the best answer and give +50 award to your answer, but please add more detail and show the usage.
– Hamed Gh
Oct 21 '17 at 10:18












@HamedGh updated. Is it enough?
– Scrobot
Oct 22 '17 at 11:08




@HamedGh updated. Is it enough?
– Scrobot
Oct 22 '17 at 11:08












Would be nice if you comment the code over each line and explain it, specially the bytes movement
– Diego Fernando Murillo Valenci
Sep 27 at 16:19




Would be nice if you comment the code over each line and explain it, specially the bytes movement
– Diego Fernando Murillo Valenci
Sep 27 at 16:19




1




1




@KathanShah hi! I'm working on it )) I took a lot of feedbacks, and decide to create library for this feature) So, the android library will be published soon. )) In particular, negative waves will be 'in box' too ))
– Scrobot
Oct 31 at 10:10




@KathanShah hi! I'm working on it )) I took a lot of feedbacks, and decide to create library for this feature) So, the android library will be published soon. )) In particular, negative waves will be 'in box' too ))
– Scrobot
Oct 31 at 10:10


















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f38744579%2fshow-waveform-of-audio%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

What visual should I use to simply compare current year value vs last year in Power BI desktop

Alexandru Averescu

Trompette piccolo