Search This Blog

Showing posts with label Runnable. Show all posts
Showing posts with label Runnable. Show all posts

Saturday, March 30, 2013

Streaming songs from Url in Default Media Player

Here i am posting with the example code of the streaming the songs from the Url in android with android default media player. 

First of all you just have to make the XML file with the following lines

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 
xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent" 
android:focusable="true">
<EditText 
android:text="The song URL" 
android:layout_width="fill_parent" 
android:layout_height="wrap_content" 
android:id="@+id/EditTextSongURL"/>
<ImageButton 
android:layout_below="@+id/EditTextSongURL"
android:id="@+id/ButtonTestPlayPause" 
android:layout_height="wrap_content" 
android:layout_width="match_parent"
android:src="@drawable/button_play"/>
<SeekBar 
android:layout_below="@+id/ButtonTestPlayPause"
android:id="@+id/SeekBarTestPlay" 
android:layout_height="wrap_content" 
android:layout_width="match_parent"
android:thumb="@drawable/thumb_transparent"/>
</RelativeLayout>

Now in you main Activity class replace with the following code and enter the URL of your server song and hit play.

public class StreamingPlayer extends Activity implements OnClickListener, OnTouchListener, OnCompletionListener, OnBufferingUpdateListener{

private ImageButton buttonPlayPause;
private SeekBar seekBarProgress;
public EditText editTextSongURL;

private MediaPlayer mediaPlayer;
// this value contains the song duration in milliseconds. Loook at getDuration() method in MediaPlayer class
private int mediaFileLengthInMilliseconds; 

private final Handler handler = new Handler();

** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
             super.onCreate(savedInstanceState);
             setContentView(R.layout.main);
             initView();
}

/** This method initialise all the views in project*/
private void initView() {
             buttonPlayPause = (ImageButton)findViewById(R.id.ButtonTestPlayPause);
             buttonPlayPause.setOnClickListener(this);

             seekBarProgress = (SeekBar)findViewById(R.id.SeekBarTestPlay);
             seekBarProgress.setMax(99); // It means 100% .0-99
             seekBarProgress.setOnTouchListener(this);
             editTextSongURL = (EditText)findViewById(R.id.EditTextSongURL);
             editTextSongURL.setText(R.string.testsong_20_sec);

             mediaPlayer = new MediaPlayer();
             mediaPlayer.setOnBufferingUpdateListener(this);
             mediaPlayer.setOnCompletionListener(this);
}

/** Method which updates the SeekBar primary progress by current song playing position*/
private void primarySeekBarProgressUpdater() {
              seekBarProgress.setProgress((int)                                     (((float)mediaPlayer.getCurrentPosition()/mediaFileLengthInMilliseconds)*100)); // This math construction give a percentage of "was playing"/"song length"
                     if (mediaPlayer.isPlaying()) {
                                     Runnable notification = new Runnable() {
                                                         public void run() {
                                                                    primarySeekBarProgressUpdater();
                                                         }
                                     };
                    handler.postDelayed(notification,1000);
       }
}

@Override
public void onClick(View v) {

           if(v.getId() == R.id.ButtonTestPlayPause){
/** ImageButton onClick event handler. Method which start/pause mediaplayer playing */
             try {
                   // setup song URL to mediaplayer data source
                  mediaPlayer.setDataSource(editTextSongURL.getText().toString());
                  mediaPlayer.prepare();
                 } catch (Exception e) {
                  e.printStackTrace();
               }

mediaFileLengthInMilliseconds = mediaPlayer.getDuration();

           if(!mediaPlayer.isPlaying()){
                mediaPlayer.start();
                buttonPlayPause.setImageResource(R.drawable.button_pause);
           }else {
                mediaPlayer.pause();
                buttonPlayPause.setImageResource(R.drawable.button_play);
          }

          primarySeekBarProgressUpdater();
     }
}

@Override
public boolean onTouch(View v, MotionEvent event) {
           if(v.getId() == R.id.SeekBarTestPlay){
             /** Seekbar onTouch event handler. Method which seeks MediaPlayer to seekBar        primary progress position*/
                    if(mediaPlayer.isPlaying()){
                          SeekBar sb = (SeekBar)v;
                          int playPositionInMillisecconds = (mediaFileLengthInMilliseconds / 100) * sb.getProgress();
                         mediaPlayer.seekTo(playPositionInMillisecconds);
                    }
               }
          return false;
     }


@Override
public void onCompletion(MediaPlayer mp) {
       /** MediaPlayer onCompletion event handler. Method which calls then song playing is complete*/
           buttonPlayPause.setImageResource(R.drawable.button_play);
}

@Override
public void onBufferingUpdate(MediaPlayer mp, int percent) {
/** Method which updates the SeekBar secondary progress by current song loading from URL position*/
             seekBarProgress.setSecondaryProgress(percent);
      }
}

Monday, February 4, 2013

ROTATE MULTIPLE IMAGES IN SINGLE IMAGE VIEW LIKE A VIDEO

First of all add all the images in drawable no metter how big it is.

import android.app.Activity;
import android.content.Context;
import android.content.res.TypedArray;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Gallery;
import android.widget.ImageView;

public class MainActivity extends Activity {

                  ImageView image1;
                  Gallery gallery;
                  private Runnable r;
                  private static int i = 0;
                  int[] image_array = { R.drawable.i_pad0000, R.drawable.i_pad0001,
                                       R.drawable.i_pad0002, R.drawable.i_pad0003, R.drawable.i_pad0004,
                                      R.drawable.i_pad0005, R.drawable.i_pad0006, R.drawable.i_pad0007,
                                      R.drawable.i_pad0008, R.drawable.i_pad0009, R.drawable.i_pad0010,
                                      R.drawable.i_pad0011, R.drawable.i_pad0012, R.drawable.i_pad0013,
                                      R.drawable.i_pad0014, R.drawable.i_pad0015, R.drawable.i_pad0016,
                                      R.drawable.i_pad0017, R.drawable.i_pad0018, R.drawable.i_pad0019,
                                      R.drawable.i_pad0020, R.drawable.i_pad0021, R.drawable.i_pad0022,
                                      R.drawable.i_pad0023, R.drawable.i_pad0024, R.drawable.i_pad0025,
                                      R.drawable.i_pad0027, R.drawable.i_pad0028, R.drawable.i_pad0029,
                                      R.drawable.i_pad0030, R.drawable.i_pad0031, R.drawable.i_pad0032,
                                      R.drawable.i_pad0033, R.drawable.i_pad0034, R.drawable.i_pad0035,
                                      R.drawable.i_pad0036, R.drawable.i_pad0037, R.drawable.i_pad0038,
                                      R.drawable.i_pad0039, R.drawable.i_pad0040, R.drawable.i_pad0041,
                                      R.drawable.i_pad0042, R.drawable.i_pad0043, R.drawable.i_pad0044,
                                      R.drawable.i_pad0045, R.drawable.i_pad0046, R.drawable.i_pad0047,
                                      R.drawable.i_pad0048, R.drawable.i_pad0049, R.drawable.i_pad0050,
                                      R.drawable.i_pad0051, R.drawable.i_pad0052, R.drawable.i_pad0053,
                                      R.drawable.i_pad0054, R.drawable.i_pad0055, R.drawable.i_pad0056,
                                      R.drawable.i_pad0057, R.drawable.ipad0058, R.drawable.i_pad0059,
                                      R.drawable.i_pad0060, R.drawable.i_pad0061, R.drawable.i_pad0062,
                                      R.drawable.i_pad0063, R.drawable.i_pad0064, R.drawable.i_pad0065,
                                      R.drawable.i_pad0066, R.drawable.i_pad0067, R.drawable.i_pad0068,
                                      R.drawable.i_pad0069, R.drawable.i_pad0070, R.drawable.i_pad0071,
                                      R.drawable.i_pad0072, R.drawable.i pad0073, R.drawable.i_pad0074};

                  int[] verse_titles_hindi = { R.drawable.img_1,
                                     R.drawable.img_2, R.drawable.img_3,
                                     R.drawable.img_4, R.drawable.img_5,
                                     R.drawable.img_6, R.drawable.v_7,
                                     R.drawable.img_8, R.drawable.img_9,
                                     R.drawable.img_10, R.drawable.img_11,
                                     R.drawable.img_12, R.drawable.img_13,
                                     R.drawable.img_14, R.drawable.img_15,
                                     R.drawable.img_16, R.drawable.img_17,
                                     R.drawable.img_18,  R.drawable.img_19,
                                     R.drawable.img_20, R.drawable.img_21,
                                     R.drawable.img_22, R.drawable.img_23,
                                     R.drawable.img_24, R.drawable.img_25,
                                     R.drawable.img_26, R.drawable.img_27,
                                     R.drawable.img_28, R.drawable.img_29,
                                     R.drawable.img_30, R.drawable.img_31,
                                     R.drawable.img_32, R.drawable.img_33,
                                     R.drawable.img_34, R.drawable.img_35,
                                     R.drawable.img_36, R.drawable.img_37,
                                     R.drawable.img_38, R.drawable.img_39,
                                     R.drawable.img_40, R.drawable.img_41,
                                     R.drawable.img_42, R.drawable.img_43 };

            @SuppressWarnings("deprecation")
            @Override
            protected void onCreate(Bundle savedInstanceState) {
                             super.onCreate(savedInstanceState);
                             setContentView(R.layout.activity_main);
                             image1 = (ImageView) findViewById(R.id.image);
                             gallery = (Gallery) findViewById(R.id.versesIndex);
                             gallery.setAdapter(new ImageAdapter(this, verse_titles_hindi));

                             r = new Runnable() {
                                         public void run() {
                                                      image1.setImageResource(image_array[i]);
                                                      System.gc();
                                                      i++;
                                                      image1.postDelayed(r, 25);
                                                      if (i >= image_array.length) {
                                                                   Log.i("log_tag", "" + i);
                                                                   i = 0;
                                                                  // image1.removeCallbacks(r);
                                                      }
                                           } 
                              };
              image1.postDelayed(r, 25);
           }

           private class ImageAdapter extends BaseAdapter {
                                   int mGalleryItemBackground;
                                   private Context mContext;
                                   private int[] verse_title;

                                   public ImageAdapter(Context c, int[] verse_title) {
                                   // TODO Auto-generated constructor stub
                                                         this.verse_title = verse_title;
                                                         this.mContext = c;
                                                         TypedArray a = obtainStyledAttributes(R.styleable.Theme);
                                                         mGalleryItemBackground = a.getResourceId(
                                                            R.styleable.Theme_android_galleryItemBackground, 0);
                                                         a.recycle();
                                    }  

                                   @Override
                                   public int getCount() {
                                    // TODO Auto-generated method stub
                                                       return verse_title.length;
                                    } 

                                   @Override
                                   public Object getItem(int position) {
                                    // TODO Auto-generated method stub
                                                        return position;
                                    }

                                   @Override
                                    public long getItemId(int position) {
                                    // TODO Auto-generated method stub
                                                            return position; 
                                    }

                                    @SuppressWarnings("deprecation")
                                    @Override
                                    public View getView(int position, View convertView, ViewGroup parent) {
                                    // TODO Auto-generated method stub
                                                        ImageView i = new ImageView(mContext);
                                                        i.setImageResource(verse_title[position]);
                                                        i.setLayoutParams(new Gallery.LayoutParams(353, 159));
                                                        i.setScaleType(ImageView.ScaleType.FIT_XY);
                                                        i.setBackgroundResource(mGalleryItemBackground);
                                                        return i;
                                     }

               }
}

This would be run with the gallery as well as multiple image in one image view.