Playing a audio file on SD card using MediaPlayer Class on Android

When I was working on my Android app I needed some playback features. I found a good example of playing an audio file on the sd card for the Android on a blog the other day. I wish I had the link to this guy’s blog to give him credit but I can not find it now. I think I have updated it enough to call it my own by now. I am still fairly new to Android so it took me a little awhile for it to work. All the code is below just edit a few things below for it to work in your app.

Make sure you you create a layout named “main.xml” with a button with an id of “play.”

 

package com.sound.button;  // your package

import java.io.IOException;
import android.app.Activity;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnPreparedListener;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class SoundButtonActivity extends Activity {  // your class name
    Button button;
    MediaPlayer mp = new MediaPlayer();
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);  
        try {
			audioPlayer("/sdcard/customSound" , "RECORDING0.3gp");  // your file location
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}  //edit this to your liking
        addListenerOnButton();
    }

	public void addListenerOnButton() {
		button = (Button) findViewById(R.id.play);
		button.setOnClickListener(new OnClickListener() {
		@Override
		public void onClick(View arg0) {
		    mp.start();
	        }
		});
	}
	public void audioPlayer(String path, String fileName) throws IOException{		
	    if (mp != null) {
               mp.reset();     
            }

	    try {
	        mp.setDataSource(path+"/"+fileName);
	    } catch (IllegalArgumentException e) {
	        // TODO Auto-generated catch block
	    	//mp.reset();
	        e.printStackTrace();
	    } catch (IllegalStateException e) {
	        // TODO Auto-generated catch block
	        e.printStackTrace();
	    } catch (IOException e) {
	        // TODO Auto-generated catch block
	        e.printStackTrace();
	    }
	    try {
	        mp.prepare();
	    } catch (IllegalStateException e) {
	        // TODO Auto-generated catch block
	        e.printStackTrace();
	    }  
	    mp.setOnPreparedListener(new OnPreparedListener() {
            @Override
            public void onPrepared(MediaPlayer mp) {
               mp.start();
            }                
            });
            }
}

Add this to your manifest:

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

Posted in Coding

Tags:

Permalink 1 Comment

  • UEFI

    very helpful,thanks :)


Graveyard Database Logo

Has everything you want to know about grave yards. Plus has many celebrites and world leaders graves. Pssst it is my other site

Google + Profile
Check out Android Photo Mapping, its a app/site I been working on

  • wholesale nfl jerseys
  • wholesale nfl jerseys china
  • wholesale sports jerseys