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

Audio App

The document contains an XML layout for an Android audio player application with two buttons: 'Play' and 'Stop'. The Java code initializes a MediaPlayer to play a sound file when the 'Play' button is clicked and stops playback when the 'Stop' button is pressed. It also handles resource cleanup in the onDestroy method to release the MediaPlayer resources.

Uploaded by

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

Audio App

The document contains an XML layout for an Android audio player application with two buttons: 'Play' and 'Stop'. The Java code initializes a MediaPlayer to play a sound file when the 'Play' button is clicked and stops playback when the 'Stop' button is pressed. It also handles resource cleanup in the onDestroy method to release the MediaPlayer resources.

Uploaded by

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

//XML FILE

<?xml version="1.0" encoding="utf-8"?>


<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">

<Button
android:id="@+id/playButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Play"
android:layout_centerHorizontal="true"
android:layout_marginTop="100dp"/>

<Button
android:id="@+id/stopButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Stop"
android:layout_below="@id/playButton"
android:layout_marginTop="20dp"
android:layout_centerHorizontal="true"/>
</RelativeLayout>

//Java File

package com.example.audioplayer;

import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {

private MediaPlayer mediaPlayer;


private Button playButton;
private Button stopButton;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

playButton = findViewById(R.id.playButton);
stopButton = findViewById(R.id.stopButton);

// Initialize MediaPlayer
mediaPlayer = MediaPlayer.create(this, R.raw.sound);

playButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (!mediaPlayer.isPlaying()) {
mediaPlayer.start();
}
}
});
stopButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (mediaPlayer.isPlaying()) {
mediaPlayer.stop();
mediaPlayer.prepareAsync(); // Prepare for the next play
}
}
});
}

@Override
protected void onDestroy() {
super.onDestroy();
if (mediaPlayer != null) {
mediaPlayer.release();
mediaPlayer = null;
}
}
}

You might also like