Search This Blog

Showing posts with label Seekbar. Show all posts
Showing posts with label Seekbar. 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, January 7, 2013

LISTVIEW gets refresh and change the value while scroling issue(SOLVED)



First of all main.xml file will be like below:


<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"
    tools:context=".MainActivity" >
    <ListView
        android:id="@+id/list"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        
         />
</RelativeLayout>

Row file for the inflator is as below.



<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >
    <SeekBar
        android:id="@+id/seekBar1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" 
        android:max="100"/>
    <LinearLayout android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="horizontal">
        <Button
            android:id="@+id/button1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="click to see row number" />
        <Button
            android:id="@+id/button2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="0" />
    </LinearLayout>
</LinearLayout>




Now the class in the activity should be like this.... See the getview method for the logic of not refreshing the list view after scrolling :


public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

ListView lv = (ListView) findViewById(R.id.list);
lv.setAdapter(new ListAdapter(MainActivity.this));
}

private class ListAdapter extends BaseAdapter {
private Context con;
private int size = 20;
private int s[] = new int[20];

public ListAdapter(MainActivity mainActivity) {
// TODO Auto-generated constructor stub
this.con = mainActivity;
for (int i = 0; i < size; i++) {
s[i] = 0;
}

}

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

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

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

@Override
public View getView(final int position, View convertView,ViewGroup parent) {
// TODO Auto-generated method stub
LayoutInflater inflater = LayoutInflater.from(this.con);
View View = inflater.inflate(R.layout.list_row, null);
SeekBar seekbar = (SeekBar) View.findViewById(R.id.seekBar1);
Button btnrow = (Button) View.findViewById(R.id.button1);
final Button btnseek = (Button) View.findViewById(R.id.button2);

seekbar.setProgress(s[position]);
btnseek.setText("" + s[position]);

seekbar.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {

@Override
public void onStopTrackingTouch(SeekBar seekBar) {
}

@Override
public void onStartTrackingTouch(SeekBar seekBar) {
}

@Override
public void onProgressChanged(SeekBar seekBar, int progress,
boolean fromUser) {
// TODO Auto-generated method stub
btnseek.setText("" + progress);
s[position] = progress;

}
});

return View;
}

}

Tuesday, February 14, 2012

Color Picker Dialog With Seekbar

This color picker dialog has sliders (SeekBars) and  EditTexts for red, green, blue and alpha values. It has a live preview of the color. It returns int values for R, G, B, A for easy use with Color.argb().

Make an .xml file main.xml and put the code below.


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 
xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" 
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >
    <ImageView
android:id="@+id/color"
android:layout_width="fill_parent"
android:layout_height="60dip" />
    <Button 
    android:id="@+id/select"
    android:text="Select Color" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" />
</LinearLayout>


Now make an another .xml file named color_layout.xml and put the code below.



<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
    android:layout_height="wrap_content" 
    android:id="@+id/main"
    android:orientation="vertical"
    android:paddingLeft="10dp"
    android:paddingRight="10dp"
    android:minWidth="300dp">
    
    
<TextView
android:id="@+id/red_label"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:text="red" />
<LinearLayout
android:layout_width="fill_parent"
    android:layout_height="wrap_content" 
    android:orientation="horizontal"
    android:gravity="center_vertical|left">
<SeekBar 
  android:id="@+id/red_bar"
  android:layout_width="fill_parent"
  android:layout_height="wrap_content"
  android:max="255"
  android:layout_weight="1"
  android:paddingLeft="7dp"
  android:paddingRight="7dp" />
<EditText
android:id="@+id/red_text"
android:layout_width="60dp"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:lines="1"
android:gravity="center_vertical|right"
android:maxLength="3"
android:inputType="number"
android:text="0"  />
</LinearLayout>


<TextView
android:id="@+id/green_label"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:text="green" />
<LinearLayout
android:layout_width="fill_parent"
    android:layout_height="wrap_content" 
    android:orientation="horizontal"
    android:gravity="center_vertical|left">
<SeekBar 
  android:id="@+id/green_bar"
  android:layout_width="fill_parent"
  android:layout_height="wrap_content"
  android:max="255"
  android:layout_weight="1"
  android:paddingLeft="7dp"
  android:paddingRight="7dp" />
<EditText
android:id="@+id/green_text"
android:layout_width="60dp"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:lines="1"
android:gravity="center_vertical|right"
android:maxLength="3"
android:inputType="number"
android:text="0"  />
</LinearLayout>


<TextView
android:id="@+id/blue_label"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:text="blue" />
<LinearLayout
android:layout_width="fill_parent"
    android:layout_height="wrap_content" 
    android:orientation="horizontal"
    android:gravity="center_vertical|left">
<SeekBar 
  android:id="@+id/blue_bar"
  android:layout_width="fill_parent"
  android:layout_height="wrap_content"
  android:max="255"
  android:layout_weight="1"
  android:paddingLeft="7dp"
  android:paddingRight="7dp" />
<EditText
android:id="@+id/blue_text"
android:layout_width="60dp"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:lines="1"
android:gravity="center_vertical|right"
android:maxLength="3"
android:inputType="number"
android:text="0"  />
</LinearLayout>


<TextView
android:id="@+id/alpha_label"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:text="alpha" />
<LinearLayout
android:layout_width="fill_parent"
    android:layout_height="wrap_content" 
    android:orientation="horizontal"
    android:gravity="center_vertical|left">
<SeekBar 
  android:id="@+id/alpha_bar"
  android:layout_width="fill_parent"
  android:layout_height="wrap_content"
  android:max="255"
  android:progress="255"
  android:layout_weight="1"
  android:paddingLeft="7dp"
  android:paddingRight="7dp" />
<EditText
android:id="@+id/alpha_text"
android:layout_width="60dp"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:lines="1"
android:gravity="center_vertical|right"
android:maxLength="3"
android:inputType="number" 
android:text="255" 
/>
</LinearLayout>


<ImageView
android:id="@+id/color_preview"
android:layout_width="fill_parent"
android:layout_height="40dip" />

<LinearLayout
android:layout_width="fill_parent"
    android:layout_height="wrap_content" 
    android:orientation="horizontal"
    android:gravity="center_vertical|center_horizontal"
    android:layout_marginTop="20dp">
<Button 
  android:id="@+id/ok"
  android:layout_width="100dp"
  android:layout_height="wrap_content"
  android:text="OK" />
  <Button 
  android:id="@+id/cancel"
  android:layout_width="100dp"
  android:layout_height="wrap_content"
  android:text="Cancel" />

</LinearLayout>

</LinearLayout>




First of all make a java file named ColorPickerDialog and insert the following code into it.


public class ColorPickerDialog extends Dialog implements SeekBar.OnSeekBarChangeListener, TextWatcher, OnClickListener {
SeekBar   redBar;
EditText  redText;
SeekBar   greenBar;
EditText  greenText;
SeekBar   blueBar;
EditText  blueText;
SeekBar   alphaBar;
EditText  alphaText;
ImageView colorPreview;
Button    ok;
Button    cancel;
String    color;

    public interface OnColorChangedListener {
        void colorChanged(int a, int r, int g, int b);
    }


    private OnColorChangedListener mListener;




    public ColorPickerDialog(Context context, OnColorChangedListener listener, String color) {
        super(context);
        mListener = listener;
        this.color = color;
    }


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        
        setContentView(R.layout.color_layout);
        setTitle("Color Picker");
        
        redBar    = (SeekBar)  findViewById(R.id.red_bar);
redText   = (EditText) findViewById(R.id.red_text);
greenBar  = (SeekBar)  findViewById(R.id.green_bar);
greenText = (EditText) findViewById(R.id.green_text);
blueBar   = (SeekBar)  findViewById(R.id.blue_bar);
blueText  = (EditText) findViewById(R.id.blue_text);
alphaBar  = (SeekBar)  findViewById(R.id.alpha_bar);
alphaText = (EditText) findViewById(R.id.alpha_text);
ok         = (Button)   findViewById(R.id.ok);
cancel     = (Button)   findViewById(R.id.cancel);

colorPreview = (ImageView) findViewById(R.id.color_preview);

//set initial colors
String[] colorVal = color.split(",");
int a = Integer.parseInt(colorVal[0]);
int r = Integer.parseInt(colorVal[1]);
int g = Integer.parseInt(colorVal[2]);
int b = Integer.parseInt(colorVal[3]);

colorPreview.setBackgroundColor(Color.argb(a, r, g, b));
redBar.setProgress(r);
greenBar.setProgress(g);
blueBar.setProgress(b);
alphaBar.setProgress(a);

redText.setText(colorVal[1]);
greenText.setText(colorVal[2]);
blueText.setText(colorVal[3]);
alphaText.setText(colorVal[0]);

redBar.setOnSeekBarChangeListener(this);
greenBar.setOnSeekBarChangeListener(this);
blueBar.setOnSeekBarChangeListener(this);
alphaBar.setOnSeekBarChangeListener(this);

redText.addTextChangedListener(this);
greenText.addTextChangedListener(this);
blueText.addTextChangedListener(this);
alphaText.addTextChangedListener(this);


ok.setOnClickListener(this);
cancel.setOnClickListener(this);

setCancelable(false);




    }


    public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
if(fromUser){
switch (seekBar.getId()) {
        case R.id.red_bar:  
        redText.setText(Integer.toString(progress));
        break;
        case R.id.green_bar:  
        greenText.setText(Integer.toString(progress));
        break;
        case R.id.blue_bar:  
        blueText.setText(Integer.toString(progress));
        break;
        case R.id.alpha_bar:  
        alphaText.setText(Integer.toString(progress));
        break;
       }
}

colorPreview.setBackgroundColor(Color.argb(alphaBar.getProgress(), redBar.getProgress(), greenBar.getProgress(), blueBar.getProgress()));


}


public void onStartTrackingTouch(SeekBar seekBar) {
}


public void onStopTrackingTouch(SeekBar seekBar) {
}


public void afterTextChanged(Editable arg0) {
if(Integer.parseInt(redText.getText().toString()) > 255)
redText.setText("255");

if(!redText.getText().toString().equals("")){
if(Integer.parseInt(redText.getText().toString()) > 255)
redText.setText("255");
redBar.setProgress(Integer.parseInt(redText.getText().toString()));
} else 
redBar.setProgress(0);


if(!greenText.getText().toString().equals("")){
if(Integer.parseInt(greenText.getText().toString()) > 255)
greenText.setText("255");
greenBar.setProgress(Integer.parseInt(greenText.getText().toString()));
} else 
greenBar.setProgress(0);


if(!blueText.getText().toString().equals("")){
if(Integer.parseInt(blueText.getText().toString()) > 255)
blueText.setText("255");
blueBar.setProgress(Integer.parseInt(blueText.getText().toString()));
}else 
blueBar.setProgress(0);


if(!alphaText.getText().toString().equals("")){
if(Integer.parseInt(alphaText.getText().toString()) > 255)
alphaText.setText("255");
alphaBar.setProgress(Integer.parseInt(alphaText.getText().toString()));
}else 
alphaBar.setProgress(0);


}


public void beforeTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {
}


public void onTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {
}



public void onClick(View v) {
switch (v.getId()) {
        case R.id.ok:  
        mListener.colorChanged(alphaBar.getProgress(), redBar.getProgress(), greenBar.getProgress(), blueBar.getProgress());
        dismiss();
        break;
        case R.id.cancel:  
        dismiss();
        break;
}

}
}


Now make a main java File named MyActivity with implementing the ColorPickerDialog.OnColorChangedListener. And put the code in it.



public class MyActivity extends Activity implements ColorPickerDialog.OnColorChangedListener {




String color = "255,255,000,000"; //alpha, red, green, blue
    
ImageView image;


    
public void onCreate(Bundle savedInstanceState) {
        
super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        image = (ImageView) findViewById(R.id.color);
        String[] colorVal = color.split(",");
        image.setBackgroundColor(Color.argb(Integer.parseInt(colorVal[0]),Integer.parseInt(colorVal[1]),Integer.parseInt(colorVal[2]),Integer.parseInt(colorVal[3])));
        
        Button btn = (Button) findViewById(R.id.select);
        btn.setOnClickListener(new View.OnClickListener() {
            
            public void onClick(View v) {
                new ColorPickerDialog(MyActivity.this, MyActivity.this, color).show();
            }
        });


    }


    public void colorChanged(int a, int r, int g, int b) {
    color = a + "," + r + "," + g + "," + b;
        image.setBackgroundColor(Color.argb(a,r,g,b));


    }


}






Wednesday, February 1, 2012

Change background color by SeekBar

In this exercise, the background color is changed, controlled by SeekBar.

Seek Bar is an extension of ProgressBar that adds a draggable thumb. The user can touch the thumb and drag left or right to set the current progress level or use the arrow keys.

When SeekBarChange, the onProgressChanged() of OnSeekBarChangeListener will be called, and the background of the screen(LinearLayout) will be updated according to the value (SeekBar.getProgress()).

Create a Android Application, SeekColor, with three SeekBar for Red, Green and Blue setting.

Put the below code in the main.xml   ::::


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/myScreen"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
/>
<SeekBar
android:id="@+id/mySeekingBar_R"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:max="255"
android:progress="0"/>
<SeekBar
android:id="@+id/mySeekingBar_G"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:max="255"
android:progress="0"/>
<SeekBar
android:id="@+id/mySeekingBar_B"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:max="255"
android:progress="0"/>
</LinearLayout>


Now make the main class as the ChangeColorBySeekBar.java and put this code in to it :


import android.app.Activity;
import android.os.Bundle;
import android.widget.LinearLayout;
import android.widget.SeekBar;

public class SeekColorActivity extends Activity {

private int seekR, seekG, seekB;
SeekBar redSeekBar, greenSeekBar, blueSeekBar;
LinearLayout mScreen;

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

mScreen = (LinearLayout) findViewById(R.id.myScreen);
redSeekBar = (SeekBar) findViewById(R.id.mySeekingBar_R);
greenSeekBar = (SeekBar) findViewById(R.id.mySeekingBar_G);
blueSeekBar = (SeekBar) findViewById(R.id.mySeekingBar_B);
updateBackground();

redSeekBar.setOnSeekBarChangeListener(seekBarChangeListener);
greenSeekBar.setOnSeekBarChangeListener(seekBarChangeListener);
blueSeekBar.setOnSeekBarChangeListener(seekBarChangeListener);

}

private SeekBar.OnSeekBarChangeListener seekBarChangeListener
= new SeekBar.OnSeekBarChangeListener()
{

@Override
public void onProgressChanged(SeekBar seekBar, int progress,
boolean fromUser) {
// TODO Auto-generated method stub
updateBackground();
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub
}

@Override
public void onStopTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub
}
};

private void updateBackground()
{
            seekR = redSeekBar.getProgress();
            seekG = greenSeekBar.getProgress();
            seekB = blueSeekBar.getProgress();
            mScreen.setBackgroundColor(
                                        0xff000000
                                        + seekR * 0x10000
                                        + seekG * 0x100
                                        + seekB
                                     );
          }
}