Structure Query Language, C programming, Java, Servlet, Jsp, Unix

Thursday, 18 October 2012

Create an application that will play a media file from the memory card.

PRO17Activity.java
Note :  Drop Audios on Sdcard then go to menus open "Dev-Tools" -> "Media Scanner"
package vc.PRO17;

import android.app.Activity;
import android.database.Cursor;
import android.graphics.Color;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.provider.MediaStore.Audio.Media;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.Toast;

public class PRO17Activity extends Activity implements OnClickListener, OnItemClickListener {
    /**
     *  www.master-gtu.blogspot.com
     *  pankaj sharma(8460479175),
     *  chavda vijay(8460420769) 
     */
 Button btnpre,btnstart,btnnext;
 String song[];
 MediaPlayer player;
 ListView lv;
 boolean flag=false;
 static int j=0;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        btnpre=(Button) findViewById(R.id.btnpre);
        btnstart=(Button) findViewById(R.id.btnstart);
        btnnext=(Button) findViewById(R.id.btnnext);
        lv=(ListView) findViewById(R.id.list);
        
        btnpre.setOnClickListener(this);
        btnstart.setOnClickListener(this);
        btnnext.setOnClickListener(this);
        lv.setOnItemClickListener(this);
        btnpre.setTextColor(Color.GREEN);
        btnstart.setTextColor(Color.GREEN);
        btnnext.setTextColor(Color.GREEN);
        try
        {
         Cursor cur=managedQuery(Media.EXTERNAL_CONTENT_URI, null, null, null, null);
         song=new String[cur.getCount()];
         int i=0;
         while(cur.moveToNext())
         {
          song[i]=cur.getString(1).substring(12);
          i++;
         }
         ArrayAdapter aa=new ArrayAdapter(this,android.R.layout.simple_list_item_1,song);
         lv.setAdapter(aa);
        }
        catch(Exception e)
        {
         Toast.makeText(this,e.toString(),1).show();
        }
    }
    public void song_play()
    {
     try
     {
      if(flag==true)
       player.stop();
      player=new MediaPlayer();
   player.setDataSource("/sdcard/"+song[j]);
      player.prepare();
   player.start();
   flag=true;
  }
     catch(Exception e)
  {
   Toast.makeText(this,"Play"+j, 1).show();
  }
    }
 @Override
 public void onClick(View v) {
  // TODO Auto-generated method stub
  Button action =(Button) v;
  if(v.getId()==btnpre.getId())
  {
   if(j>0)
   {
    j--;
    song_play();
   }
   else
   {
    song_play();
   }
   
  }
  else if(action.getText().toString().equals("|>"))
  {
   song_play();
   btnstart.setBackgroundResource(R.drawable.pause);
   btnstart.setText("||");
  }
  else if(action.getText().toString().equals("||"))
  {
   player.stop();
   btnstart.setBackgroundResource(R.drawable.play);
   btnstart.setText("|>");
  }
  else if(v.getId()==btnnext.getId())
  {
   if(song.length-1>j)
   {
    j++;
    song_play();
   }
   else
   {
    song_play();
   }
  }
 }
 @Override
 public void onItemClick(AdapterView arg0, View arg1, int arg2, long arg3) {
  // TODO Auto-generated method stub
  j=arg2;
  song_play();
  btnstart.setText("||");
  btnstart.setBackgroundResource(R.drawable.pause);
 }
}

2 comments: