-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Library Support Record And Play (#2)
* feat: rebuild library initial commit * chore: fixed package json * fix: fixed android crash on stop play back * feat: added list and clear audio files
- Loading branch information
1 parent
ec69695
commit b17bce7
Showing
37 changed files
with
2,681 additions
and
449 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,4 @@ | ||
<manifest> | ||
<manifest xmlns:android="http://schemas.android.com/apk/res/android"> | ||
<uses-permission android:name="android.permission.RECORD_AUDIO"/> | ||
<uses-permission android:name="android.permission.INTERNET"/> | ||
</manifest> |
9 changes: 9 additions & 0 deletions
9
android/src/main/java/expo/modules/audiostream/AudioDataEncoder.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package expo.modules.audiostream | ||
|
||
import android.util.Base64 | ||
|
||
class AudioDataEncoder { | ||
public fun encodeToBase64(rawData: ByteArray): String { | ||
return Base64.encodeToString(rawData, Base64.NO_WRAP) | ||
} | ||
} |
92 changes: 92 additions & 0 deletions
92
android/src/main/java/expo/modules/audiostream/AudioFileHandler.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
package expo.modules.audiostream | ||
|
||
import java.io.File | ||
import java.io.IOException | ||
import java.io.OutputStream | ||
import java.io.RandomAccessFile | ||
|
||
class AudioFileHandler(private val filesDir: File) { | ||
// Method to write WAV file header | ||
fun writeWavHeader(out: OutputStream, sampleRateInHz: Int, channels: Int, bitDepth: Int) { | ||
val header = ByteArray(44) | ||
val byteRate = sampleRateInHz * channels * bitDepth / 8 | ||
val blockAlign = channels * bitDepth / 8 | ||
|
||
// RIFF/WAVE header | ||
"RIFF".toByteArray().copyInto(header, 0) | ||
// (file size - 8) to be updated later | ||
header[4] = 0 // Placeholder | ||
header[5] = 0 // Placeholder | ||
header[6] = 0 // Placeholder | ||
header[7] = 0 // Placeholder | ||
"WAVE".toByteArray().copyInto(header, 8) | ||
"fmt ".toByteArray().copyInto(header, 12) | ||
|
||
// 16 for PCM | ||
header[16] = 16 | ||
header[17] = 0 | ||
header[18] = 0 | ||
header[19] = 0 | ||
|
||
// PCM format ID | ||
header[20] = 1 // Audio format 1 for PCM (not compressed) | ||
header[21] = 0 | ||
|
||
// Number of channels | ||
header[22] = (channels and 0xff).toByte() | ||
header[23] = (channels shr 8 and 0xff).toByte() | ||
|
||
// Sample rate | ||
header[24] = (sampleRateInHz and 0xff).toByte() | ||
header[25] = (sampleRateInHz shr 8 and 0xff).toByte() | ||
header[26] = (sampleRateInHz shr 16 and 0xff).toByte() | ||
header[27] = (sampleRateInHz shr 24 and 0xff).toByte() | ||
|
||
// Byte rate | ||
header[28] = (byteRate and 0xff).toByte() | ||
header[29] = (byteRate shr 8 and 0xff).toByte() | ||
header[30] = (byteRate shr 16 and 0xff).toByte() | ||
header[31] = (byteRate shr 24 and 0xff).toByte() | ||
|
||
// Block align | ||
header[32] = (blockAlign and 0xff).toByte() | ||
header[33] = (blockAlign shr 8 and 0xff).toByte() | ||
|
||
// Bits per sample | ||
header[34] = (bitDepth and 0xff).toByte() | ||
header[35] = (bitDepth shr 8 and 0xff).toByte() | ||
|
||
// Data chunk | ||
"data".toByteArray().copyInto(header, 36) | ||
// Data size to be updated later | ||
header[40] = 0 // Placeholder | ||
header[41] = 0 // Placeholder | ||
header[42] = 0 // Placeholder | ||
header[43] = 0 // Placeholder | ||
|
||
out.write(header, 0, 44) | ||
} | ||
|
||
fun updateWavHeader(file: File) { | ||
try { | ||
RandomAccessFile(file, "rw").use { raf -> | ||
val fileSize = raf.length() | ||
val dataSize = fileSize - 44 // Subtract the header size | ||
|
||
raf.seek(4) // Write correct file size, excluding the first 8 bytes of the RIFF header | ||
raf.writeInt(Integer.reverseBytes((dataSize + 36).toInt())) | ||
|
||
raf.seek(40) // Go to the data size position | ||
raf.writeInt(Integer.reverseBytes(dataSize.toInt())) // Write the size of the data segment | ||
} | ||
} catch (e: IOException) { | ||
println("Could not update WAV header: ${e.message}") | ||
} | ||
} | ||
|
||
fun clearAudioStorage() { | ||
filesDir.listFiles()?.forEach { | ||
it.delete() | ||
} | ||
} | ||
} |
Oops, something went wrong.