Skip to content

Instantly share code, notes, and snippets.

View halilozercan's full-sized avatar
🏛️
Learning

Halil Ozercan halilozercan

🏛️
Learning
View GitHub Profile
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()
# 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.
// 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 {
@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))
@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))
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(sourceUrl: String) {
// 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 {
SimpleExoPlayer.Builder(context).build()
}
class PlayerController(
private val context: Context
) {
private val exoPlayer by lazy {
SimpleExoPlayer.Builder(context).build()
}
fun prepare(sourceUrl: String) {
val dataSourceFactory: DataSource.Factory = DefaultDataSourceFactory(context,
@Composable
fun PlayerSurface() {
val playerController = PlayerControllerAmbient.current
AndroidView(resId = R.layout.surface) {
val exoPlayerView = it.findViewById<PlayerView>(R.id.player_view)
playerController.setPlayerView(exoPlayerView)
}
}
@Composable
fun MediaControlButtons(modifier: Modifier = Modifier) {
val controller = PlayerControllerAmbient.current
Stack(modifier = Modifier + modifier) {
Box(modifier = Modifier.gravity(Alignment.Center).fillMaxSize().clickable(indication = null) {
controller.setControlsVisible(false)
})
PlayPauseButton(modifier = Modifier.gravity(Alignment.Center))