How to stop old song when new song is playing in Android? - android

I am new to Android development and currently I am developing a simple mediaplayer application.
According to my project I have define three fragments. First fragment is a Player fragment which displays the mediaplayer control and plays the song. Second fragment is a Playlist which contains songlist. Third fragment is a recent playlist which contains recently played songs.
My question is how to stop old song playing when new song is selected from the Playlist fragment?
PLAYER FRAGMENT.
public class Player extends Fragment implements SeekBar.OnSeekBarChangeListener{
private ImageButton btnPlay;
private ImageButton btnForward;
private ImageButton btnBackward;
private ImageButton btnNext;
private ImageButton btnPrevious;
private ImageButton btnRepeat;
private ImageButton btnShuffle;
private SeekBar songProgressBar;
private TextView songTitleLabel;
private TextView songCurrentDurationLabel;
private TextView songTotalDurationLabel;
// Media Player
public static MediaPlayer mp,mp2;
VisualizerView mVisualizerView;
SliderView sl;
private Visualizer mVisualizer;
// Handler to update UI timer, progress bar etc,.
private Handler mHandler = new Handler();;
private SongsManager songManager;
private Utilities utils;
private int seekForwardTime = 5000; // 5000 milliseconds
private int seekBackwardTime = 5000; // 5000 milliseconds
private int currentSongIndex = 0;
private boolean isShuffle = false;
private boolean isRepeat = false;
private ArrayList<SongModel> songsList = new ArrayList<SongModel>();
int songIndex;
SongAdapter songAdapter;
public Player(int position) {
songIndex = position;
}
public Player() {
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View android = inflater.inflate(R.layout.player, container, false);
/*ClickWheel wheel = (ClickWheel) android.findViewById(R.id.wheel);
wheel.getModel().addListener(this);*/
// variable initialization for button
return android;
}
#Override
public void onViewCreated(View v, Bundle savedInstanceState) {
super.onViewCreated(v, savedInstanceState);
btnPlay = (ImageButton)getView().findViewById(R.id.btnPlay);
btnForward = (ImageButton) getView().findViewById(R.id.btnForward);
btnBackward = (ImageButton) getView().findViewById(R.id.btnBackward);
btnNext = (ImageButton) getView().findViewById(R.id.btnNext);
btnPrevious = (ImageButton) getView().findViewById(R.id.btnPrevious);
btnRepeat = (ImageButton) getView().findViewById(R.id.btnRepeat);
btnShuffle = (ImageButton) getView().findViewById(R.id.btnShuffle);
songProgressBar = (SeekBar) getView().findViewById(R.id.songProgressBar);
songTitleLabel = (TextView) getView().findViewById(R.id.songTitle);
songTitleLabel.setSelected(true);
songTitleLabel.setEllipsize(TextUtils.TruncateAt.MARQUEE);
songTitleLabel.setSingleLine(true);
songCurrentDurationLabel = (TextView) getView().findViewById(R.id.songCurrentDurationLabel);
songTotalDurationLabel = (TextView) getView().findViewById(R.id.songTotalDurationLabel);
mp = new MediaPlayer();
songManager = new SongsManager();
utils = new Utilities();
songProgressBar.setOnSeekBarChangeListener(this);
songsList = songManager.getPlayList();
if (songIndex == 0) {
playSong(0);
} else {
playSong(songIndex);
}
btnPlay.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
// check for already playing
if (mp.isPlaying()) {
if (mp != null) {
mp.pause();
btnPlay.setImageResource(R.drawable.btn_play);
}
} else {
// Resume song
if (mp != null) {
mp.start();
btnPlay.setImageResource(R.drawable.btn_pause);
}
}
}
});
btnForward.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
int currentPosition = mp.getCurrentPosition();
if(currentPosition + seekForwardTime <= mp.getDuration()){
mp.seekTo(currentPosition + seekForwardTime);
}else{
mp.seekTo(mp.getDuration());
}
}
});
btnBackward.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
int currentPosition = mp.getCurrentPosition();
if(currentPosition - seekBackwardTime >= 0){
mp.seekTo(currentPosition - seekBackwardTime);
}else{
mp.seekTo(0);
}
}
});
btnNext.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
if(currentSongIndex < (songsList.size() - 1)){
playSong(currentSongIndex + 1);
currentSongIndex = currentSongIndex + 1;
}else{
playSong(0);
currentSongIndex = 0;
}
}
});
btnPrevious.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
if(currentSongIndex > 0){
playSong(currentSongIndex - 1);
currentSongIndex = currentSongIndex - 1;
}else{
playSong(songsList.size() - 1);
currentSongIndex = songsList.size() - 1;
}
}
});
btnRepeat.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
if(isRepeat){
isRepeat = false;
Toast.makeText(getActivity().getApplicationContext(), "Repeat is OFF", Toast.LENGTH_SHORT).show();
btnRepeat.setImageResource(R.drawable.btn_repeat);
}else{
isRepeat = true;
Toast.makeText(getActivity().getApplicationContext(), "Repeat is ON", Toast.LENGTH_SHORT).show();
isShuffle = false;
btnRepeat.setImageResource(R.drawable.btn_repeat_focused);
btnShuffle.setImageResource(R.drawable.btn_shuffle);
}
}
});
btnShuffle.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
if(isShuffle){
isShuffle = false;
Toast.makeText(getActivity().getApplicationContext(), "Shuffle is OFF", Toast.LENGTH_SHORT).show();
btnShuffle.setImageResource(R.drawable.btn_shuffle);
}else{
isShuffle= true;
Toast.makeText(getActivity().getApplicationContext(), "Shuffle is ON", Toast.LENGTH_SHORT).show();
isRepeat = false;
btnShuffle.setImageResource(R.drawable.btn_shuffle_focused);
btnRepeat.setImageResource(R.drawable.btn_repeat);
}
}
});
}
public void playSong(int songIndex) {
try {
Log.e("playSong()...", "....is called");
mp.reset();
mp.setDataSource(songsList.get(songIndex).getSongPath());
mp.prepare();
songProgressBar.setProgress(0);
songProgressBar.setMax(100);
String songTitle = songsList.get(songIndex).getSongTitle();
songTitleLabel.setText(songTitle);
btnPlay.setImageResource(R.drawable.btn_pause);
mp.start();
updateProgressBar();
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
private Runnable mUpdateTimeTask = new Runnable() {
public void run() {
long totalDuration = mp.getDuration();
long currentDuration = mp.getCurrentPosition();
songTotalDurationLabel.setText(""+utils.milliSecondsToTimer(totalDuration));
songCurrentDurationLabel.setText("" + utils.milliSecondsToTimer(currentDuration));
int progress =(int)(utils.getProgressPercentage(currentDuration, totalDuration));
songProgressBar.setProgress(progress);
mHandler.postDelayed(this, 100);
}
};
private void updateProgressBar() {
mHandler.postDelayed(mUpdateTimeTask, 100);
}
#Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
}
public void onStartTrackingTouch(SeekBar seekBar) {
mHandler.removeCallbacks(mUpdateTimeTask);
}
public void onStopTrackingTouch(SeekBar seekBar) {
mHandler.removeCallbacks(mUpdateTimeTask);
int totalDuration = mp.getDuration();
int currentPosition = utils.progressToTimer(seekBar.getProgress(), totalDuration);
mp.seekTo(currentPosition);
updateProgressBar();
}}
PLAYLIST FRAGMENT
public class Playlists extends ListFragment {
EditText edtSearch;
SongAdapter songAdapter;
ArrayList<SongModel> songList = new ArrayList<SongModel>();
SongsManager songsManager = new SongsManager();
// Songs list
public ArrayList<SongModel> songsList = new ArrayList<>();
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View windows = inflater.inflate(R.layout.playlist, container, false);
return windows;
}
#Override
public void onViewCreated(View v, Bundle savedInstanceState) {
super.onViewCreated(v, savedInstanceState);
getListView().setFastScrollEnabled(true);
//ListView animation
LayoutAnimationController controller
= AnimationUtils.loadLayoutAnimation(
getActivity(), R.anim.list_layout_controller);
getListView().setLayoutAnimation(controller);
edtSearch = (EditText)getView().findViewById(R.id.search);
final ArrayList<SongModel> songsListData = songsManager.songList;
Log.i("songsListData...",""+songsListData.size());
SongsManager plm = new SongsManager();
// get all songs from sdcard
this.songsList = plm.getPlayList();
// looping through playlist
for (int i = 0; i < songsListData.size(); i++) {
// creating new HashMap
SongModel song = songsListData.get(i);
// adding HashList to ArrayList
songsListData.add(song);
}
songAdapter = new SongAdapter(getActivity(),songsList);
setListAdapter(songAdapter);
// selecting single ListView item
ListView lv = getListView();
// listening to single listitem click
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View arg0,
int position, long id) {
Log.i("Index", "..." + position);
songAdapter.setSelectedIndex(position);
Intent i = new Intent(getActivity().getApplicationContext(),Main.class);
i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK|Intent.FLAG_ACTIVITY_NEW_TASK);
SongModel songModel = (SongModel) songAdapter.getItem(position);
int indexOfSong = songAdapter.songsList.indexOf(songModel);
// Sending songIndex to PlayerActivity
i.putExtra("songIndex", indexOfSong);
getActivity().setResult(100, i);
startActivityForResult(i, 100);
/*Player.mp.stop();
Player.mp.release();*/
//putting song in recentSongList arraylist
SongModel model = (SongModel) songAdapter.getItem(position);
model.setSongTitle(songModel.getSongTitle());
model.setSongPath(songModel.getSongPath());
Constant.recentSongList.add(model);
Log.i("recentSongList...","..."+Constant.recentSongList.size());
getActivity().finish();
}
});
lv.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
#Override
public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
removeItemFromList(position);
return true;
}
private void removeItemFromList(int position) {
final int deletePosition = position;
AlertDialog.Builder alert = new AlertDialog.Builder(
getActivity());
alert.setTitle("Delete");
alert.setMessage("Do you want delete this song?");
alert.setPositiveButton("YES", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
// TOD O Auto-generated method stub
// main code on after clicking yes
songsList.remove(deletePosition);
songAdapter.notifyDataSetChanged();
songAdapter.notifyDataSetInvalidated();
}
});
alert.setNegativeButton("CANCEL", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
dialog.dismiss();
}
});
alert.show();
}
});
edtSearch.addTextChangedListener(new TextWatcher() {
#Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
#Override
public void onTextChanged(CharSequence cs, int start, int before, int count) {
}
#Override
public void afterTextChanged(Editable s) {
String text = edtSearch.getText().toString().toLowerCase(Locale.getDefault());
songAdapter.filter(text);
}
});
}
#Override
public void setUserVisibleHint(boolean isVisibleToUser) {
super.setUserVisibleHint(isVisibleToUser);
if (this.isVisible()) {
// If we are becoming invisible, then...
Log.d("setUserVisibleHint()...", "PlayList...Visible");
if (!isVisibleToUser) {
Log.d("setUserVisibleHint()...", "PlayList...notVisible");
// TODO stop audio playback
}
}
}
If anyone know how to do this, help me.

In your playlist fragment remove this Player.mp.release();
Edit your Listview onItemCLickListner like this
#Override
public void onItemClick(AdapterView<?> parent, View arg0,
int position, long id) {
Log.i("Index", "..." + position);
songAdapter.setSelectedIndex(position);
Intent i = new Intent(getActivity().getApplicationContext(),Main.class);
i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK|Intent.FLAG_ACTIVITY_NEW_TASK);
SongModel songModel = (SongModel) songAdapter.getItem(position);
int indexOfSong = songAdapter.songsList.indexOf(songModel);
// Sending songIndex to PlayerActivity
i.putExtra("songIndex", indexOfSong);
getActivity().setResult(100, i);
startActivityForResult(i, 100);
Player.mp.stop();
//putting song in recentSongList arraylist
SongModel model = (SongModel) songAdapter.getItem(position);
model.setSongTitle(songModel.getSongTitle());
model.setSongPath(songModel.getSongPath());
Constant.recentSongList.add(model);
Log.i("recentSongList...","..."+Constant.recentSongList.size());
getActivity().finish();
}
});
I hope this work...

Related

In listview I click an audio to start but when I scroll I see other audios has stared too

I have a ListView of audios which have seek bar in it . When I click the play button of one of audios, it starts but when I scroll down I see that some other seekbars are also playing and when I scroll up again the first item that I clicked is no paused. I have searched a lot and it seems that there is a problem with position of item in list , the only solution I found was to use viewholder in adapter but it didn't work . does any one have a solution? thanks.
public class AudioAdapter extends ArrayAdapter<News> {
Context context;
boolean isPLaying = false;
public AudioAdapter(Context context, ArrayList<News> array) {
super(context, R.layout.list_item_news, array);
this.context = context;
}
public static class ViewHolder {
TextView txtTitle;
SeekBar seekBar;
TextView audioDuration;
ImageView btnPlayStop;
MediaPlayer mp;
Handler seekHandler;
int audioCurrentPosition;
}
public View getView(final int position, View convertView, ViewGroup parent) {
final News news = getItem(position);
final ViewHolder viewHolder;
if (convertView == null) {
viewHolder = new ViewHolder();
convertView = LayoutInflater.from(getContext())
.inflate(R.layout.list_item_audio, parent, false);
viewHolder.txtTitle = (TextView) convertView.findViewById(R.id.audioTitle);
viewHolder.seekBar = (SeekBar) convertView.findViewById(seek_bar);
viewHolder.audioDuration = (TextView) convertView.findViewById(R.id.txtDuration);
viewHolder.btnPlayStop = (ImageView) convertView.findViewById(R.id.btnPlayStop);
viewHolder.mp = new MediaPlayer();
viewHolder.seekHandler = new Handler();
convertView.setTag(viewHolder);
}
else {
viewHolder = (ViewHolder) convertView.getTag();
}
viewHolder.txtTitle.setText(news.getTitle());
viewHolder.mp = MediaPlayer.create(context, R.raw.team);
viewHolder.audioCurrentPosition = news.getAudioPosition();
viewHolder.mp.seekTo(viewHolder.audioCurrentPosition * 1000);
viewHolder.audioDuration.setText(ms);
viewHolder.btnPlayStop.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if ( !isPLaying) {
viewHolder.mp.seekTo(viewHolder.audioCurrentPosition * 1000);
//play
isPLaying = true;
viewHolder.btnPlayStop.setImageResource(ic_media_pause);
viewHolder.mp.start();
viewHolder.seekBar.setMax( viewHolder.mp.getDuration() / 1000);
((Activity) context).runOnUiThread(new Runnable() {
#Override
public void run() {
if ( viewHolder.mp != null && viewHolder.mp.isPlaying()) {
int mCurrentPosition = viewHolder.mp.getCurrentPosition() / 1000;
viewHolder.seekBar.setProgress(mCurrentPosition);
viewHolder.seekHandler.postDelayed(this, 1000);
news.setAudioPosition( viewHolder.mp.getCurrentPosition() / 1000);
}
}
});
} else {
//pause
isPLaying = false;
viewHolder.btnPlayStop.setImageResource(ic_media_play);
viewHolder.audioCurrentPosition = viewHolder.mp.getCurrentPosition() / 1000;
viewHolder.mp.pause();
}
}
});
viewHolder.seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
#Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
#Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
#Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
if ( viewHolder.mp != null && fromUser) {
//Change the seekbar with finger
viewHolder.mp.seekTo(progress * 1000);
viewHolder.audioCurrentPosition = progress * 1000;
}
}
});
viewHolder.mp.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
#Override
public void onCompletion(MediaPlayer mp) {
//When mediaplayer finishes
isPLaying = false;
viewHolder.btnPlayStop.setImageResource(ic_media_play);
viewHolder.audioCurrentPosition = 0;
}
});
return convertView;
}
}
Since you are using ViewHolder pattern, your views are being recycled and hence, your playing-indicator gets reused too...
Release your old MediaPlayer when you a new instance is started. For example:
protected void mediaplayerMethod(String filepath) {
Log.d(TAG, "mediaplayerMethod audio file path " + filepath);
if(mp != null){
mp.release();
}
mp = null;
mp = new MediaPlayer();
mp.setOnCompletionListener(AudioAdapter.this); // Important
seekBar.setOnSeekBarChangeListener(AudioAdapter.this);
utils = new Utilities();
playSong(filepath);
}
EDIT 1
You can try the below code
public class SoundTest extends Activity {
private ListView lv1;
private String lv_arr[]={"test 1","test 2","test 3","test 4","test 5"};
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
lv1=(ListView)findViewById(R.id.ListView01);
lv1.setAdapter(new ArrayAdapter<String>(this,R.layout.list_item, lv_arr));
lv1.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,int position, long id) {
if (lv1.getItemAtPosition(position)=="test 1") {
MediaPlayer mp = MediaPlayer.create(getApplicationContext(),R.raw.sound1);
mp.start();
mp.setOnCompletionListener(new OnCompletionListener() {
public void onCompletion(MediaPlayer mp) {
mp.release();
}
});
}
if (lv1.getItemAtPosition(position)=="test 2") {
MediaPlayer mp = MediaPlayer.create(getApplicationContext(),R.raw.sound2);
mp.start();
mp.setOnCompletionListener(new OnCompletionListener() {
public void onCompletion(MediaPlayer mp) {
mp.release();
}
});
}
//And the rest of the audio 3,4,5.
}
});
}
}
The global variable MediaPlayer needs to be set private static. This has caught me several times.

How to add next song and previous song play button in audio player using android studio2.3?

I am developing an app with audio player.I want to add next song play button and previous song play button. Following is my code which is working but in this code i want to add next track play and previous track play button.Help me!!
//MainActivity.java
public class MainActivity extends ListActivity {
private static final int UPDATE_FREQUENCY = 500;
private static final int STEP_VALUE = 4000;
private TextView selectedfile = null;
private SeekBar seekBar = null;
private MediaPlayer player = null;
private ImageButton prev = null;
private ImageButton play = null;
private ImageButton next = null;
private MediaCursorAdapter adapter = null;
private boolean isStarted = true;
private String currentFile = "";
private boolean isMovingSeekBar = false;
private final Handler handler = new Handler();
private final Runnable updatePositinRunnable = new Runnable() {
#Override
public void run() {
updatePosition();
}
};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
selectedfile = (TextView) findViewById(R.id.selecteditem);
seekBar = (SeekBar) findViewById(R.id.seekBar);
prev = (ImageButton) findViewById(R.id.previous);
play = (ImageButton) findViewById(R.id.play);
next = (ImageButton) findViewById(R.id.next);
player = new MediaPlayer();
player.setOnCompletionListener(onCompletion);
player.setOnErrorListener(onError);
seekBar.setOnSeekBarChangeListener(seekBarChanged);
Cursor cursor = getContentResolver().query(MediaStore.Audio.Media.
INTERNAL_CONTENT_URI, null, null, null, null);
if (null != cursor) {
cursor.moveToFirst();
adapter = new MediaCursorAdapter(this, R.layout.item, cursor);
setListAdapter(adapter);
prev.setOnClickListener(OnButtonClick);
play.setOnClickListener(OnButtonClick);
next.setOnClickListener(OnButtonClick);
}
}
#Override
protected void onListItemClick(ListView l, View v, int position, long id) {
super.onListItemClick(l, v, position, id);
currentFile = (String) v.getTag();
startPlay(currentFile);
}
private void startPlay(String file) {
Log.i("Selected: ", file);
selectedfile.setText(file);
seekBar.setProgress(0);
player.stop();
player.reset();
try {
player.setDataSource(file);
player.prepare();
player.start();
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
seekBar.setMax(player.getDuration());
play.setImageResource(android.R.drawable.ic_media_pause);
updatePosition();
isStarted = true;
}
private void stopPlay() {
player.stop();
player.reset();
play.setImageResource(android.R.drawable.ic_media_play);
handler.removeCallbacks(updatePositinRunnable);
seekBar.setProgress(0);
isStarted = false;
}
#Override
protected void onDestroy() {
super.onDestroy();
handler.removeCallbacks(updatePositinRunnable);
player.stop();
player.reset();
player.release();
player = null;
}
private void updatePosition() {
handler.removeCallbacks(updatePositinRunnable);
seekBar.setProgress(player.getCurrentPosition());
handler.postDelayed(updatePositinRunnable, UPDATE_FREQUENCY);
}
private View.OnClickListener OnButtonClick = new View.OnClickListener() {
#Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.play: {
if (player.isPlaying()) {
handler.removeCallbacks(updatePositinRunnable);
player.pause();
play.setImageResource(android.R.drawable.ic_media_play);
} else {
if (isStarted) {
player.start();
play.setImageResource(android.R.drawable.ic_media_pause);
updatePosition();
} else {
startPlay(currentFile);
}
}
break;
}
case R.id.next: {
int seekto = player.getCurrentPosition() + STEP_VALUE;
if (seekto > player.getDuration())
seekto = player.getDuration();
player.pause();
player.seekTo(seekto);
player.start();
break;
}
case R.id.previous: {
int seekto = player.getCurrentPosition() - STEP_VALUE;
if (seekto < 0)
seekto = 0;
player.pause();
player.seekTo(seekto);
player.start();
break;
}
}
}
};
private MediaPlayer.OnCompletionListener onCompletion = new MediaPlayer.OnCompletionListener() {
#Override
public void onCompletion(MediaPlayer mp) {
stopPlay();
}
;
};
private MediaPlayer.OnErrorListener onError = new MediaPlayer.OnErrorListener() {
#Override
public boolean onError(MediaPlayer mp, int what, int extra) {
return false;
}
};
private SeekBar.OnSeekBarChangeListener seekBarChanged =
new SeekBar.OnSeekBarChangeListener() {
#Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
if (isMovingSeekBar) {
player.seekTo(progress);
Log.i("OnSeekBarChangeListener", "OnProgressChanged");
}
}
#Override
public void onStartTrackingTouch(SeekBar seekBar) {
isMovingSeekBar = true;
}
#Override
public void onStopTrackingTouch(SeekBar seekBar) {
isMovingSeekBar = false;
}
};
}
//MediaCursorAdapter.java
public class MediaCursorAdapter extends SimpleCursorAdapter {
public MediaCursorAdapter(Context context, int layout, Cursor c) {
super(context, layout, c, new String[]{MediaStore.MediaColumns.DISPLAY_NAME,
MediaStore.MediaColumns.TITLE,MediaStore.Audio.AudioColumns.DURATION},
new int[]{R.id.displayname, R.id.title, R.id.duration});
}
#Override
public void bindView(View view, Context context, Cursor cursor) {
TextView title = (TextView) view.findViewById(R.id.title);
TextView name = (TextView) view.findViewById(R.id.displayname);
TextView duration = (TextView) view.findViewById(R.id.duration);
name.setText(cursor.getString(cursor.getColumnIndex(
MediaStore.MediaColumns.DISPLAY_NAME)));
title.setText(cursor.getString(cursor.getColumnIndex(
MediaStore.MediaColumns.TITLE)));
long durationInMS = Long.parseLong(cursor.getString(
cursor.getColumnIndex(MediaStore.Audio.AudioColumns.DURATION)));
double durationInMin = ((double) durationInMS / 1000.0) / 60.0;
durationInMin = new BigDecimal(Double.toString(durationInMin)).
setScale(2, BigDecimal.ROUND_UP).doubleValue();
duration.setText("" + durationInMin);
view.setTag(cursor.getString(cursor.getColumnIndex(MediaStore.MediaColumns.DATA)));
}
#Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
LayoutInflater inflater = LayoutInflater.from(context);
View v = inflater.inflate(R.layout.item, parent, false);
bindView(v, context, cursor);
return v;
}
}

How to implement the prev and next button in second activity using Simpleadapter in Fragment?

I integrated a Radiochannel list in a Fragment using Simpleadapter. Now I want implement the prev and next button in a second activity to switch between the List.
Now I passed data using Bundle, but by clicking the nextButton i can just increment the position one time, at the second click the app stops.
What is my ERROR?
1) Fragment
public class RadioFragment extends ListFragment {
String[] radioLink = new String[]{
"http://vivisolive-lh.akamaihd.net/i/syg_rd_berfin_1#383073/index_128_a-b.m3u8?sd=10&rebase=on",
"http://yayin.firatfm.net:2021",
"http://95.173.188.155:9984/;",
};
//Med Nûçe
// Array of strings storing country names
String[] radioName = new String[]{
"Dengê Kûrdî",
"Firat Fm",
"Radyo Dengê Kûrdî",
};
// Array of integers points to images stored in /res/drawable/
int[] radioLogo = new int[]{
R.drawable.ic_menu_camera,
R.drawable.ic_menu_camera,
R.drawable.ic_menu_camera,
};
// Array of strings to store Radio_Number
String[] channel_number = new String[100];
private Activity activity;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
// Each row in the list stores country name, currency and flag
List<HashMap<String, String>> bList = new ArrayList<HashMap<String, String>>();
//fill the TV Number list
int count ;
for(count =1; count< radioLink.length+1;count++){
channel_number[count-1]= "Radio: "+count;
}
for (int i = 0; i < radioLink.length; i++) {
HashMap<String, String> hm = new HashMap<String, String>();
hm.put("number_radio", "channel : " + radioName[i]);
hm.put("name_radio", "number : " + channel_number[i]);
hm.put("logo_radio", Integer.toString(radioLogo[i]));
bList.add(hm);
}
// Keys used in Hashmap
String[] from = {"logo_radio", "number_radio", "name_radio"};
// Ids of views in listview_layout
int[] to = {R.id.logo_radio, R.id.number_radio, R.id.name_radio};
// Instantiating an adapter to store each items
// R.layout.listview_layout defines the layout of each item
SimpleAdapter adapter = new SimpleAdapter(getActivity().getBaseContext(), bList, R.layout.fragment_radio, from, to);
setListAdapter(adapter);
//Data From Fragment to Activity
return super.onCreateView(inflater, container, savedInstanceState);
}
#Override
public void onStart() {
super.onStart();
getListView().setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Toast.makeText(getActivity(), radioName[position], Toast.LENGTH_SHORT).show();
Bundle bundle_send = new Bundle();
Intent intent = new Intent(getActivity(), RadioPlayActivity.class);
switch (position) {
case 0:
Toast.makeText(getActivity(), radioLink[position], Toast.LENGTH_SHORT).show();
bundle_send.putString("radiodata", radioLink[position]);
bundle_send.putInt("position", position);
bundle_send.putStringArray("array", radioLink);
intent.putExtras(bundle_send);
startActivity(intent);
break;
case 1:
Toast.makeText(getActivity(), radioLink[position], Toast.LENGTH_SHORT).show();
bundle_send.putString("radiodata", radioLink[position]);
bundle_send.putInt("position", position);
bundle_send.putStringArray("array", radioLink);
intent.putExtras(bundle_send);
startActivity(intent);
break;
case 2:
Toast.makeText(getActivity(), radioLink[position], Toast.LENGTH_SHORT).show();
bundle_send.putString("radiodata", radioLink[position]);
bundle_send.putInt("position", position);
bundle_send.putStringArray("array", radioLink);
intent.putExtras(bundle_send);
startActivity(intent);
break;
default:
}
}
});
}
}
By clicking the listview items one by one, i can send the data from RadioFragment to RadioActivity and can play my Radio.
Now I passed data using Bundle, but by clicking the nextButton i can just increment the position one time, at the second click the app stops.
What is my ERROR?
1) RadioActivity
public class RadioPlayActivity extends AppCompatActivity implements OnClickListener {
private ProgressBar playSeekBar;
private Button buttonPlay;
private Button buttonStopPlay;
private MediaPlayer player = new MediaPlayer();;
private Button buttonPrevious;
private Button buttonNext;
private int playerId=0;
private int newId = 0;
private String[] array;
private int Counter=0;
private int pos=0;
/**
* Called when the activity is first created.
*/
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.radiobuffer);
initializeUIElements();
initializeMediaPlayer();
}
private void initializeUIElements() {
playSeekBar = (ProgressBar) findViewById(R.id.progressBar1);
playSeekBar.setMax(100);
playSeekBar.setVisibility(View.INVISIBLE);
buttonPlay = (Button) findViewById(R.id.buttonPlay);
buttonPlay.setOnClickListener(this);
buttonStopPlay = (Button) findViewById(R.id.buttonStopPlay);
buttonStopPlay.setEnabled(false);
buttonStopPlay.setOnClickListener(this);
}
public void onClick(View v) {
if (v == buttonPlay) {
startPlaying();
} else if (v == buttonStopPlay) {
stopPlaying();
}
}
private void startPlaying() {
buttonStopPlay.setEnabled(true);
buttonPlay.setEnabled(false);
playSeekBar.setVisibility(View.VISIBLE);
player.prepareAsync();
player.setOnPreparedListener(new OnPreparedListener() {
public void onPrepared(MediaPlayer mp) {
player.start();
}
});
}
private void stopPlaying() {
if (player.isPlaying()) {
player.stop();
player.release();
initializeMediaPlayer();
}
buttonPlay.setEnabled(true);
buttonStopPlay.setEnabled(false);
playSeekBar.setVisibility(View.INVISIBLE);
}
private void initializeMediaPlayer() {
//Get Radio_Data from RadioFragment
Bundle bundle_get = getIntent().getExtras();
// final String tvListTvFragment = zielkorb.getString("radiodata");
pos = bundle_get.getInt("position");
array = bundle_get.getStringArray("array");
try {
//player.setDataSource(tvListTvFragment);
Toast.makeText(RadioPlayActivity.this, String.valueOf(pos), Toast.LENGTH_SHORT).show();
// player.setDataSource(array[position]);
buttonNext = (Button) findViewById(R.id.button_next);
buttonNext.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
int position = pos + 1;
try {
Toast.makeText(RadioPlayActivity.this, String.valueOf(position), Toast.LENGTH_SHORT).show();
player.setDataSource(array[position]);
} catch (IOException e) {
e.printStackTrace();
}
if (!(position >= array.length)) {
pos += 1;
} else {
pos = -1;
}
}
});
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (IllegalStateException e) {
e.printStackTrace();
}
player.setOnBufferingUpdateListener(new OnBufferingUpdateListener() {
public void onBufferingUpdate(MediaPlayer mp, int percent) {
playSeekBar.setSecondaryProgress(percent);
Log.i("Buffering", "" + percent);
}
});
}
#Override
protected void onPause() {
super.onPause();
if (player.isPlaying()) {
player.stop();
}
}
}
I want to switch My Radio List,e.g from List position 1 to List Position in the 2. Activity by cklicking the next or previous button. I want to play all radio channel in one activity.

Android: Playing one media file at one time from listview

i have create one listview which contain 4 media file to play.If i am select 1st song to play,it is playing but if i select socond song on listview to play then both are playing so i want that if second will be select to play then first need to be stop and only second song need to be play. Kindly help me solve such issue...Thank in advance.
Below is my code.
Main Activity
public class MainActivity extends Activity implements AdapterView.OnItemClickListener {
private ArrayList<String> audioList;
private File file;
private ListView lvAudio;
private Context context;
private Adapter Adaptr;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
audioList = new ArrayList<String>();
String root_sd = Environment.getExternalStorageDirectory().toString();
file = new File( root_sd + "/EmoticApp/AudioFile/" ) ;
File list[] = file.listFiles();
for( int i=0; i< list.length; i++)
{
audioList.add( list[i].getName() );
}
getid();
setListner();
Adaptr = new Adapter(MainActivity.this,audioList);
lvAudio.setAdapter(Adaptr);
}
private void setListner() {
lvAudio.setOnItemClickListener(this);
}
private void getid() {
lvAudio =(ListView)findViewById(R.id.lvAudio);
}
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
}
}
Adapter code:
public class Adapter extends BaseAdapter {
ArrayList<String> audioList;
Context context;
private static LayoutInflater inflater = null;
public boolean flag =false;
public Adapter(MainActivity mainActivity, ArrayList<String> audioList) {
this.context = mainActivity;
this.audioList = audioList;
}
#Override
public int getCount() {
return audioList.size();
}
#Override
public Object getItem(int position) {
return position;
}
#Override
public long getItemId(int position) {
return position;
}
public class Holder {
private ImageView imgIcon, imgPlay, imgPause, imgStop;
private MediaPlayer mediaPlayer;
private SeekBar seekbar;
private TextView tvTotalTime,duration;
private double timeElapsed = 0, finalTime = 0;
private int forwardTime = 2000, backwardTime = 2000;
private Handler durationHandler = new Handler();
//handler to change seekBarTime
private Runnable updateSeekBarTime = new Runnable() {
public void run() {
//get current position
timeElapsed = mediaPlayer.getCurrentPosition();
//set seekbar progress
seekbar.setProgress((int) timeElapsed);
tvTotalTime.setText(String.format("%d min, %d sec", TimeUnit.MILLISECONDS.toMinutes((long) finalTime), TimeUnit.MILLISECONDS.toSeconds((long) finalTime)));
//set time remaing
double timeRemaining = finalTime - timeElapsed;
duration.setText(String.format("%d min, %d sec", TimeUnit.MILLISECONDS.toMinutes((long) timeRemaining), TimeUnit.MILLISECONDS.toSeconds((long) timeRemaining) - TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes((long) timeRemaining))));
//repeat yourself that again in 100 miliseconds
durationHandler.postDelayed(this, 100);
}
};
}
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
View myView = convertView;
final Holder holder;
if (myView == null) {
LayoutInflater inflater = LayoutInflater.from(context);
myView = inflater.inflate(R.layout.video_adapter, null);
holder = new Holder();
holder.imgPlay = (ImageView) myView.findViewById(R.id.imgPlay);
holder.imgPause = (ImageView) myView.findViewById(R.id.imgPause);
holder.imgStop = (ImageView) myView.findViewById(R.id.imgStop);
holder.seekbar =(SeekBar)myView.findViewById(R.id.seekbar);
holder.tvTotalTime =(TextView)myView.findViewById(R.id.tvTotalTime);
holder.duration =(TextView)myView.findViewById(R.id.duration);
holder.seekbar.setClickable(true);
holder.seekbar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
#Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
if (fromUser) {
holder.mediaPlayer.seekTo(progress);
}
}
#Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
#Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
});
String fileName = audioList.get(position);
String path="/EmoticApp/AudioFile/";
Log.e("Checking File:", "Checking=======:" + fileName);
flag =true;
holder.mediaPlayer = MediaPlayer.create(context, Uri.parse(Environment.getExternalStorageDirectory().getPath()+path + fileName));
holder.imgPlay.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (flag == true) {
holder.mediaPlayer.start();
if (holder.mediaPlayer.isPlaying()) {
Toast.makeText(context, "Playing", Toast.LENGTH_LONG).show();
holder.imgPlay.setVisibility(View.INVISIBLE);
holder.imgPause.setVisibility(View.VISIBLE);
holder.timeElapsed = holder.mediaPlayer.getCurrentPosition();
holder.seekbar.setProgress((int) holder.timeElapsed);
holder.durationHandler.postDelayed(holder.updateSeekBarTime, 100);
holder.finalTime = holder.mediaPlayer.getDuration();
holder.seekbar.setMax((int) holder.finalTime);
Toast.makeText(context,"Playing",Toast.LENGTH_SHORT).show();
}
}
}
});
holder.imgPause.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
holder.mediaPlayer.pause();
holder.imgPause.setVisibility(View.INVISIBLE);
holder.imgPlay.setVisibility(View.VISIBLE);
}
});
holder.mediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
#Override
public void onCompletion(MediaPlayer mp) {
holder.imgPause.setVisibility(View.INVISIBLE);
holder.imgPlay.setVisibility(View.VISIBLE);
}
});
myView.setTag(holder);
return myView;
}
return myView;
}
}
Use this lines of code:
MediaPlayer mp = new MediaPlayer();
playMySong(String songPath)
{
mp.reset();
mp.setDataSource(songPath);
mp.prepare();
mp.start();
}
Don't create MediaPlayer instance for individual row of list. Let it be property of Adapter because this will help us to detect whether this media player is playing some song currently or not
So do this
public class MyAdapter{
private MediaPlayer mediaPlayer=new MediaPlayer();// I am ignoring exact constructor
//now in your getView method do like this
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
//.............prev code
holder.imgPlay.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (mediaPlayer.isPlaying()) {
mediaPlayer.pause();
holder.imgPause.setVisibility(View.INVISIBLE);
holder.imgPlay.setVisibility(View.VISIBLE);
}else{
mp.reset();
mp.setDataSource(Uri.parse(Environment.getExternalStorageDirectory().getPath()+path + fileName));
mp.prepare();
mp.start();
//set other things
}
}
});
//......some code below
}
}
Hope this will help you !!!!
As you progress in your application, you will find out that there would be other activities or fragment which may try to play some music and you will fall in same circumstances, Otherwise you might think that you just want to stop music as you come out of this list activity.
instead of going though the hustle of managing the instance of MediaPlayer make just make it once in the Application inside a class which will handle your business for playing the music, in your current case what is happening is the MediaPlayer API of android keep getting the request for playing music and by no means that these API can't handle multiple request so they will keep on playing as you command them,
in your custom class you gotta check if your MediaPlayer instance is still playing or not like this...
player.isPlaying()
if it is playing than stop it, like this
player.stop()
and give it some new data what you want it to play.

correctly displays a seekbar in custom adapter

I have a list in which each element on a player. When I click on "start" and a song starts playing strip seekbar starts moving. when click on the "stop" the song stops playing and the strip stops. But if while moving the strip I scroll list and it goes off the screen, it starts to move another strip that is on the same position in the visible area of the screen.
public class RecordAdapter extends BaseAdapter {
private MediaPlayer mediaPlayer;
private LayoutInflater inflater;
private ArrayList<RecordBean> recordBeans;
private final Handler handler = new Handler();
private Context ctx;
private int global_position;
...
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
View view = convertView;
final ViewHolder holder;
if (convertView == null) {
view = inflater.inflate(R.layout.recorditem, parent, false);
holder = new ViewHolder();
holder.date = (TextView) view.findViewById(R.id.recordate);
holder.seekBar = (SeekBar) view.findViewById(R.id.seekBar);
holder.start = (Button) view.findViewById(R.id.btnStart);
holder.stop = (Button) view.findViewById(R.id.btnStop);
view.setTag(holder);
} else {
holder = (ViewHolder) view.getTag();
}
holder.date.setText(" " + recordBeans.get(position).getDate());
holder.start.setTag(position);
holder.stop.setTag(position);
holder.seekBar.setTag(position);
holder.seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
#Override
public void onProgressChanged(final SeekBar seekBar, final int i, boolean b) {
if (b && (Integer)seekBar.getTag()==global_position) {
mediaPlayer.seekTo(i);
}
}
#Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
#Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
});
holder.start.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
releaseMP();
try {
mediaPlayer = new MediaPlayer();
mediaPlayer.setDataSource(recordBeans.get((Integer) v.getTag()).getFile());
mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
mediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
#Override
public void onPrepared(MediaPlayer mediaPlayer) {
mediaPlayer.start();
global_position = position;
holder.start.setTextColor(Color.RED);
holder.seekBar.setMax(mediaPlayer.getDuration());
startPlayProgressUpdater(holder.seekBar,holder.start);
}
});
mediaPlayer.prepareAsync();
} catch (IOException e) {
e.printStackTrace();
}
if (mediaPlayer == null) {
return;
}
mediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
#Override
public void onCompletion(MediaPlayer mediaPlayer) {
Log.d("LOG_TAG", "onCompletion");
holder.start.setTextColor(Color.WHITE);
}
});
}
});
holder.stop.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (mediaPlayer == null)
return;
if ((Integer) v.getTag() == global_position) {
mediaPlayer.stop();
}
}
});
return view;
}
private void releaseMP() {
if (mediaPlayer != null) {
try {
mediaPlayer.release();
mediaPlayer = null;
} catch (Exception e) {
e.printStackTrace();
}
}
}
private static class ViewHolder {
TextView date;
SeekBar seekBar;
Button start;
Button stop;
}
public void startPlayProgressUpdater(final SeekBar seek, final Button start) {
seek.setProgress(mediaPlayer.getCurrentPosition());
if (mediaPlayer.isPlaying()) {
Runnable notification = new Runnable() {
public void run() {
startPlayProgressUpdater(seek,start);
}
};
handler.postDelayed(notification, 1000);
} else {
mediaPlayer.pause();
seek.setProgress(0);
}
}
}
EDIT:
I added geter and seter in recordBean
public int getSeekPos() {
return SeekPos;
}
public void setSeekPos(int seekPos) {
SeekPos = seekPos;
}
and
holder.seekBar.setProgress(recordBeans.get(position).getSeekPos());
holder.date.setText(" " + recordBeans.get(position).getDate());
and
#Override
public void onProgressChanged(final SeekBar seekBar, final int i, boolean b) {
if (b && (Integer)seekBar.getTag()==global_position) {
// mediaPlayer.seekTo(i);
recordBeans.get(position).setSeekPos(i);
}
}
but I think it left something extra in my code
The list item is being recycled when it leaves the screen.
You could try to call this in getView:
holder.seekBar.clearFocus();
This is a tricky process, but you can do this in the following way, as I have seen that you have created a List of RecordBean class. So what you can do is you have to create a Getter Setter in RecordBean for storing the position of you SeekBar like:
public void setSeekPos(int pos) {
this.pos = pos;
}
public int getSeekPos() {
return pos;
}
In your adapter below the line
holder.date.setText(" " + recordBeans.get(position).getDate());
set the progress of seekBar like this: holder.seekBar.setProgress(recordBeans.get(position).getSeekPos());
and inside the onProgressChanged add the following line recordBeans.get(position).setSeekPos(i);
This will resolve your issue.
Please let me know if it's working or not.
Thanks

Categories

Resources