An application that will play a media file from the memory card

Hey Buddy, I,m here with another new app source code to create an application that will play a media file from the memory card.

Steps Description
1 create a project in android studio with any name.
2 Modify src/MainActivity.java and add the code
3 Modify layout XML file res/layout/activity_main.xml
4 change the permission in manifests file

Read Also –> Login Application for validating EmailID and Password

Content of src/MainActivity.java 

package com.example.module41;

import android.Manifest;
import android.content.pm.PackageManager;
import android.media.MediaPlayer;
import android.os.Environment;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class MainActivity extends AppCompatActivity {


private Button start,pause;

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




start=(Button)findViewById(R.id.button1);
pause=(Button)findViewById(R.id.button2);

final MediaPlayer mp=new MediaPlayer();
try{

mp.setDataSource(Environment.getExternalStorageDirectory().getPath()+"/Iphone.mp3");

mp.prepare();
}catch(Exception e){e.printStackTrace();}

start.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mp.start();
}
});
pause.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mp.pause();
}
});
}
}

Content of res/layout/activity_main.xml

<?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">

<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_marginTop="30dp"
android:text="Audio Controller" />

<Button
android:id="@+id/button1"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/textView1"
android:layout_below="@+id/textView1"
android:layout_marginTop="48dp"
android:text="start" />

<Button
android:id="@+id/button2"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="@+id/button1"
android:layout_marginLeft="154dp"
android:layout_marginTop="-3dp"
android:layout_toRightOf="@+id/button1"
android:text="pause" />


</RelativeLayout>

Edit your manifest file for permission to Read Storage

<uses-permission android:name=”android.permission.READ_EXTERNAL_STORAGE” />

Add this line before the start of <application>

Output:

Stay Connect with our app: –¬†

https://play.google.com/store/apps/details?id=com.edu.easyengineer

For Assignment Questions and Explanation of a theory topic visit:

http://mycandal.com/mobile-application-devlopmentseit2021/

About easyengineering 38 Articles
Easyengineering.in provides you to all subject and exam related materials online like GPSC, UPSC, IES, GATE, etc. As well as we provide daily job notification, some life-related books and other online courses which are useful too in your study.

Be the first to comment

Leave a Reply

Your email address will not be published.


*