How to use an AVAudioPlayer in iOS with Swift 5

import AVFoundation

var player: AVAudioPlayer?


let url = Bundle.main.path(forResource: "richtig", ofType: "wav")
do {
    try AVAudioSession.sharedInstance().setMode(.default)
    try AVAudioSession.sharedInstance().setActive(true, options: .notifyOthersOnDeactivation)
    guard let url = url else {
        return
    }
    player = try AVAudioPlayer(contentsOf: URL(fileURLWithPath: url))
} catch {
    print("Something went wrong")
}

@IBAction func playButton(_ sender: Any) {
    guard let player = player else {
    return
    }
    if player.isPlaying {
        player.stop()
    }
    else {
        player.play()
    }
}

Create an iOS 12 compatible app with Xcode 12

To make an app downwards compatible do the follwing

  • Change deployment target to iOS 12
  • Remove the methods in AppDelegate.swift
  • Add var window: UIWindow?
  • Remove SceneDelegate.swift
  • Remove the entry Application Scene Manifest in the info.plist.

Use github with tokens

I got an error accessing github from Android Studio

It seems that github disabled the usage of user/pw authentification.

To fix it you have to

  • Go to https://github.com/settings/tokens
  • Generate new token
  • enable “repo”, “admin:org”, “gist”

Now you can use the token from within Android Studio

Developing for iOS 14 with Xcode 12.4

Oh boy, Apple….

I’m still running macOS 10.15.7 Catalina on my Mac due to different reasons. My iPhone is already up to date on iOS 14.5.

That means my Mac can only run Xcode 12.4 which makes it incompatible with my iPhone for development. But there is a way.

DeviceSupport files

You need an additional DeviceSupport file. Do the following:

  • Close Xcode
  • Go to https://github.com/filsv/iPhoneOSDeviceSupport
  • Download the matching zip in my case 14.5
  • Unzip the archive
  • Copy the folder to
    • /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/DeviceSupport/
  • Start Xcode

 

Generating SHA-1 for Google API

When you want to interface with any Google API e.g. YouTube from within your Android App, you need to register your app in the developer console.

You have to provide a SHA-1 key which can be generated as following:

Command line

$ chmod u+x gradlew
$ ./gradlew signingReport

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)
        }
    }
}

Android: Fix Unauthorized Devices on macOS

  1. Disconnect USB connection between Mac/MacBook and Android device
  2. On the device: Revoke USB debugging authorization in Settings -> Developer Options
  3. cd /Users/<user_name>/Library/Android/sdk/platform-tools
  4. ./adb kill-server
  5. cd ~/.android/
  6. rm -rf adbkey
  7.  Reconnect device
  8. Re-authorize computer to device
  9. cd /Users/<user_name>/Library/Android/sdk/platform-tools
  10. check with ./adb devices that device is authorized

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”