An open API service indexing awesome lists of open source software.

https://github.com/hyunjinno/foregroundaudiorecorder

A library for audio recording at foreground.
https://github.com/hyunjinno/foregroundaudiorecorder

android foreground-service jitpack recorder

Last synced: about 1 month ago
JSON representation

A library for audio recording at foreground.

Awesome Lists containing this project

README

          

# ForegroundAudioRecorder
A library for audio recording at foreground.

[![](https://jitpack.io/v/HyunJinNo/ForegroundAudioRecorder.svg)](https://jitpack.io/#HyunJinNo/ForegroundAudioRecorder)


## Index

[1. Examples](#example)

[2. Gradle Setup](#gradle-setup)

[3. How to use](#how-to-use)


## Example


## Gradle Setup

To get this library into your build:

#### Step 1. Add the JitPack repository to your build file.

Add it in your settings.gradle at the end of repositories:

```gradle
repositories {
...
maven { url 'https://jitpack.io' }
}
```

#### Step 2. Add the dependency.

Add it in your build.gradle (:app):

```gradle
dependencies {
...
implementation 'com.github.HyunJinNo:ForegroundAudioRecorder:0.0:3'
}
```


## How to use

#### Step 1. Add the following permissions to your AndroidManifest.xml.

```xml

...




...

```

#### Step 2. Request permissions before calling startService() method.

```kotlin
class MainActivity : AppCompatActivity() {
private var permissions: Array = arrayOf(
Manifest.permission.RECORD_AUDIO,
Manifest.permission.WRITE_EXTERNAL_STORAGE
)

companion object {
private const val REQUEST_PERMISSIONS = 200
}

override fun onCreate(savedInstanceState: Bundle?) {
...
requestPermissions(permissions, REQUEST_PERMISSIONS)
}

override fun onRequestPermissionsResult(
requestCode: Int,
permissions: Array,
grantResults: IntArray
) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults)
val flag = if (requestCode == REQUEST_PERMISSIONS) {
grantResults.all { it == PackageManager.PERMISSION_GRANTED }
} else {
false
}

if (!flag) {
Toast.makeText(applicationContext, "Permissions rejected.", Toast.LENGTH_SHORT).show()
finish()
}
}
}
```

#### Step 3. After the permissions granted, call startService() method.

```kotlin
val intent = Intent(applicationContext, AudioService::class.java)
startService(intent)
```

#### Step 4. call stopService() method if you don't use the service anymore.

If you call stopService() method during audio recording, a recorded audio file is automatically created.

```kotlin
val intent = Intent(applicationContext, AudioService::class.java)
stopService(intent)
```