Kotlin Kursu etiketine sahip kayıtlar gösteriliyor. Tüm kayıtları göster
Kotlin Kursu etiketine sahip kayıtlar gösteriliyor. Tüm kayıtları göster

2 Mart 2024 Cumartesi

Kotlin Video Player

İlkin Manifest dosyasına aşağıdaki izinler eklenir


  1. <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>  
  2. <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>  
Ardından layout dosyasına aşağıdaki kod eklenir
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     xmlns:app="http://schemas.android.com/apk/res-auto"  
  4.     xmlns:tools="http://schemas.android.com/tools"  
  5.     android:layout_width="match_parent"  
  6.     android:layout_height="match_parent"  
  7.     tools:context="example.javatpoint.com.kotlinvideoplayer.MainActivity">  
  8.   
  9.     <FrameLayout  
  10.         android:layout_width="match_parent"  
  11.         android:layout_height="wrap_content">  
  12.   
  13.         <VideoView  
  14.             android:id="@+id/videoView"  
  15.             android:layout_width="wrap_content"  
  16.             android:layout_height="308dp"  
  17.             android:layout_marginBottom="0dp"  
  18.             android:layout_marginEnd="0dp"  
  19.             android:layout_marginStart="0dp"  
  20.             app:layout_constraintBottom_toBottomOf="parent"  
  21.             app:layout_constraintEnd_toEndOf="parent"  
  22.             app:layout_constraintHorizontal_bias="0.0"  
  23.             app:layout_constraintStart_toStartOf="parent"  
  24.             app:layout_constraintTop_toTopOf="parent"  
  25.             app:layout_constraintVertical_bias="1.0" />  
  26.     </FrameLayout>  
  27. </android.support.constraint.ConstraintLayout>  



Ardından Main Actity aşağıdaki şekle çevrilir
Üsttte kendi packet isminin altına şunları yapıştır
  1. import android.net.Uri  
  2. import android.net.Uri.*  
  3. import android.support.v7.app.AppCompatActivity  
  4. import android.os.Bundle  
  5. import android.os.Environment  
  6. import android.widget.MediaController  
  7. import android.widget.VideoView  
  8.   
  9. class MainActivity : AppCompatActivity() {  
  10.   
  11.     override fun onCreate(savedInstanceState: Bundle?) {  
  12.         super.onCreate(savedInstanceState)  
  13.         setContentView(R.layout.activity_main)  
  14.   
  15.         val videoView = findViewById<VideoView>(R.id.videoView)  
  16.         //Creating MediaController  
  17.         val mediaController = MediaController(this)  
  18.         mediaController.setAnchorView(videoView)  
  19.         //specify the location of media file  
  20.         val uri:Uri = parse(Environment.getExternalStorageDirectory().getPath() + "/Movies/video.mp4")  
  21.         //Setting MediaController and URI, then starting the videoView  
  22.         videoView.setMediaController(mediaController)  
  23.         videoView.setVideoURI(uri)  
  24.         videoView.requestFocus()  
  25.         videoView.start()  
  26.     }  
  27. }  





27 Şubat 2024 Salı

Kotlin Fotoğrafları Arkaplan Yapma Programı Kodları

```kotlin
import android.os.Bundle
import android.widget.ImageView
import androidx.appcompat.app.AppCompatActivity

class MainActivity : AppCompatActivity() {

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

        val imageView: ImageView = findViewById(R.id.imageView)
        imageView.setImageResource(R.drawable.background_image)
    }
}
```

Output:
The code provided sets a background image from the assets folder in an ImageView in an Android app using Kotlin.

Kotlin Audio Recorder

import android.Manifest
import android.content.pm.PackageManager
import android.media.MediaRecorder
import android.os.Bundle
import android.os.Environment
import android.widget.Button
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import androidx.core.app.ActivityCompat
import androidx.core.content.ContextCompat
import java.io.IOException

class MainActivity : AppCompatActivity() {

    private lateinit var mediaRecorder: MediaRecorder
    private var outputFile: String = ""

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

        val recordButton: Button = findViewById(R.id.record_button)

        if (ContextCompat.checkSelfPermission(this, Manifest.permission.RECORD_AUDIO) != PackageManager.PERMISSION_GRANTED) {
            ActivityCompat.requestPermissions(this, arrayOf(Manifest.permission.RECORD_AUDIO), 1)
        }

        outputFile = Environment.getExternalStorageDirectory().absolutePath + "/recording.3gp"

        mediaRecorder = MediaRecorder()
        mediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC)
        mediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP)
        mediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB)
        mediaRecorder.setOutputFile(outputFile)

        recordButton.setOnClickListener {
            try {
                mediaRecorder.prepare()
                mediaRecorder.start()
                Toast.makeText(this, "Recording started", Toast.LENGTH_SHORT).show()
            } catch (e: IOException) {
                e.printStackTrace()
            }
        }
    }

    override fun onStop() {
        super.onStop()
        mediaRecorder.stop()
        mediaRecorder.release()
        Toast.makeText(this, "Recording stopped", Toast.LENGTH_SHORT).show()
    }
}

23 Şubat 2024 Cuma

Kotlin Audio Recorder Örneği

import android.Manifest
import android.content.pm.PackageManager
import android.media.MediaRecorder
import android.os.Bundle
import android.os.Environment
import android.widget.Button
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import androidx.core.app.ActivityCompat
import androidx.core.content.ContextCompat
import java.io.IOException

class AudioRecorderActivity : AppCompatActivity() {

    private lateinit var mediaRecorder: MediaRecorder
    private var isRecording = false

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

        val recordButton = findViewById<Button>(R.id.record_button)
        recordButton.setOnClickListener {
            if (isRecording) {
                stopRecording()
            } else {
                if (checkPermissions()) {
                    startRecording()
                }
            }
        }
    }

    private fun startRecording() {
        mediaRecorder = MediaRecorder()
        mediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC)
        mediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP)
        mediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB)
        val filePath = Environment.getExternalStorageDirectory().absolutePath + "/recording.3gp"
        mediaRecorder.setOutputFile(filePath)

        try {
            mediaRecorder.prepare()
            mediaRecorder.start()
            isRecording = true
            Toast.makeText(this, "Recording started", Toast.LENGTH_SHORT).show()
        } catch (e: IOException) {
            e.printStackTrace()
        }
    }

    private fun stopRecording() {
        mediaRecorder.stop()
        mediaRecorder.release()
        isRecording = false
        Toast.makeText(this, "Recording stopped", Toast.LENGTH_SHORT).show()
    }

    private fun checkPermissions(): Boolean {
        if (ContextCompat.checkSelfPermission(this, Manifest.permission.RECORD_AUDIO) != PackageManager.PERMISSION_GRANTED) {
            ActivityCompat.requestPermissions(this, arrayOf(Manifest.permission.RECORD_AUDIO), 1)
            return false
        }
        return true
    }

    override fun onRequestPermissionsResult(requestCode: Int, permissions: Array<out String>, grantResults: IntArray) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults)
        if (requestCode == 1 && grantResults.isNotEmpty() && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
            startRecording()
        } else {
            Toast.makeText(this, "Permission denied", Toast.LENGTH_SHORT).show()
        }
    }
}