Skip to content

Instantly share code, notes, and snippets.

View halilozercan's full-sized avatar
🏛️
Learning

Halil Ozercan halilozercan

🏛️
Learning
View GitHub Profile
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContent {
ComposeTubeTheme {
var source by remember {
mutableStateOf(
"https://commondatastorage.googleapis.com/gtv-videos-bucket/sample/BigBuckBunny.mp4"
)
}
@Composable
fun VideoPlayer() {
// This is the official way to access current context from Composable functions
val context = ContextAmbient.current
// Do not recreate the player everytime this Composable commits
val exoPlayer = remember {
SimpleExoPlayer.Builder(context).build().apply {
val dataSourceFactory: DataSource.Factory = DefaultDataSourceFactory(context,
Util.getUserAgent(context, context.packageName))
@Composable
fun VideoPlayer() {
// This is the official way to access current context from Composable functions
val context = LocalContext.current
// Do not recreate the player everytime this Composable commits
val exoPlayer = remember(context) {
SimpleExoPlayer.Builder(context).build().apply {
val dataSourceFactory: DataSource.Factory = DefaultDataSourceFactory(context,
Util.getUserAgent(context, context.packageName))
// Simply put the Demo composable in a Compose tree and observe the logs
// Focus on how dependency changes affect the recomposition.
// How does Ambient work?
// When basic callbacks fire: onCommit, launchInComposition, onActive, remember
@Composable
fun Demo() {
var textCounter by mutableStateOf(0)
Column {
# A shell script to launch scrcpy automatically when a device is connected
# Device name selection is based on lines from `adb devices -l`.
# Script takes a single argument to match any of these lines.
# First matched devices' serial number is used for scrcpy device serial number.
while true
do
# Try to start scrcpy
scrcpy -s $(adb devices -l | awk -v j=$1 'index($0, j);' | awk '{print $1}')
# If scrcpy was closed by user, do not restart until device is replugged in.
fun updateAudioDeviceState() {
// Check if any Bluetooth headset is connected. The internal BT state will
// change accordingly.
if (bluetoothManager.state == State.HEADSET_AVAILABLE || bluetoothManager.state == State.HEADSET_UNAVAILABLE || bluetoothManager.state == State.SCO_DISCONNECTING) {
bluetoothManager.updateDevice()
}
// Update the set of available audio devices.
val newAudioDevices: MutableSet<AudioDevice> =
HashSet()
private inner class BluetoothServiceListener : BluetoothProfile.ServiceListener {
// Called to notify the client when the proxy object has been connected to the service.
// Once we have the profile proxy object, we can use it to monitor the state of the
// connection and perform other operations that are relevant to the headset profile.
override fun onServiceConnected(profile: Int, proxy: BluetoothProfile) {
if (profile != BluetoothProfile.HEADSET || bluetoothState == State.UNINITIALIZED) {
return
}
// Android only supports one connected Bluetooth Headset at a time.
bluetoothHeadset = proxy as BluetoothHeadset
typealias OnAudioDeviceChanged = (
selectedAudioDevice: AudioGenie.AudioDevice?,
availableAudioDevices: Set<AudioGenie.AudioDevice>
) -> Unit
val newAudioDevices: MutableSet<AudioDevice> = HashSet()
if (bluetoothManager.state == State.SCO_CONNECTED || bluetoothManager.state == State.SCO_CONNECTING || bluetoothManager.state == State.HEADSET_AVAILABLE
) {
newAudioDevices.add(AudioDevice.BLUETOOTH)
}
override fun onPictureInPictureModeChanged(
isInPictureInPictureMode: Boolean,
newConfig: Configuration
) {
if (isInPictureInPictureMode) {
// entered PiP
} else {
// keep track of background in onStart and onStop
if (inBackground) {
// User highly probably finished the activity in PiP.
private fun startPictureInPicture(): Boolean {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O && shouldStart) {
enterPictureInPictureMode(
PictureInPictureParams.Builder()
.setAspectRatio(…) // This has lower and upper limitations
.build()
)
return true
}
return false