Reputation: 11999
I have a list view previewing a list of songs. On the onclick of a list item an alert dialog appears asking the user to play or download the song. The song is played from the url using media player class.
What i want to do is that after the user clicks play button he should also be able to stop the playback.
What i have in my mind is either change the name of the command play to stop after user had pressed play and stop appears to stop playing or if i can add a third button to alert dialog simply to stop it.
Please tell me how can i achieve it?
My list item on click code
list .setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,int position, long id)
{
songURL=songURL.get(position).toString();
final Dialog dialog = new Dialog(context);
dialog.setContentView(R.layout.custom_dialog);
dialog.setTitle("Downlaod or Play?");
pImage = (ImageView) dialog.findViewById(R.id.playImageView);
pImage.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
try {
MediaPlayer player = new MediaPlayer();
player.setAudioStreamType(AudioManager.STREAM_MUSIC);
player.setDataSource(songURL);
player.prepare();
player.start();
pImage.setImageResource(R.drawable.stop);
} catch (Exception e) {
// TODO: handle exception
}
}
});
dialog.show();
}});
Upvotes: 2
Views: 5255
Reputation: 22527
Using a custom dialog is more user friendly and more easy in my opinion.
Just have a play button in the dialog. When pressed change the text to "Stop".
Here is a simple tutorial on custom dialog to get you going.
Hope this helps.
Upvotes: 4