You are on page 1of 6

Name: Aryan Kadu Roll no.

30

PRACTICAL 11
A :- Programming Media API.
a) Defining a new directory.

Go to app → res → new →Directory →raw

Add mp3 file to raw.

1
Name: Aryan Kadu Roll no. 30

Now go to activity_main.xml and type the following code:

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


<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginTop="8dp"
android:text="Start"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"/>

</androidx.constraintlayout.widget.ConstraintLayout>

Now go to MainActivity.java and type the following code:


package com.example.amp11;

import androidx.appcompat.app.AppCompatActivity;

import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {


MediaPlayer mediaPlayer;
Button bn;

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

bn=(Button)findViewById(R.id.button);
bn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {

mediaPlayer=MediaPlayer.create(getApplicationContext(),R.raw.naruto);
mediaPlayer.start();
bn.setEnabled(false);
Toast.makeText(getApplicationContext(), "Playing Music",
Toast.LENGTH_SHORT).show();

2
Name: Aryan Kadu Roll no. 30

mediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener()
{
@Override
public void onCompletion(MediaPlayer mp) {
mediaPlayer.release();
mediaPlayer = null;
Toast.makeText(getApplicationContext(), "Playing Done",
Toast.LENGTH_SHORT).show();
bn.setEnabled(true);
}
});
}
});
}
}

OUTPUT:

3
Name: Aryan Kadu Roll no. 30

11B :- Programming Media API.


Now go to activity_main.xml and type the following code:

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


<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">

<Button
android:id="@+id/buttonCall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="call 5556"
tools:ignore="MissingConstraints" />

</RelativeLayout>

Add permission in Androidmanifest.xml file


<uses-permission android:name="android.permission.CALL_PHONE"/>

4
Name: Aryan Kadu Roll no. 30

Now go to MainActivity.java and type the following code:

package com.example.amp11;

import static android.Manifest.permission.CALL_PHONE;


import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {


private Button button,button2,button3;

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

button = (Button) findViewById(R.id.buttonCall);

button.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0) {
Intent callIntent = new Intent(Intent.ACTION_CALL);

callIntent.setData(Uri.parse("tel:+5556"));

if (ActivityCompat.checkSelfPermission(MainActivity.this,
CALL_PHONE) != PackageManager.PERMISSION_GRANTED) {
return;
}
startActivity(callIntent);

}
});

5
Name: Aryan Kadu Roll no. 30

Output:-

You might also like