Skip to content

Commit

Permalink
Create audio track only once (do keep instance when pressing stop)
Browse files Browse the repository at this point in the history
  • Loading branch information
thetwom committed Aug 3, 2020
1 parent a17ad37 commit bdcf714
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 31 deletions.
57 changes: 27 additions & 30 deletions app/src/main/java/de/moekadu/audiotracktest/Player.kt
Original file line number Diff line number Diff line change
Expand Up @@ -10,75 +10,72 @@ import kotlin.math.sin

class Player {

private val sampleRate = AudioTrack.getNativeOutputSampleRate(AudioManager.STREAM_MUSIC)
private val bufferSizeInBytes = AudioTrack.getMinBufferSize(sampleRate, AudioFormat.CHANNEL_OUT_MONO, AudioFormat.ENCODING_PCM_FLOAT)

val audioTrack = AudioTrack.Builder()
.setAudioAttributes(
AudioAttributes.Builder()
.setUsage(AudioAttributes.USAGE_MEDIA)
.setContentType(AudioAttributes.CONTENT_TYPE_MUSIC)
.build()
)
.setAudioFormat(
AudioFormat.Builder()
.setEncoding(AudioFormat.ENCODING_PCM_FLOAT)
.setSampleRate(sampleRate)
.setChannelMask(AudioFormat.CHANNEL_OUT_MONO)
.build()
)
.setBufferSizeInBytes(bufferSizeInBytes)
.build()

private var audioTrack : AudioTrack? = null
private var updatePeriod = 0
private var writtenFrames = 0
private var buffer = FloatArray(0)

fun play() {
audioTrack?.stop()
audioTrack.stop()

val sampleRate = AudioTrack.getNativeOutputSampleRate(AudioManager.STREAM_MUSIC)
val bufferSizeInBytes = AudioTrack.getMinBufferSize(sampleRate, AudioFormat.CHANNEL_OUT_MONO, AudioFormat.ENCODING_PCM_FLOAT)
val updatePeriodFraction = 2
// Divide by 4 to get buffer size in float
updatePeriod = bufferSizeInBytes / 4 / updatePeriodFraction
buffer = FloatArray(updatePeriod)
writtenFrames = 0

val track = AudioTrack.Builder()
.setAudioAttributes(
AudioAttributes.Builder()
.setUsage(AudioAttributes.USAGE_MEDIA)
.setContentType(AudioAttributes.CONTENT_TYPE_MUSIC)
.build()
)
.setAudioFormat(
AudioFormat.Builder()
.setEncoding(AudioFormat.ENCODING_PCM_FLOAT)
.setSampleRate(sampleRate)
.setChannelMask(AudioFormat.CHANNEL_OUT_MONO)
.build()
)
.setBufferSizeInBytes(bufferSizeInBytes)
.build()

track.setPlaybackPositionUpdateListener(object : AudioTrack.OnPlaybackPositionUpdateListener {
audioTrack.setPlaybackPositionUpdateListener(object : AudioTrack.OnPlaybackPositionUpdateListener {
override fun onMarkerReached(track: AudioTrack?) { }

override fun onPeriodicNotification(track: AudioTrack?) {
writeSineToAudioTrack()
}

})
audioTrack = track
track.flush()
track.play()
track.positionNotificationPeriod = updatePeriod
audioTrack.flush()
audioTrack.play()
audioTrack.positionNotificationPeriod = updatePeriod
// write several update periods to audioTrack to fill the complete buffer, afterwards
// our periodic notification listener will fill the buffer.
for(i in 0 until updatePeriodFraction)
writeSineToAudioTrack()
}

fun stop() {
audioTrack?.stop()
audioTrack = null
audioTrack.stop()
}

private fun writeSineToAudioTrack() {
val track = audioTrack ?: return
val frequency = 220f
val sampleRate = track.sampleRate
val sampleRate = audioTrack.sampleRate
// Log.v("AudioTrackTest", "writtenFrames = $writtenFrames, update period = $updatePeriod")

for (i in buffer.indices) {
val time = (writtenFrames + i).toFloat() / sampleRate.toFloat()
buffer[i] = sin(2f * PI.toFloat() * frequency * time)
}

track.write(buffer, 0, buffer.size, AudioTrack.WRITE_NON_BLOCKING)
audioTrack.write(buffer, 0, buffer.size, AudioTrack.WRITE_NON_BLOCKING)
writtenFrames += buffer.size
}
}
2 changes: 1 addition & 1 deletion app/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
android:text="stop"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/play_button" />
app:layout_constraintTop_toBottomOf="@id/play_button" />

<Button
android:id="@+id/play_button"
Expand Down

0 comments on commit bdcf714

Please sign in to comment.