Analyze real-time audio and display sound waves in an interactive and engaging way π΅
- π Real-time audio analysis: View sound waves while recording or playing an audio file.
- π¨ Customize Display: Easily adjust wave colors, line widths, and wave speeds.
- π Easy-to-use Programming Interface: Simple and intuitive programming interface for seamless integration.
- π± Wide Compatibility: Supports Android API level 21 and above.
- ποΈ Advanced Control: Playback speed control, pause/resume functions, and audio progress monitoring.
- ποΈ Audio Recording: Ability to record audio and display real-time waveforms.
- π Volume Control: Adjust the volume for the left and right channels.
- π Repeat Playback: Ability to set repeated playback of the audio file.
- π High Performance: Designed for high efficiency with minimal impact on your app's performance.
- π Open Source: Full access to the source code for customization and optimization.
- π₯ Community Support: Supported by a dedicated community, with opportunities for contributions and improvements.
- π Highly Smooth: Utilizes the latest and most powerful algorithms to deliver an impressive and smooth result
dependencyResolutionManagement {
repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
repositories {
mavenCentral()
//...
maven { url 'https://jitpack.io' }
}
}
Add the following to your project's
build.gradle
file:
dependencies {
implementation 'com.github.alex11111115:WavePlayerView:1.12'
}
Add
WavePlayerView
to your activity layout fileactivity_main.xml
:
<sound.wave.kilobyte.WavePlayerView
android:id="@+id/waveView"
android:layout_width="250dp"
android:layout_height="75dp"
android:padding="2dp"
android:gravity="center"
android:orientation="horizontal"
android:layout_gravity="center"
app:waveColor1="#FF8E8DE5"
app:waveStrokeWidth1="5dp"
app:waveColor2="#FFE8D8FB"
app:waveStrokeWidth2="5dp"
app:waveSpeed="0.5"
app:wave2Visible="true" />
In your
MainActivity.java
file, you can control theWavePlayerView
as follows:
To start using it, you need to initialize it first
public class MainActivity extends AppCompatActivity {
private WavePlayerView waveView;
//....
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
waveView = findViewById(R.id.waveView);
//.....
}
}
To play from file path:
// Play audio from a file path
waveView.playAudioWithWaveFromPath(filePath);
To play from your app resources:
// Play audio from app resources
waveView.playAudioWithWave(R.raw.mus); //Replace 'mus' with your audio
To pause media and waves:
// Pause media and waves
waveView.pauseAudioWithWave();
To resume media and waves:
// Resume media and waves
waveView.resumeAudioWithWave();
To stop media and waves:
// Stop media and waves
waveView.stopAudioWithWave();
To start listening and show waves live:
// Start listening and show live waves
waveView.startListening();
To stop listening:
// Stop listening
waveView.stopListening();
To set playback speed:
// Set playback speed (default is "1.0")
waveView.setPlaybackSpeed("1.5");
To set wave properties:
// Set wave properties
waveView.setWaveProperties(color1, wave1Width, color2, wave2Width, waveSpeed);
To set wave1 alpha
waveView.setWave1Alpha(alpha1);
To set wave2 alpha
waveView.setWave2Alpha(alpha2);
To control the visibility of the second wave:
// Hide the second wave
waveView.setWave2Visible(false);
// Show the second wave
waveView.setWave2Visible(true);
To handle audio completion events:
// Handle audio completion events
waveView.setOnAudioCompleteListener(() -> {
//your code here
});
To track audio progress:
// Track audio progress
waveView.setOnAudioProgressListener(currentMilliSec -> {
//your codes here
});
To seek to a position in the media:
// Seek to a position in the media
progressbar1.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
value = progress; // Store value
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
waveView.pauseAudioWithWave(); // Pause media
}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
waveView.seekToPosition((int) value); // Seek to new position
waveView.setPlaybackSpeed(s);
}
});
Getting the duration of the audio
// Getting the duration of the audio
int duration = waveView.getAudioDuration();
Get the current position of the audio
// Get the current position of the audio
int currentPosition = waveView.getCurrentAudioPosition();
Adjusting the volume
// Adjusting the volume
waveView.setVolume(leftVolume, rightVolume);
setLooping
// setLooping
waveView.setLooping(true);
Put this in "onDestroy" as follows
if (waveView != null) {
waveView.release();
}
Add the following permissions to your
AndroidManifest.xml
:
<uses-permission android:name="android.permission.RECORD_AUDIO" />
- Be sure to ask for audio recording permissions at runtime for devices running Android 6.0 and above.
- The appearance of the waves can be customized through various properties available in XML or programmatically.
- Use
setPlaybackSpeed()
to change the playback speed. Allowable values range from 0.2 to 6.0. - You can control the volume with
setVolume()
where the values range from 0.0 (mute) to 1.0 (loudest). - Use
setLooping()
to set whether the sound will automatically repeat after it ends.
WavePlayerView is developed and maintained by alex11111115.
We welcome contributions from the community! If you have ideas or improvements, feel free to submit pull requests or open issues on the GitHub repository.
WavePlayerView is licensed under the Apache License 2.0.