Seekbar for a mp3 file from SD card - android

I want add a seekbar on this class , but I cant
Please help me out
public class Play extends Title { // your class name
Button button, button1, stop;
MediaPlayer mp = new MediaPlayer();
/** Called when the activity is first created. */
#Override public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.music);
TextView tvHeading = (TextView) findViewById(R.id.titleHeading);
tvHeading.setText("music");
try {
audioPlayer("/sdcard/Android" , "Lala.mp3");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
button = (Button) findViewById(R.id.play);
PersianReshape.ReshapeButton(button, "2 Sfarnaz.TTF", Play.this);
button1 = (Button) findViewById(R.id.puse);
PersianReshape.ReshapeButton(button1, "2 Sfarnaz.TTF", Play.this);
stop = (Button) findViewById(R.id.stop);
PersianReshape.ReshapeButton(stop, "2 Sfarnaz.TTF", Play.this);
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
mp.start();
}
});
button1.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
mp.pause();
}
});
stop.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
mp.setLooping(true);
}
});
}
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();
}
}
#Override
public void onBackPressed() {
// TODO Auto-generated method stub
super.onBackPressed();
mp.stop();
finish();
}}
I've tried a lot of code but did not receive an answer,
This code reads the file from the memory, but it is not the case for seekbar
PLZ help me

Related

How do I call mediaplayer once only?

In android, I have a mediaplayer on detail screen, which duplicates when I select another screen then play another detail, causing sound files to play on the same time. how do I fix this problem? Id like to play the most recent selected media only.
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_feeddetail);
tc_recDurat = (TextView)findViewById(R.id.recDetDurat);
tc_recDuratBack = (TextView)findViewById(R.id.recDetDuratBack);
TextView tc_recPath = (TextView)findViewById(R.id.recDetPath);
recPath = (String) intent.getSerializableExtra("INTENT_PATH");
tc_recPath.setText(recPath);
mediaPlayer = new MediaPlayer();
try {
mediaPlayer.setDataSource(recPath);
} catch (IOException e1) {
// TODO Auto-generated catch block
Toast.makeText(getApplicationContext(), e1.getLocalizedMessage(), Toast.LENGTH_LONG);
}
ImageButton mClickButton1 = (ImageButton)findViewById(R.id.btnPlay);
mClickButton1.setOnClickListener( new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
if (mediaPlayer !=null){
try {
mediaPlayer
mediaPlayer.prepare();
seekBar.setMax(mediaPlayer.getDuration());
mediaPlayer.start();
seekUpdate();
isPlaying = true;
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
});
ImageButton mClickButton2 = (ImageButton)findViewById(R.id.btnStop);
mClickButton2.setOnClickListener( new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
mediaPlayer.stop();
isPlaying = false;
}
});
I am not sure what your problem is, but i believe you are trying to say that as you change through different activities, the sounds files do not stop playing
In that case you can do the following
In the activity
You can override the onPause method and put mediaPlayer.stop() inside it
#Override
public void onPause() {
super.onPause();
mediaPlayer.stop();
}

Android Media Player Next Song

I have a listview in MainActivity from hehe i passing my listview position using this
Intent intent = new Intent(MainActivity.this, Linkview.class);
intent.putExtra("position", position);
startActivity(intent);
And I have a Linkview class here i received my position using this
final Bundle bundle = getIntent().getExtras();
position = bundle.getInt("position");
And i have a linkArray list.
link = getResources().getStringArray(R.array.lin
My player play well and i can stop it.
But I have a next button
next.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
uri = Uri.parse(link[position+1]);
}
});
When I pressed my next button nothing is happened. I want that when I pressed the next button from my array next link will be play but nothing is play.
How i can solve this issue.
My hole code is here.
public class Linkview extends Activity {
Button play, stop, next;
MediaPlayer mediaPlayer;
private String currentSongIndex;
RowItem rowItem;
MainActivity mainActivity;
Uri uri;
public String link[];
int position;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, 0);
setContentView(R.layout.activity_linkview);
stop = (Button) findViewById(R.id.stopbutton);
play = (Button) findViewById(R.id.playbutton);
next = (Button) findViewById(R.id.nextbutton1);
link = getResources().getStringArray(R.array.link);
// String itemString = MainActivity.link[position];
final Bundle bundle = getIntent().getExtras();
// String link = bundle.getString("link");
// position = bundle.getInt("position");
next.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
mediaPlayer.stop();
if (position <= 2) {
uri = Uri.parse(link[position + 1]);
}
}
});
position = bundle.getInt("position");
uri = Uri.parse(link[position]);
// .parse("android.resource://com.prgguru.example/"
// + R.raw.hosannatamil);");
mediaPlayer = new MediaPlayer();
mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
try {
mediaPlayer.setDataSource(getApplicationContext(), uri);
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SecurityException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
mediaPlayer.prepare();
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
mediaPlayer.start();
stop.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
if (mediaPlayer != null && mediaPlayer.isPlaying()) {
mediaPlayer.stop();
}
}
});
play.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
// TODO Auto-generated method stub
try {
mediaPlayer.prepare();
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
mediaPlayer.start();
}
});
}
protected void onDestroy() {
super.onDestroy();
// TODO Auto-generated method stub
if (mediaPlayer != null) {
mediaPlayer.release();
mediaPlayer = null;
}
}
}
Maybe this will work:
next.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
mediaPlayer.stop();
uri = Uri.parse(link[position + 1]);
mediaPlayer.setDataSource(getApplicationContext(), uri);
mediaPlayer.prepare();
mediaPlayer.start();
}
});

Can't handle with Handler

I am playing music form my sd card and i want my seekbar to getprogress every 2 sec when music is playing. I am trying to do it with Handler. But i dont know how to use Handler coretly
Here is my code:
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
seekBar1 = (SeekBar) findViewById(R.id.seekBar1);
button1 = (Button) findViewById(R.id.button1);
button2 = (Button) findViewById(R.id.button2);
textView1 = (TextView) findViewById(R.id.textView1);
button1.setOnClickListener(new OnClickListener()
{
#Override
public void onClick(View v)
{
MediaPlayer mediaPlayer = new MediaPlayer();
try {
mediaPlayer.setDataSource(Environment.getExternalStorageDirectory()+"/MyImages/.audio2.wav");
mediaPlayer.prepare();
mediaPlayer.start();
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SecurityException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
setupHandler();
}
});
}
private void setupHandler()
{
handler.removeCallbacks(moveSeekBarThread);
handler.postDelayed(moveSeekBarThread, 2000); //cal the thread after millisec
}
private Runnable moveSeekBarThread = new Runnable() {
public void run() {
if(mediaPlayer.isPlaying()){
int mediaPos = mediaPlayer.getCurrentPosition();
int mediaMax = mediaPlayer.getDuration();
seekBar1.setMax(mediaMax);
seekBar1.setProgress(mediaPos);
handler.postDelayed(this, 2000); //Looping the thread after second
// seconds
}
}
};
}
My app crashing 2 second after i clikc the button1. So i am sure the problem here is Handler. Do i call this handler wrong or something?
Post your logcat always when you ask about app crashes.
Therefor in this case you have NullPointerException here, i think:
if(mediaPlayer.isPlaying())
You doesn't initialize your mediaplayer variable.
EDITED:
MediaPlayer mediaPlayer;
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
seekBar1 = (SeekBar) findViewById(R.id.seekBar1);
button1 = (Button) findViewById(R.id.button1);
button2 = (Button) findViewById(R.id.button2);
textView1 = (TextView) findViewById(R.id.textView1);
button1.setOnClickListener(new OnClickListener()
{
#Override
public void onClick(View v)
{
mediaPlayer = new MediaPlayer();
try {
mediaPlayer.setDataSource(Environment.getExternalStorageDirectory()+"/MyImages/.audio2.wav");
mediaPlayer.prepare();
mediaPlayer.start();
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SecurityException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
setupHandler();
}
});
}
private void setupHandler()
{
handler.removeCallbacks(moveSeekBarThread);
handler.postDelayed(moveSeekBarThread, 2000); //cal the thread after millisec
}
private Runnable moveSeekBarThread = new Runnable() {
public void run() {
if(mediaPlayer.isPlaying()){
int mediaPos = mediaPlayer.getCurrentPosition();
int mediaMax = mediaPlayer.getDuration();
seekBar1.setMax(mediaMax);
seekBar1.setProgress(mediaPos);
handler.postDelayed(this, 2000); //Looping the thread after second
// seconds
}
}
};
Use The below code to Update the SeekBar after 2Seconds.
Create the Object for Handler inside your onCreate() like the following one.
Handler miHandler=new Handler();
add Paste the code inside your thread.
miHandler.postDelayed(new Runnable() {
#Override
public void run() {
try{
seekBar1.setProgress(mediaPos+ add 2sec delay time also);
}catch(Exception e) {e.printStrackTrace();}
}
}, 1500);

Playing music even after the UI has been closed

Well I am a bit confused with the following.
I have a class (Main Player UI) that has the mediaplayer object.
Now the problem is that I want the music to played even after the UI has been disposed, hence I have to depend on the service.
But then if I move the MediaPlayer to the Service class
How am I going to control the onCompletion(MediaPlayer arg0) when the player UI is shown up, which also needs to show the change in the song? But then the method has to be included in the Service method.
How do I make the Play buttons on the UI to start the song that is available in the Service method?
Please guide me. The current player class that holds the UI as well as the mediaplayer is as follows:
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_boombastic_player);
//INITIALIZATIONS
mMediaPlayer=new MediaPlayer();
utils = new Utilities();
songProgressBar=(SeekBar)findViewById(R.string.seeker);
play=(Button)findViewById(R.string.play);
next=(Button)findViewById(R.string.next);
previous=(Button)findViewById(R.string.previous);
playlist=(Button)findViewById(R.string.Playlist);
search=(Button)findViewById(R.string.search);
repeat=(Button)findViewById(R.string.repeat);
shuffle=(Button)findViewById(R.string.shuffle);
starttime=(TextView)findViewById(R.string.startTime);
endtime=(TextView)findViewById(R.string.endTime);
ObjectIntermediate=new Intermediate();
srvMplayer=new ServiceMan();
//Preparation
if(!Searched)
{
GetAllSongs();
Searched=true;
ShowToast("Finished Search");
}
songProgressBar.setOnSeekBarChangeListener(this); // Important
mMediaPlayer.setOnCompletionListener(this);
play.setOnClickListener(new OnClickListener()
{
#Override
public void onClick(View arg0)
{
// TODO Auto-generated method stub
// check for already playing
if(mMediaPlayer.isPlaying())
{
if(mMediaPlayer!=null)
{
mMediaPlayer.pause();
/* Release Service*/
// Changing button image to play button
play.setBackgroundResource(R.drawable.playback_play);
}
}
else
{
// Resume song
if(mMediaPlayer!=null)
{
mMediaPlayer.start();
/* Starting Service*/
// Changing button image to pause button
play.setBackgroundResource(R.drawable.pause);
}
}
}
});
next.setOnClickListener(new OnClickListener()
{
#Override
public void onClick(View arg0)
{
// TODO Auto-generated method stub
// check if next song is there or not
if(isRepeat)
{
playSong(currentSongIndex);
}
else if(isShuffle)
{
ShuffleSong();
playSong(currentSongIndex);
}
else if(currentSongIndex < (BoombasticPlayer.Songslist.size() - 1))
{
try
{
ShowToast(Integer.toString(currentSongIndex));
playSong(currentSongIndex + 1);
}
catch (IllegalArgumentException e)
{
// TODO Auto-generated catch block
Log.e("~~IllegalArgumentException~~",e.toString());
android.util.Log.e("->>" , "~~stacktrace~~", e);
}
catch (IllegalStateException e)
{
// TODO Auto-generated catch block
Log.e("~~IllegalStateException~~",e.toString());
android.util.Log.e("->>" , "~~stacktrace~~", e);
}
currentSongIndex = currentSongIndex + 1;
}
else
{
// play first song
try
{
playSong(0);
}
catch (IllegalArgumentException e)
{
// TODO Auto-generated catch block
Log.e("~~IllegalArgumentException~~",e.toString());
android.util.Log.e("->>" , "~~stacktrace~~", e);
}
catch (IllegalStateException e)
{
// TODO Auto-generated catch block
Log.e("~~IllegalStateException~~",e.toString());
android.util.Log.e("->>" , "~~stacktrace~~", e);
}
currentSongIndex = 0;
}
}
});
previous.setOnClickListener(new OnClickListener()
{
#Override
public void onClick(View arg0)
{
// TODO Auto-generated method stub
if(isRepeat)
{
playSong(currentSongIndex);
}
else if(currentSongIndex > 0)
{
try {
playSong(currentSongIndex - 1);
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
currentSongIndex = currentSongIndex - 1;
}
else
{
// play last song
try {
playSong(BoombasticPlayer.Songslist.size() - 1);
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
currentSongIndex = BoombasticPlayer.Songslist.size() - 1;
}
}
});
playlist.setOnClickListener(new OnClickListener()
{
#Override
public void onClick(View arg0)
{
// TODO Auto-generated method stub
Intent playlistIntent= new Intent(arg0.getContext(), Playlist.class);
startActivityForResult(playlistIntent, MODE_APPEND);
}
});
repeat.setOnClickListener(new OnClickListener()
{
#Override
public void onClick(View arg0)
{
// TODO Auto-generated method stub
if(isRepeat==true)
{
isRepeat=false;
ShowToast("Repeat OFF");
}
else
{
isRepeat=true;
ShowToast("Repeat ON");
}
}
});
shuffle.setOnClickListener(new OnClickListener()
{
#Override
public void onClick(View arg0)
{
// TODO Auto-generated method stub
if(isShuffle==true)
{
isShuffle=false;
ShowToast("Shuffle OFF");
}
else
{
isShuffle=true;
ShowToast("Shuffle ON");
}
}
});
}

MediaPlayer play next track from /res/raw

I'm making an lullaby app.Initially there are two tracks in my /res/raw folder.
On completing first track I want it to play next track.
i did this using onCompletionListener.But it is showing force close error.
Below is my code for that.
So can anyone tell me how to do that?
public class PlayRandom extends Activity implements OnCompletionListener{
MediaPlayer mp;
int rnum;
TextView tv;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.playrandom);
mp.setOnCompletionListener(this);
mp = MediaPlayer.create(this, R.raw.lullaby1);
mp.start();
}
#Override
public void finish() {
// TODO Auto-generated method stub
super.finish();
mp.stop();
}
public void onCompletion(MediaPlayer mp) {
// TODO Auto-generated method stub
File pdfFile = new File("res/raw/lullaby2.mp3");
Uri path = Uri.fromFile(pdfFile);
mp.stop();
try{
mp.setDataSource(getApplicationContext(), path);
mp.prepare();
mp.start();
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SecurityException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
I tried this but still its not working.
That meand there's something wrong in OnClickListener.
public class PlayRandom extends Activity implements OnCompletionListener{
MediaPlayer mp;
int rnum;
TextView tv;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.playrandom);
mp.setOnCompletionListener(this);
mp = MediaPlayer.create(this, R.raw.lullaby1);
mp.start();
}
#Override
public void finish() {
// TODO Auto-generated method stub
super.finish();
mp.stop();
}
public void onCompletion(MediaPlayer mp) {
// TODO Auto-generated method stub
Toast.makeText(this,"Yuppieee!!", Toast.LENGTH_SHORT);
}
}
use AssetFileDescriptor for playing file from raw folder:
AssetFileDescriptor afdes = getAssets().openRawResourceFd(R.raw.lullaby2);
mp.reset();
mp.setDataSource(afdes.getFileDescriptor(), afdes.getStartOffset(), afdes.getLength());
mp.prepare();
mp.start();

Categories

Resources