Code Monkey home page Code Monkey logo

voicerecorder's Introduction

Easy Audio Recorder

License version

Two stylish design and Easy to use record functions

Why this project exists

In applications that include chat, it is often desired to record audio in m4a-mpeg format to be compatible with IOS. To avoid the confusion of algorithms on the chat screen you can use this library to add voice recording feature to your application with a few lines.

Features and Usage

Administration Base and Idea

Start record when press the button and keep recording until release the button.

class DemoActivity : AppCompatActivity(), AudioRecordListener {

private var recorder: Recorder? = null

override fun onCreate(savedInstanceState: Bundle?) {
      super.onCreate(savedInstanceState)
      setContentView(R.layout.activity_button_usage)
      recorder = Recorder(this)
}

override fun onTouch(p0: View?, p1: MotionEvent?): Boolean 
{
      when (p1?.action) {
          MotionEvent.ACTION_DOWN -> {     
              recorder?.startRecord()                    
              return true
          }
          MotionEvent.ACTION_UP,
          MotionEvent.ACTION_BUTTON_RELEASE -> {         
              recorder?.stopRecording()                
              return true
          }
      }
      return false
}

}

BottomSheetFragment Usage

here :)

class BottomSheetUsageActivity : AppCompatActivity(), AudioRecordListener {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_bottom_sheet_usage)
    }
    
    fun openDialog(view: View) {
        VoiceSenderDialog(this).show(supportFragmentManager, "VOICE")
    }

    override fun onAudioReady(audioUri: String?) {
        TODO("Not yet implemented")
    }

    override fun onRecordFailed(errorMessage: String?) {
        TODO("Not yet implemented")
    }
}

Normal Usage

here :)

class WorkerUsageActivity : AppCompatActivity(), AudioRecordListener {

    lateinit var recorder: Recorder

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_worker_usage)
    }

    fun initRecorder() {
        recorder = Recorder(this)
    }

    fun startRecord() {
        recorder.startRecord()
    }

    fun stopRecord() {
        recorder.stopRecording()
    }

    override fun onAudioReady(audioUri: String?) {
        TODO("Not yet implemented")
    }

    override fun onRecordFailed(errorMessage: String?) {
        TODO("Not yet implemented")
    }

}

Stylish Button Usage

here :)

class ButtonUsageActivity : AppCompatActivity(), AudioRecordListener {

    private var permissionsRequired = arrayOf(
        Manifest.permission.RECORD_AUDIO,
        Manifest.permission.WRITE_EXTERNAL_STORAGE,
        Manifest.permission.READ_EXTERNAL_STORAGE
    )

    private var permissionToRecordAccepted = false
    private var permissionCode = 200

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_button_usage)
        setViews()
    }

    private fun setViews() {
        recordButton.audioRecordListener = this
        if (letsCheckPermissions()) {
            recordButton.setRecordListener()
        } else {
            ActivityCompat.requestPermissions(this, permissionsRequired, permissionCode)
        }
    }

    override fun onAudioReady(audioUri: String?) {
        Toast.makeText(this, audioUri, Toast.LENGTH_SHORT).show()
    }

    override fun onRecordFailed(errorMessage: String?) {
        Toast.makeText(this, errorMessage, Toast.LENGTH_SHORT).show()
    }

    private fun letsCheckPermissions(): Boolean {
        return ContextCompat.checkSelfPermission(
            this,
            android.Manifest.permission.RECORD_AUDIO
        ) == PackageManager.PERMISSION_GRANTED && ContextCompat.checkSelfPermission(
            this,
            android.Manifest.permission.WRITE_EXTERNAL_STORAGE
        ) == PackageManager.PERMISSION_GRANTED && ContextCompat.checkSelfPermission(
            this,
            android.Manifest.permission.READ_EXTERNAL_STORAGE
        ) == PackageManager.PERMISSION_GRANTED
    }

    override fun onRequestPermissionsResult(
        requestCode: Int,
        permissions: Array<out String>,
        grantResults: IntArray
    ) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults)
        if (requestCode == permissionCode) {
            permissionToRecordAccepted =
                (grantResults[0] == PackageManager.PERMISSION_GRANTED) && ((grantResults[1] == PackageManager.PERMISSION_GRANTED))
            if (permissionToRecordAccepted) recordButton.setRecordListener()
        }
        if (!permissionToRecordAccepted) Toast.makeText(
            this,
            "You have to accept permissions to send voice",
            Toast.LENGTH_SHORT
        ).show()
    }

}
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".ButtonUsageActivity">

    <ImageView
        android:layout_width="40dp"
        android:layout_height="40dp"
        android:layout_gravity="bottom"
        android:layout_margin="5dp"
        android:background="@drawable/button_circle"
        android:backgroundTint="@android:color/holo_green_light"
        android:padding="10dp"
        android:src="@drawable/ic_gallery_icon"
        android:tint="@color/white" />


    <androidx.cardview.widget.CardView
        android:layout_width="match_parent"
        android:layout_height="40dp"
        android:layout_gravity="bottom"
        android:layout_marginStart="50dp"
        android:layout_marginEnd="50dp"
        android:layout_marginBottom="5dp"
        app:cardBackgroundColor="@color/white"
        app:cardCornerRadius="20dp"
        app:cardElevation="0dp">

        <EditText
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="@null"
            android:fontFamily="@font/rubik_regular"
            android:hint="Your Message"
            android:paddingStart="20dp"
            android:paddingEnd="15dp"
            android:textSize="14sp" />

    </androidx.cardview.widget.CardView>

    <com.asynctaskcoffee.audiorecorder.uikit.RecordButton
        android:id="@+id/recordButton"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_gravity="bottom|end" />

</FrameLayout>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools">

    <uses-permission android:name="android.permission.RECORD_AUDIO" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />

    <application
        android:requestLegacyExternalStorage="true"
        tools:targetApi="q" />
</manifest>

Extras

Change Record Path
fun setFileName(fileName: String?) {
   this.fileName = fileName
}
Beep before record
val dialog = VoiceSenderDialog(this)
dialog.setBeepEnabled(true)
dialog.show(supportFragmentManager, "VOICE")
recordButton.audioRecordListener = this
recordButton.beepEnabled = true
Easy MPlayer
lateinit var player: Player

override fun onAudioReady(audioUri: String?) {
    player = Player(this)
    player.injectMedia(audioUri)
}

fun playRecord(view: View) {
    if (player.player!!.isPlaying)
        player.stopPlaying()
    else player.startPlaying()
}
Customize Dialog Language with LangObj
public LangObj() {
}

String record_audio_string = "Start Record";
String hold_for_record_string = "Hold for record";
String release_for_end_string = "Release for end record";
String listen_record_string = "You can listen record";
String stop_listen_record_string = "Stop Listen";
String stop_record_string = "Stop Record";
String send_record_string = "Send Record";

public LangObj(String record_audio_string, String hold_for_record_string, String release_for_end_string, String listen_record_string, String stop_listen_record_string, String stop_record_string, String send_record_string) {
    this.record_audio_string = record_audio_string;
    this.hold_for_record_string = hold_for_record_string;
    this.release_for_end_string = release_for_end_string;
    this.listen_record_string = listen_record_string;
    this.stop_listen_record_string = stop_listen_record_string;
    this.stop_record_string = stop_record_string;
    this.send_record_string = send_record_string;
}
public VoiceSenderDialog(AudioRecordListener audioRecordListener, LangObj langObj) {
    this.langObj = langObj;
    this.audioRecordListener = audioRecordListener;
}
Customize Dialog Icons with LangObj
public IconsObj() {
}

int ic_start_record = R.drawable.ic_start_record;
int ic_stop_play = R.drawable.ic_stop_play;
int ic_play_record = R.drawable.ic_play_record;
int ic_audio_delete = R.drawable.ic_audio_delete;
int ic_send_circle = R.drawable.ic_send_circle;
int ic_stop_record = R.drawable.ic_stop_record;

public IconsObj(int ic_start_record, int ic_stop_play, int ic_play_record, int ic_audio_delete, int ic_send_circle, int ic_stop_record) {
    this.ic_start_record = ic_start_record;
    this.ic_stop_play = ic_stop_play;
    this.ic_play_record = ic_play_record;
    this.ic_audio_delete = ic_audio_delete;
    this.ic_send_circle = ic_send_circle;
    this.ic_stop_record = ic_stop_record;
}
public VoiceSenderDialog(AudioRecordListener audioRecordListener, IconsObj iconsObj) {
    this.iconsObj = iconsObj;
    this.audioRecordListener = audioRecordListener;
}

Implementation Gradle

Add it in your root build.gradle at the end of repositories
allprojects {
    repositories {
        maven { url 'https://jitpack.io' }
    }
}
Add the dependency
dependencies {
    implementation 'com.github.AsynctaskCoffee:VoiceRecorder:beta-0.5'
}

Implementation Maven

Add the JitPack repository to your build file
<repositories>
	<repository>
		<id>jitpack.io</id>
		<url>https://jitpack.io</url>
	</repository>
</repositories>
Add the dependency
<dependency>
	<groupId>com.github.AsynctaskCoffee</groupId>
	<artifactId>VoiceRecorder</artifactId>
	<version>beta-0.5</version>
</dependency>

Credits

@vedraj360

License

Copyright 2020 Egemen ÖZOGUL

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License 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.

voicerecorder's People

Contributors

asynctaskcoffee avatar vedraj360 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

voicerecorder's Issues

java.lang.IllegalStateException

Hello, Im getting this error when not holding the record button. is there any way to avoid this from happening? thanks

java.lang.IllegalStateException
at android.media.MediaRecorder._start(Native Method)
at android.media.MediaRecorder.start(MediaRecorder.java:1313)
at com.asynctaskcoffee.audiorecorder.worker.Recorder.startRecord(Recorder.kt:40)
at com.asynctaskcoffee.audiorecorder.uikit.VoiceSenderDialog$1.run(VoiceSenderDialog.java:184)
at android.os.Handler.handleCallback(Handler.java:789)
at android.os.Handler.dispatchMessage(Handler.java:98)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6944)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:327)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1374)

Fragment VoiceSenderDialog not attached to an activity.

I am having a issue that It just records for 1 second and the it stops recording. And the uri path is also null. can you help me out.

I am using bottom sheet dialog
new VoiceSenderDialog(this).show(getSupportFragmentManager(), "VOICE");

java.lang.IllegalStateException: Fragment VoiceSenderDialog{b1bef6a} (f0b82165-1ae6-4a05-9e4f-c0fef4ba5a4c) not attached to an activity.
at androidx.fragment.app.Fragment.requireActivity(Fragment.java:928)

Android 13

Voice not recording in Android Version 13

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.