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

Commit 22dce9b

Browse files
committed
Added seeking functionality, added more convenient listeners for java
1 parent e801f19 commit 22dce9b

File tree

8 files changed

+106
-15
lines changed

8 files changed

+106
-15
lines changed

app/src/main/java/rm/com/audiogram/AnotherActivity.java

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
import android.os.Bundle;
55

66
import rm.com.audiowave.AudioWaveView;
7+
import rm.com.audiowave.OnSamplingListener;
8+
import rm.com.audiowave.OnProgressListener;
79

810
public class AnotherActivity extends Activity {
911

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

1820
waveView.setScaledData(data);
1921

20-
// waveView.setRawData(data, new Function0<Unit>() {
21-
// @Override
22-
// public Unit invoke() {
23-
// Log.d("Set raw data", "Callback called");
24-
// return null;
25-
// }
26-
// });
22+
waveView.setRawData(data, new OnSamplingListener() {
23+
@Override
24+
public void onComplete() {
25+
26+
}
27+
});
28+
29+
waveView.setOnProgressListener(new OnProgressListener() {
30+
@Override
31+
public void onStartTracking(float progress) {
32+
33+
}
34+
35+
@Override
36+
public void onStopTracking(float progress) {
37+
38+
}
39+
40+
@Override
41+
public void onProgressChanged(float progress, boolean byUser) {
42+
43+
}
44+
});
2745
}
2846
}

app/src/main/kotlin/rm/com/audiogram/MainActivity.kt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package rm.com.audiogram
33
import android.animation.ObjectAnimator
44
import android.os.Bundle
55
import android.support.v7.app.AppCompatActivity
6+
import android.util.Log
67
import android.view.animation.LinearInterpolator
78
import android.widget.Button
89
import rm.com.audiowave.AudioWaveView
@@ -26,6 +27,10 @@ class MainActivity : AppCompatActivity() {
2627
play.setOnClickListener {
2728
inflateWave()
2829
}
30+
31+
wave.onStopTracking = {
32+
Log.e("wave", "Progress set: $it")
33+
}
2934
}
3035

3136
fun inflateWave() {

app/src/main/res/layout/activity_main.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
android:layout_alignParentBottom="true"
1919
android:layout_margin="16dp"
2020
app:chunkWidth="3dp"
21-
app:chunkHeight="32dp"
21+
app:chunkHeight="24dp"
2222
app:minChunkHeight="2dp"
2323
app:chunkSpacing="1dp"
2424
app:chunkRadius="1dp"

audiowave/build.gradle

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,13 @@ apply plugin: 'kotlin-android'
33

44
android {
55
compileSdkVersion 25
6-
buildToolsVersion "24.0.0"
6+
buildToolsVersion "25.0.0"
77

88
defaultConfig {
99
minSdkVersion 15
1010
targetSdkVersion 25
1111
versionCode 1
1212
versionName "1.0"
13-
14-
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
15-
1613
}
1714
buildTypes {
1815
release {
@@ -26,7 +23,7 @@ android {
2623
}
2724

2825
dependencies {
29-
compile 'org.jetbrains.kotlin:kotlin-stdlib:1.0.4'
26+
compile 'org.jetbrains.kotlin:kotlin-stdlib:1.0.5-2'
3027
}
3128

3229
repositories {

audiowave/src/main/kotlin/rm/com/audiowave/AudioWaveView.kt

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import android.graphics.Bitmap
66
import android.graphics.Canvas
77
import android.graphics.Color
88
import android.util.AttributeSet
9+
import android.view.MotionEvent
910
import android.view.View
1011
import android.view.animation.OvershootInterpolator
1112

@@ -21,6 +22,14 @@ class AudioWaveView : View {
2122
inflateAttrs(attrs)
2223
}
2324

25+
var onProgressListener: OnProgressListener? = null
26+
27+
var onProgressChanged: (Float, Boolean) -> Unit = { progress, byUser -> Unit }
28+
29+
var onStartTracking: (Float) -> Unit = {}
30+
31+
var onStopTracking: (Float) -> Unit = {}
32+
2433
var chunkHeight: Int = 0
2534
get() = if (field == 0) h else Math.abs(field)
2635
set(value) {
@@ -64,6 +73,10 @@ class AudioWaveView : View {
6473
require(value in 0..100) { "Progress must be in 0..100" }
6574

6675
field = Math.abs(value)
76+
77+
onProgressListener?.onProgressChanged(field, isTouched)
78+
onProgressChanged(field, isTouched)
79+
6780
postInvalidate()
6881
}
6982

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

101+
var isTouched = false
102+
88103
private val chunksCount: Int
89104
get() = w / chunkStep
90105

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

168+
override fun onTouchEvent(event: MotionEvent?): Boolean {
169+
event ?: return super.onTouchEvent(event)
170+
171+
when (event.action) {
172+
MotionEvent.ACTION_DOWN -> {
173+
isTouched = true
174+
progress = event.toProgress()
175+
176+
// these paired calls look ugly, but we need them for Java
177+
onProgressListener?.onStartTracking(progress)
178+
onStartTracking(progress)
179+
180+
return true
181+
}
182+
MotionEvent.ACTION_MOVE -> {
183+
isTouched = true
184+
progress = event.toProgress()
185+
return true
186+
}
187+
MotionEvent.ACTION_UP -> {
188+
isTouched = false
189+
onProgressListener?.onStopTracking(progress)
190+
onStopTracking(progress)
191+
return false
192+
}
193+
else -> {
194+
isTouched = false
195+
return super.onTouchEvent(event)
196+
}
197+
}
198+
}
199+
200+
fun MotionEvent.toProgress() = this@toProgress.x.clamp(0F, w.toFloat()) / w * 100F
201+
202+
// Java convenience
203+
fun setRawData(raw: ByteArray, callback: OnSamplingListener) {
204+
setRawData(raw) { callback.onComplete() }
205+
}
206+
153207
@JvmOverloads
154208
fun setRawData(raw: ByteArray, callback: () -> Unit = {}) {
155209
MAIN_THREAD.postDelayed({

audiowave/src/main/kotlin/rm/com/audiowave/Graphics.kt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@ internal fun Int.withAlpha(alpha: Int): Int {
4141
return this and 0x00FFFFFF or (alpha shl 24)
4242
}
4343

44+
internal fun Float.clamp(min: Float, max: Float) = Math.min(max, Math.max(this, min))
45+
4446
internal fun Bitmap.inCanvas(): Canvas = Canvas(this)
4547

4648
internal fun Bitmap?.safeRecycle() =
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package rm.com.audiowave
2+
3+
/**
4+
* Created by alex
5+
*/
6+
7+
interface OnSamplingListener {
8+
fun onComplete()
9+
}
10+
11+
interface OnProgressListener {
12+
fun onStartTracking(progress: Float)
13+
fun onStopTracking(progress: Float)
14+
fun onProgressChanged(progress: Float, byUser: Boolean)
15+
}

build.gradle

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
// Top-level build file where you can add configuration options common to all sub-projects/modules.
22

33
buildscript {
4-
ext.kotlin_version = '1.0.4'
4+
ext.kotlin_version = '1.0.5-2'
55
repositories {
66
jcenter()
77
}
88
dependencies {
9-
classpath 'com.android.tools.build:gradle:2.2.0'
9+
classpath 'com.android.tools.build:gradle:2.2.2'
1010
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
1111

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

0 commit comments

Comments
 (0)