Play sound on Android

Resource files shall be placed under app\src\main\res\raw

import android.media.AudioManager
import android.media.SoundPool
import android.os.Build
import android.os.Bundle
import android.util.Log
import android.view.View
import androidx.appcompat.app.AppCompatActivity


class MainActivity : AppCompatActivity() {
    private var mCSoundOne: Int = 0
    private lateinit var mSoundPool: SoundPool
    private var mLoaded = false

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

        mSoundPool = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            Log.d("SoundPoolTest", "Building Soundpool")
            SoundPool.Builder().setMaxStreams(1).build()
        } else {
            SoundPool(1, AudioManager.STREAM_MUSIC, 1)
        }
        mSoundPool.setOnLoadCompleteListener(SoundPool.OnLoadCompleteListener { soundPool, sampleId, status ->
            Log.d("SoundPoolTest", "Load Completed")
            mLoaded = true
        })
        Log.d("SoundPoolTest", "Loading wav")
        mCSoundOne = mSoundPool.load(applicationContext, R.raw.eins, 1)

    }

    fun play_one(view: View?) {
        Log.d("SoundPoolTest", "Button Clicked")
        if (mLoaded) {
            Log.d("SoundPoolTest", "Playing wav")
            mSoundPool.play(mCSoundOne, 1.0f, 1.0f, 0, 0, 1.0f)
        }
    }
}

Log4j2 PatternLayout Cheatsheet

In Log4j2 for Kotlin I’ve showed you how to configure a basic logger with log4j2.

If you use a PatternLayout like this

appender.console.layout.type = PatternLayout
appender.console.layout.pattern = %m%n

you can customize the appearance of the output.

eg.

appender.console.layout.pattern = %d{yyyy-mm-dd-HH:mm:ss.SSS} %-4p %C{1}.%M [%t] - %m%n%ex

Parameter

Parameter Meaning
%m The log message
%n line break
%d timestamp
%p priority
%t thread
%C class

Timestamps

 

%d{yyyy-mm-dd-HH:mm:ss.SSS}
2021-50-27-10:50:32.701
Parameter Meaning
yyyy year in four digits
MM

MMM

month in two digits

Name of month three characters wide

dd day
HH hour
mm minutes
ss seconds
SSS milliseconds

Log4j2 for Kotlin

Motivation

Logging is a common good practice in software engineering. It enables you to monitor applications in production to gather information about crashes and other malfunctions for further analysis. It is the “little brother” of debugging and often a precursor for setting up test cases which can lead to reproducing the bugs on the developers machine.

Log4j2 is version 2 of the very famous Apache logging library log4j from the Java ecosystem.

This article describes a minimal working solution for log4j2 with Kotlin and Gradle.

Dependencies

To get started with log4j2 add the following lines to your build.gradle file in the dependency section:

compile "org.apache.logging.log4j:log4j-api-kotlin:1.0.0"
compile "org.apache.logging.log4j:log4j-api:2.11.1"
compile "org.apache.logging.log4j:log4j-core:2.11.1"

Configuration

Continue reading “Log4j2 for Kotlin”

Kotlin Scripts with Gradle

If you are using gradle and you want to run a Kotlin script you have to add the following to your build.gradle file

dependencies {
     implementation "org.jetbrains.kotlin:kotlin-stdlib"
     compile group: 'org.jetbrains.kotlin', name: 'kotlin-script-runtime', version: '1.4.32'
}

Passing data between AndroidActivities

Sending data

private void openVideoActivity(String video) {
    Intent newActivity = new Intent(this, PlayerActivity.class);
    newActivity.putExtra("videoId", video);
    startActivity(newActivity);
}

Retrieving data

final String videoID = getIntent().getExtras().getString("videoID");

In Kotlin it looks like this:

val intent = Intent(this, NewActivity::class.java).apply {
    putExtra("EXTRA_MESSAGE", "Test 123")
}
startActivity(intent)

Retrieving

val message = intent.getStringExtra("EXTRA_MESSAGE")

Android App Lifecycle

Code

class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        Log.d("LifecycleTest", "onCreate")
    }

    override fun onStart() {
        super.onStart()
        Log.d("LifecycleTest", "onStart")
    }

    override fun onResume() {
        super.onResume()
        Log.d("LifecycleTest", "onResume")
    }

    override fun onPause() {
        super.onPause()
        Log.d("LifecycleTest", "onPause")
    }

    override fun onSaveInstanceState(outState: Bundle) {
        super.onSaveInstanceState(outState)
        Log.d("LifecycleTest", "onSaveInstanceState")
    }

    override fun onRestoreInstanceState(savedInstanceState: Bundle) {
        Log.d("LifecycleTest", "onSaveInstanceState")
    }

    override fun onStop() {
        super.onStop()
        Log.d("LifecycleTest", "onStop")
    }

    override fun onDestroy() {
        super.onDestroy()
        Log.d("LifecycleTest", "onDestroy")
    }
}

https://github.com/jboegeholz/LifecycleTest