android play audio file from sdcard

      No Comments on android play audio file from sdcard

android play audio file from sdcard

Android Program 17 Create an application that will play a media file from the memory card

Android Program 17

Its time to develop small media player, Here using this program I have explained how you can play .mp3 file sdcard and many other options of media player. using his code example you can develop complete media player for your use.

Don’t forget to copy media1.mp3 file into sdcard.


Location : res\layout

FileName: activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#fce4ec"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="gtu.android.prog17.MainActivity" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textColor="#e91e63"
        android:text="Play media file from memory card"
        android:textAppearance="?android:attr/textAppearanceLarge" />

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:layout_marginBottom="52dp"
        android:textColor="#9e9e9e"
        android:text="Developed By: Adarsh Patel"
        android:textAppearance="?android:attr/textAppearanceLarge" />

    <TextView
        android:id="@+id/textView3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:text=""
        android:textColor="#d81b60"
        android:textAppearance="?android:attr/textAppearanceLarge" />

</RelativeLayout>

 

Location : src\package\

FileName : MainActivity.java

package gtu.android.prog17;

/*
 *  Android Program 17 Create an application that will play a media file from the memory card
 * 
 */
import android.app.Activity;
import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;

public class MainActivity extends Activity {

	MediaPlayer mp1;
	TextView tv3;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		
		tv3 = (TextView) findViewById(R.id.textView3);
		
		tv3.setText("Playing : " + Environment.getExternalStorageDirectory().getPath()+ "/media1.mp3" );
		
		mp1 = MediaPlayer.create(this, Uri.parse(Environment.getExternalStorageDirectory().getPath()+ "/media1.mp3"));
		mp1.setLooping(true);
		mp1.start();
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// Handle action bar item clicks here. The action bar will
		// automatically handle clicks on the Home/Up button, so long
		// as you specify a parent activity in AndroidManifest.xml.
		int id = item.getItemId();
		if (id == R.id.action_settings) {
			return true;
		}
		return super.onOptionsItemSelected(item);
	}
}

 

Output

Android Program 17

 

Leave a Reply