0% found this document useful (0 votes)
3 views2 pages

Multimedia

The document contains an XML layout for an Android application featuring a LinearLayout with an ImageView and two buttons for playing and stopping audio. The MainActivity.kt file initializes a MediaPlayer to play an audio file and manages the play and stop functionality through button clicks. It also ensures proper resource management by releasing the MediaPlayer when the activity is destroyed.

Uploaded by

student -1
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views2 pages

Multimedia

The document contains an XML layout for an Android application featuring a LinearLayout with an ImageView and two buttons for playing and stopping audio. The MainActivity.kt file initializes a MediaPlayer to play an audio file and manages the play and stop functionality through button clicks. It also ensures proper resource management by releasing the MediaPlayer when the activity is destroyed.

Uploaded by

student -1
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

Xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="center">

<ImageView
android:id="@+id/imageView"
android:layout_width="200dp"
android:layout_height="200dp"
android:src="@drawable/ic_launcher_background"/>

<Button
android:id="@+id/playButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Play Audio"/>

<Button
android:id="@+id/stopButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Stop Audio"/>
</LinearLayout>

Mainactivity.kt
import android.media.MediaPlayer
import android.os.Bundle
import android.widget.Button
import android.widget.ImageView
import androidx.appcompat.app.AppCompatActivity

class MainActivity : AppCompatActivity() {


private lateinit var mediaPlayer: MediaPlayer

override fun onCreate(savedInstanceState: Bundle?) {


super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)

val playButton: Button = findViewById(R.id.playButton)


val stopButton: Button = findViewById(R.id.stopButton)
val imageView: ImageView = findViewById(R.id.imageView)

// Set an image to ImageView


imageView.setImageResource(R.drawable.ic_launcher_background)

// Initialize MediaPlayer with an audio file from res/raw


mediaPlayer = MediaPlayer.create(this, R.raw.songs)

playButton.setOnClickListener {
if (!mediaPlayer.isPlaying) {
mediaPlayer.start()
}
}

stopButton.setOnClickListener {
if (mediaPlayer.isPlaying) {
mediaPlayer.pause()
mediaPlayer.seekTo(0)
}
}
}

override fun onDestroy() {


super.onDestroy()
mediaPlayer.release()
}
}

You might also like