Code Monkey home page Code Monkey logo

waveformviewdemo's Introduction

WaveFormView

WaveFormView provides a view to display audio waveforms.

Generating waveforms at runtime, you don't have to prepare data in advance.

Note : It takes a few seconds to generate

Screenshots

Importing the Library

Add the following dependicity to your build.gradle file.

dependencies {
    repositories {
        jcenter()
    }
    compile 'space.siy:waveformview:1.0.0'
}

Usage

You can see full code at MainActivity.kt

//Open From Assets Folder
val afd = assets.openFd("jazz_in_paris.mp3")

//Build WaveFormData
WaveFormData.Factory(afd.fileDescriptor, afd.startOffset, afd.length)
        .build(object : WaveFormData.Factory.Callback {
            //When Complete, you can receive data and set to the view
            override fun onComplete(waveFormData: WaveFormData) {
                waveFormView.data = waveFormData

                //Initialize MediaPlayer
                val player = MediaPlayer()
                player.setDataSource(afd.fileDescriptor, afd.startOffset, afd.length)
                player.prepare()
                player.start()

                //Synchronize with MediaPlayer using WaveFormView.Callback
                waveFormView.callback = object : WaveFormView.Callback {
                    override fun onPlayPause() {
                        if (player.isPlaying)
                            player.pause()
                        else
                            player.start()
                    }
                    override fun onSeek(pos: Long) {
                        player.seekTo(pos.toInt())
                    }
                }

                //You have to notify current position to the view
                Handler().postDelayed(object : Runnable {
                    override fun run() {
                        waveFormView.position = player.currentPosition.toLong()
                        Handler().postDelayed(this, 20)
                    }
                }, 20)

            }

            override fun onProgress(progress: Float) {
                progressBar.progress = (progress*10).toInt()
            }
        })

Customization

You can change block style via xml and program.

The following xml shows all attributes.

<space.siy.waveformview.WaveFormView
        android:id="@+id/waveFormView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:blockColor="@color/white"
        app:blockColorPlayed="@color/red"
        app:showTimeText="true"
        app:textColor="@color/white"
        app:textBgColor="@color/black"
        app:blockWidth="10"
        app:topBlockScale="1"
        app:bottomBlockScale="0.5"
        app:peakMode="peakmode_average"
        app:secPerBlock="0.1" />

Document

See here.

Requirement

Supports Android 5.0+

WaveFormView uses MediaCodec supporting 5.0+ to generate waveform at runtime.

Lisence

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this work except in compliance with the License. You may obtain a copy of the License in the LICENSE file, or at:

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

waveformviewdemo's People

Contributors

siy1121 avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.