Skip to content
This repository has been archived by the owner on Sep 22, 2022. It is now read-only.

Commit

Permalink
Added seeking functionality, added more convenient listeners for java
Browse files Browse the repository at this point in the history
  • Loading branch information
alxrm committed Nov 26, 2016
1 parent e801f19 commit 22dce9b
Show file tree
Hide file tree
Showing 8 changed files with 106 additions and 15 deletions.
32 changes: 25 additions & 7 deletions app/src/main/java/rm/com/audiogram/AnotherActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
import android.os.Bundle;

import rm.com.audiowave.AudioWaveView;
import rm.com.audiowave.OnSamplingListener;
import rm.com.audiowave.OnProgressListener;

public class AnotherActivity extends Activity {

Expand All @@ -17,12 +19,28 @@ protected void onCreate(Bundle savedInstanceState) {

waveView.setScaledData(data);

// waveView.setRawData(data, new Function0<Unit>() {
// @Override
// public Unit invoke() {
// Log.d("Set raw data", "Callback called");
// return null;
// }
// });
waveView.setRawData(data, new OnSamplingListener() {
@Override
public void onComplete() {

}
});

waveView.setOnProgressListener(new OnProgressListener() {
@Override
public void onStartTracking(float progress) {

}

@Override
public void onStopTracking(float progress) {

}

@Override
public void onProgressChanged(float progress, boolean byUser) {

}
});
}
}
5 changes: 5 additions & 0 deletions app/src/main/kotlin/rm/com/audiogram/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package rm.com.audiogram
import android.animation.ObjectAnimator
import android.os.Bundle
import android.support.v7.app.AppCompatActivity
import android.util.Log
import android.view.animation.LinearInterpolator
import android.widget.Button
import rm.com.audiowave.AudioWaveView
Expand All @@ -26,6 +27,10 @@ class MainActivity : AppCompatActivity() {
play.setOnClickListener {
inflateWave()
}

wave.onStopTracking = {
Log.e("wave", "Progress set: $it")
}
}

fun inflateWave() {
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
android:layout_alignParentBottom="true"
android:layout_margin="16dp"
app:chunkWidth="3dp"
app:chunkHeight="32dp"
app:chunkHeight="24dp"
app:minChunkHeight="2dp"
app:chunkSpacing="1dp"
app:chunkRadius="1dp"
Expand Down
7 changes: 2 additions & 5 deletions audiowave/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,13 @@ apply plugin: 'kotlin-android'

android {
compileSdkVersion 25
buildToolsVersion "24.0.0"
buildToolsVersion "25.0.0"

defaultConfig {
minSdkVersion 15
targetSdkVersion 25
versionCode 1
versionName "1.0"

testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"

}
buildTypes {
release {
Expand All @@ -26,7 +23,7 @@ android {
}

dependencies {
compile 'org.jetbrains.kotlin:kotlin-stdlib:1.0.4'
compile 'org.jetbrains.kotlin:kotlin-stdlib:1.0.5-2'
}

repositories {
Expand Down
54 changes: 54 additions & 0 deletions audiowave/src/main/kotlin/rm/com/audiowave/AudioWaveView.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import android.graphics.Bitmap
import android.graphics.Canvas
import android.graphics.Color
import android.util.AttributeSet
import android.view.MotionEvent
import android.view.View
import android.view.animation.OvershootInterpolator

Expand All @@ -21,6 +22,14 @@ class AudioWaveView : View {
inflateAttrs(attrs)
}

var onProgressListener: OnProgressListener? = null

var onProgressChanged: (Float, Boolean) -> Unit = { progress, byUser -> Unit }

var onStartTracking: (Float) -> Unit = {}

var onStopTracking: (Float) -> Unit = {}

var chunkHeight: Int = 0
get() = if (field == 0) h else Math.abs(field)
set(value) {
Expand Down Expand Up @@ -64,6 +73,10 @@ class AudioWaveView : View {
require(value in 0..100) { "Progress must be in 0..100" }

field = Math.abs(value)

onProgressListener?.onProgressChanged(field, isTouched)
onProgressChanged(field, isTouched)

postInvalidate()
}

Expand All @@ -85,6 +98,8 @@ class AudioWaveView : View {
field = Math.max(400, value)
}

var isTouched = false

private val chunksCount: Int
get() = w / chunkStep

Expand Down Expand Up @@ -150,6 +165,45 @@ class AudioWaveView : View {
super.onLayout(changed, left, top, right, bottom)
}

override fun onTouchEvent(event: MotionEvent?): Boolean {
event ?: return super.onTouchEvent(event)

when (event.action) {
MotionEvent.ACTION_DOWN -> {
isTouched = true
progress = event.toProgress()

// these paired calls look ugly, but we need them for Java
onProgressListener?.onStartTracking(progress)
onStartTracking(progress)

return true
}
MotionEvent.ACTION_MOVE -> {
isTouched = true
progress = event.toProgress()
return true
}
MotionEvent.ACTION_UP -> {
isTouched = false
onProgressListener?.onStopTracking(progress)
onStopTracking(progress)
return false
}
else -> {
isTouched = false
return super.onTouchEvent(event)
}
}
}

fun MotionEvent.toProgress() = this@toProgress.x.clamp(0F, w.toFloat()) / w * 100F

// Java convenience
fun setRawData(raw: ByteArray, callback: OnSamplingListener) {
setRawData(raw) { callback.onComplete() }
}

@JvmOverloads
fun setRawData(raw: ByteArray, callback: () -> Unit = {}) {
MAIN_THREAD.postDelayed({
Expand Down
2 changes: 2 additions & 0 deletions audiowave/src/main/kotlin/rm/com/audiowave/Graphics.kt
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ internal fun Int.withAlpha(alpha: Int): Int {
return this and 0x00FFFFFF or (alpha shl 24)
}

internal fun Float.clamp(min: Float, max: Float) = Math.min(max, Math.max(this, min))

internal fun Bitmap.inCanvas(): Canvas = Canvas(this)

internal fun Bitmap?.safeRecycle() =
Expand Down
15 changes: 15 additions & 0 deletions audiowave/src/main/kotlin/rm/com/audiowave/Listeners.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package rm.com.audiowave

/**
* Created by alex
*/

interface OnSamplingListener {
fun onComplete()
}

interface OnProgressListener {
fun onStartTracking(progress: Float)
fun onStopTracking(progress: Float)
fun onProgressChanged(progress: Float, byUser: Boolean)
}
4 changes: 2 additions & 2 deletions build.gradle
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
// Top-level build file where you can add configuration options common to all sub-projects/modules.

buildscript {
ext.kotlin_version = '1.0.4'
ext.kotlin_version = '1.0.5-2'
repositories {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:2.2.0'
classpath 'com.android.tools.build:gradle:2.2.2'
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"

// NOTE: Do not place your application dependencies here; they belong
Expand Down

0 comments on commit 22dce9b

Please sign in to comment.