postheadericon MediaPlayer Example in Android



Definition : 17) Create an application that will play a media file from the memory card.


File Name : E17Activity.java


package bsr.exa;

import bsr.exa.R;
import android.app.Activity;
import android.content.Context;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;



public class E17Activity extends Activity {
/**  @author Bipin S Rupadiya , www.bipinrupadiya.com
     *
     *  17) Create an application that will play a media file from the memory card.
     *
     *  */

MediaPlayer player=null;
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        Button start=(Button)findViewById(R.id.button1);
        Button stop=(Button)findViewById(R.id.button2);
        getSystemService(Context.AUDIO_SERVICE);
        start.setOnClickListener(new View.OnClickListener() {
       
@Override
public void onClick(View arg0)
{

try
{
player=null;
player=new MediaPlayer();
String audioFilePath="/sdcard/a.mid";
player.setDataSource(audioFilePath);
player.prepare();
player.start();
}
catch(Exception e)
{
Toast.makeText(E17Activity.this,""+e,Toast.LENGTH_LONG).show();

}
}
});
        stop.setOnClickListener(new View.OnClickListener()
        {
        @Override
public void onClick(View arg0)
        {
        player.stop();
}
});
    }
}

File Name : AndroidManifest.xml


<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      android:versionCode="1"
      android:versionName="1.0" package="bsr.exa">
    <uses-sdk android:minSdkVersion="8" />

    <application android:icon="@drawable/icon" android:label="@string/app_name">
        <activity android:name=".E17Activity"
                  android:label="@string/app_name">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

    </application>
</manifest>


0 comments:

Total Pageviews

© BipinRupadiya.com. Powered by Blogger.