I am developing an app in which I have to fetch audio from firebase. I am using firebase recycler view. I am getting a problem that when I click the first song it plays good, but when I click second item then the first song's play button does not change and seekbar of first and second song changes.
Here is the code.
holder.play_btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String audioUrl3 = model.getMedia();
if (!isPlaying){
if (mPlayer!=null){
mPlayer.release();
}
Toast.makeText(mContext, "Loading....", Toast.LENGTH_SHORT).show();
isPlaying = true;
Uri uri = Uri.parse(audioUrl3);
mPlayer = new MediaPlayer();
mPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
mPlayer.reset();
try {
mPlayer.setDataSource(mContext,uri);
} catch (Exception e) {
e.printStackTrace();
Toast.makeText(mContext, "Error Occured", Toast.LENGTH_SHORT).show();
}
try{
mPlayer.prepareAsync();
}catch (Exception e){
Toast.makeText(mContext, "Error Occured", Toast.LENGTH_SHORT).show();
holder.play_btn.setImageDrawable(holder.play_btn.getContext().getResources().getDrawable(R.drawable.ic_play));
}
mPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
#Override
public void onPrepared(MediaPlayer mp) {
if(mPlayer.isPlaying()){
mPlayer.stop();
}
mPlayer.start();
holder.play_btn.setImageDrawable(holder.play_btn.getContext().getResources().getDrawable(R.drawable.ic_pause));
holder.seekBar.setMax(mPlayer.getDuration());
Toast.makeText(mContext, "Playing...", Toast.LENGTH_SHORT).show();
updateSeekBar(holder.seekBar);
}
});
mPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
#Override
public void onCompletion(MediaPlayer mp) {
holder.play_btn.setImageDrawable(holder.play_btn.getContext().getResources().getDrawable(R.drawable.ic_play));
isPlaying = false;
}
});
}
else {
isPlaying=false;
stopPlaying();
}
}
private void stopPlaying() {
mPlayer.release();
mPlayer=null;
holder.play_btn.setImageDrawable(holder.play_btn.getContext().getResources().getDrawable(R.drawable.ic_play));
}
});
}
private void updateSeekBar(final SeekBar seek) {
try{
int pos = mPlayer.getCurrentPosition();
seek.setProgress(pos);
}catch (Exception e){
}
runnable = new Runnable() {
#Override
public void run() {
updateSeekBar(seek);
}
};
handler.postDelayed(runnable, 1800);
}
Related
the issue is, I have checked whether media player is null or not, if not I have applied release() method but still when I select another song to play, it plays new song but it doesn't stop the previous one. While playing next or previous on click also works fine. so why it is not detecting that mediaplayer is playing.
here is java code.
public class Musicplay extends AppCompatActivity implements View.OnClickListener {
Button play,next,previous;
TextView songmar;
SeekBar seekBar;
MediaPlayer mediaPlayer;
int position,idr;
Handler handler=new Handler();
Handler vindler=new Handler();
Uri uri;
int max;
#Override
protected void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.playdisplay);
Intent i=getIntent();
position=i.getIntExtra("position",0);
play=findViewById(R.id.pause);
next=findViewById(R.id.next);
previous=findViewById(R.id.previous);
songmar=findViewById(R.id.songnamemar);
songmar.setSelected(true);
songmar.setText(Musiclist.songname.get(position));
seekBar=findViewById(R.id.seekBar);
if(mediaPlayer==null)
{
Log.d("null","null");
}
else {
if(mediaPlayer.isPlaying())
{
mediaPlayer.stop();
mediaPlayer.release();
}
else {
mediaPlayer.release();
}
}
uri=Uri.parse(String.valueOf(new File(Musiclist.songpath.get(position))));
mediaPlayer=MediaPlayer.create(Musicplay.this,uri);
seekBar.setMax(mediaPlayer.getDuration());
mediaPlayer.start();
max=mediaPlayer.getDuration();
play.setBackgroundResource(R.drawable.ic_pause);
Thread thread=new Thread(){
#Override
public void run() {
super.run();
int currentposition=0;
while (currentposition<max)
{
try {
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
currentposition=mediaPlayer.getCurrentPosition();
seekBar.setProgress(currentposition);
}
}
};
thread.start();
seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
#Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
}
#Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
#Override
public void onStopTrackingTouch(SeekBar seekBar) {
mediaPlayer.seekTo(seekBar.getProgress());
}
});
next.setOnClickListener(Musicplay.this);
play.setOnClickListener(Musicplay.this);
previous.setOnClickListener(Musicplay.this);
mediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
#Override
public void onCompletion(MediaPlayer mp) {
mp.stop();
mp.reset();
if(position<(Musiclist.songname.size()-1)){
position=position +1;
}
else
{
position=0;
}
uri = Uri.parse(String.valueOf(new File(Musiclist.songpath.get(position))));
try {
mp.setDataSource(Musicplay.this,uri);
} catch (IOException e) {
e.printStackTrace();
}
songmar.setText(Musiclist.songname.get(position));
songmar.setSelected(true);
try {
mp.prepare();
} catch (IOException e) {
e.printStackTrace();
}
}
});
mediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
#Override
public void onPrepared(MediaPlayer mp) {
mp.start();
seekBar.setMax(mp.getDuration());
max=mp.getDuration();
Log.d("seekvalue",String.valueOf(max));
}
});
}
#Override
public void onClick(View v) {
idr=v.getId();
Thread thread2=new Thread(){
#Override
public void run() {
super.run();
handler.post(new Runnable() {
#Override
public void run() {
switch (idr){
case R.id.pause:
if(mediaPlayer.isPlaying()){
mediaPlayer.pause();
play.setBackgroundResource(R.drawable.ic_play);
}
else {
play.setBackgroundResource(R.drawable.ic_pause);
mediaPlayer.start();
}
break;
case R.id.next:
mediaPlayer.stop();
mediaPlayer.reset();
if(position<(Musiclist.songname.size()-1)){
position=position +1;
Uri uri=Uri.parse(String.valueOf(new File(Musiclist.songpath.get(position))));
try {
mediaPlayer.setDataSource(Musicplay.this,uri);
} catch (IOException e) {
e.printStackTrace();
}
int a=0;
for(int i=0;i<150;i++)
{
a=a+1;
}
try {
mediaPlayer.prepare();
} catch (IOException e) {
e.printStackTrace();
}
play.setBackgroundResource(R.drawable.ic_pause);
songmar.setText(Musiclist.songname.get(position));
songmar.setSelected(true);
}
else {
position=0;
Uri uri=Uri.parse(String.valueOf(new File(Musiclist.songpath.get(position))));
try {
mediaPlayer.setDataSource(Musicplay.this,uri);
} catch (IOException e) {
e.printStackTrace();
}
int a=0;
for(int i=0;i<150;i++)
{
a=a+1;
}
try {
mediaPlayer.prepare();
} catch (IOException e) {
e.printStackTrace();
}
play.setBackgroundResource(R.drawable.ic_pause);
songmar.setText(Musiclist.songname.get(position));
songmar.setSelected(true);
}
break;
case R.id.previous:
mediaPlayer.stop();
mediaPlayer.reset();
if(position>0){
position=position-1;
Uri uri=Uri.parse(String.valueOf(new File(Musiclist.songpath.get(position))));
try {
mediaPlayer.setDataSource(Musicplay.this,uri);
} catch (IOException e) {
e.printStackTrace();
}
int a=0;
for(int i=0;i<150;i++)
{
a=a+1;
}
try {
mediaPlayer.prepare();
} catch (IOException e) {
e.printStackTrace();
}
play.setBackgroundResource(R.drawable.ic_pause);
songmar.setText(Musiclist.songname.get(position));
songmar.setSelected(true);
}
else {
position=Musiclist.songname.size()-1;
Uri uri=Uri.parse(String.valueOf(new File(Musiclist.songpath.get(position))));
try {
mediaPlayer.setDataSource(Musicplay.this,uri);
} catch (IOException e) {
e.printStackTrace();
}
int a=0;
for(int i=0;i<150;i++)
{
a=a+1;
}
try {
mediaPlayer.prepare();
} catch (IOException e) {
e.printStackTrace();
}
play.setBackgroundResource(R.drawable.ic_pause);
songmar.setText(Musiclist.songname.get(position));
songmar.setSelected(true);
}
break;
}
}
});
}
};
thread2.start();
}
}
so do suggest reason why this error is taking place, and it is playing two or multiple song simultaneously.
all you have to do is, just declare mediaplayer variable public static and when you start activity to play song just check for media player is null or not and then release it
I am having 3 tones. I am calling playmusic() function to play 2 of these tones on imageview toggle. when i click on imageview for first time tone1 starts playing. when i click 2nd time on the same imageview tone2 starts playing, but tone1 is not stopped.
my requiremet is , when i click on imageview for -
(1st time - tone1 should start playing).
(2nd time - tone1 should stop playing and tone2 should start playing)
(3rd time - tone2 should stop playing and tone1 should start playing again)
and so on....
String file=null;
int x=0;
public void playMusic(int i) {
x=i;
if(i==1){
file = R.raw.tone1;
}else if(i==2){
file = R.raw.tone2;
}else{
file = R.raw.tone3;
}
mediaPlayer = new MediaPlayer();
mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
AssetFileDescriptor assetFileDescriptor =
Main2Activity.this.getResources().openRawResourceFd(file);
if(assetFileDescriptor == null) return;
try {
mediaPlayer.setDataSource(
assetFileDescriptor.getFileDescriptor(),
assetFileDescriptor.getStartOffset(),
assetFileDescriptor.getLength()
);
assetFileDescriptor.close();
mediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
#Override
public void onCompletion(MediaPlayer mediaPlayer) {
if(mediaPlayer != null){
mediaPlayer.release();
mediaPlayer = null;
}
}
});
mediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
#Override
public void onPrepared(final MediaPlayer mediaPlayer) {
mediaPlayer.start();
}
});
mediaPlayer.prepareAsync();
} catch (IllegalArgumentException e) {
// HelpFunctions.showLog("ERROR = " + e);
} catch (IllegalStateException e) {
// HelpFunctions.showLog("ERROR = " + e);
} catch (IOException e) {
// HelpFunctions.showLog("ERROR = " + e);
}
}
I did a small modification on your logic and added mediaplayer.reset(). Here is the solution link
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final ImageView player = findViewById(R.id.play);
player.setOnClickListener(new View.OnClickListener() {
#Override public void onClick(View v) {
playMusic(1);
}
});
mediaPlayer = new MediaPlayer();
}
int file = 0;
MediaPlayer mediaPlayer;
int x = 0;
public void playMusic(int i) {
x = x + i;
if (x == 1) {
file = R.raw.tone1;
}
else if (x == 2) {
file = R.raw.tone2;
} else {
x = 0;
file = R.raw.tone3;
}
mediaPlayer.reset();
mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
AssetFileDescriptor assetFileDescriptor =
MainActivity.this.getResources().openRawResourceFd(file);
if (assetFileDescriptor == null) return;
try {
mediaPlayer.setDataSource(
assetFileDescriptor.getFileDescriptor(),
assetFileDescriptor.getStartOffset(),
assetFileDescriptor.getLength()
);
assetFileDescriptor.close();
mediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
#Override
public void onCompletion(MediaPlayer mediaPlayer) {
if (mediaPlayer != null) {
mediaPlayer.release();
}
}
});
mediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
#Override
public void onPrepared(final MediaPlayer mediaPlayer) {
mediaPlayer.start();
}
});
mediaPlayer.prepareAsync();
} catch (IllegalArgumentException e) {
// HelpFunctions.showLog("ERROR = " + e);
} catch (IllegalStateException e) {
// HelpFunctions.showLog("ERROR = " + e);
} catch (IOException e) {
// HelpFunctions.showLog("ERROR = " + e);
}
}
}
when i click 2nd time on the same imageview tone2 starts playing, but tone1 is not stopped.
If that's the only issue, try putting the following line before you call mediaPlayer = new MediaPlayer();:
if (mediaPlayer != null && mediaPlayer.isPlaying()) mediaPlayer.stop();
I want to play/stream audio from url one after other automatically.
here what I have tried:
final String audioUrl[] = {"https://www.googleapis.com/storage/v1/b/ezconnectpoc/o/NV.wav?alt=media", "https://www.googleapis.com/storage/v1/b/ezconnectpoc/o/cqo.wav?alt=media"};
backtobackButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
try {
mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
mediaPlayer.setDataSource(audioUrl[currentTrack]);
mediaPlayer.prepareAsync();
mediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
#Override
public void onPrepared(MediaPlayer mp) {
mediaPlayer.start();
}
});
mediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
#Override
public void onCompletion(MediaPlayer mp) {
try {
Log.d("Current track",currentTrack+"");
Log.d("audioUrl.length",audioUrl.length+"");
if (currentTrack < audioUrl.length) {
currentTrack++;
mediaPlayer.setDataSource(audioUrl[currentTrack]);
mediaPlayer.setOnCompletionListener(this);
mediaPlayer.start();
}
mediaPlayer.stop();
} catch (Exception e) {
e.printStackTrace();
}
}
});
} catch (Exception e) {
e.printStackTrace();
// make something
}
}
});
My first audio url gets played when I click on backtoback named button but failed to play second audio from url.
changed this part of code
mediaPlayer.reset();
mediaPlayer.setDataSource(audioUrl[currentTrack]);
mediaPlayer.prepareAsync();
mediaPlayer.setOnCompletionListener(this);
mediaPlayer.start();
currentTrack++;
and worked.
play.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
MediaPlayer mediaPlayer=new MediaPlayer();
try {
mediaPlayer.setDataSource("https://firebasestorage.googleapis.com/v0/b/photo-on-birthday-cake-cccf5.appspot.com/o/birthday%20(1).mp3?alt=media&token=c1e988b4-f482-4020-8057-53c68195d2fc");
mediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
#Override
public void onPrepared(MediaPlayer mediaPlayer) {
if(flag==0) {
mediaPlayer.start();
Toast.makeText(MusicActivity.this, "start", Toast.LENGTH_SHORT).show();
flag=1;
play.setBackgroundResource(R.drawable.ic_pause_circle_outline_black_24dp);
} else {
mediaPlayer.pause();
Toast.makeText(MusicActivity.this, "Stop", Toast.LENGTH_SHORT).show();
flag=0;
}
}
});
mediaPlayer.prepare();
}
catch (IOException e){
e.printStackTrace();
}
}
});
I tried this code but this is not working on pause function while working on play.
I read through a tutorial and created a simple music player dubbed SimplePlayer. When I click the song I want to play, it plays perfectly fine.
However, when that song finishes, instead of playing the next song it plays the second song in the list, then keeps playing it over and over again.
I know that the choosing of the song when the first song finishes is inside the onCompletion method with the mp.setDataSource, but I'm not sure what I need to put in the brackets other than the +1 so that it plays the next song in the list:
public void onCompletion(MediaPlayer mp) {
try {
mp.reset();
mp.setDataSource(SD_PATH + songs.get(+1));
mp.prepare();
mp.start();
play.setEnabled(false);
pause.setEnabled(true);
stop.setEnabled(true);
} catch(IOException e) {
Log.v(getString(R.string.app_name), e.getMessage());
}
Here is the rest of the code in case I have to change something there as well:
public class MainActivity extends ListActivity implements OnCompletionListener {
private static final String SD_PATH = new String(Environment.getExternalStorageDirectory().getPath() + "/");
private List<String> songs = new ArrayList<String>();
private MediaPlayer mp = new MediaPlayer();
private View play;
private View pause;
private View stop;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
updatePlaylist();
play = (ImageButton)findViewById(R.id.imageButton1);
pause = (ImageButton)findViewById(R.id.imageButton2);
stop = (ImageButton)findViewById(R.id.imageButton3);
mp.setOnCompletionListener(this);
play.setEnabled(false);
pause.setEnabled(false);
stop.setEnabled(false);
}
#Override
protected void onListItemClick(ListView list, View view, int position, long id) {
try {
mp.reset();
mp.setDataSource(SD_PATH + songs.get(position));
mp.prepare();
mp.start();
play.setEnabled(false);
pause.setEnabled(true);
stop.setEnabled(true);
} catch(IOException e) {
Log.v(getString(R.string.app_name), e.getMessage());
}
}
private void updatePlaylist() {
File home = new File(SD_PATH);
if (home.listFiles(new Mp3Filter()).length > 0) {
for (File file : home.listFiles( new Mp3Filter())) {
songs.add(file.getName());
}
ArrayAdapter<String> songList = new ArrayAdapter<String>(this,R.layout.song_item,songs);
setListAdapter(songList);
}
}
public void play(View view){
Toast.makeText(getApplicationContext(), "Playing song",
Toast.LENGTH_SHORT).show();
mp.start();
play.setEnabled(false);
pause.setEnabled(true);
stop.setEnabled(true);
}
public void pause(View view){
Toast.makeText(getApplicationContext(), "Pausing song",
Toast.LENGTH_SHORT).show();
mp.pause();
play.setEnabled(true);
pause.setEnabled(false);
stop.setEnabled(true);
}
public void stop(View view){
Toast.makeText(getApplicationContext(), "Stopping song",
Toast.LENGTH_SHORT).show();
mp.stop();
play.setEnabled(false);
pause.setEnabled(false);
stop.setEnabled(false);
}
#Override
public void onCompletion(MediaPlayer mp) {
try {
mp.reset();
mp.setDataSource(SD_PATH + songs.get(+1));
mp.prepare();
mp.start();
play.setEnabled(false);
pause.setEnabled(true);
stop.setEnabled(true);
} catch(IOException e) {
Log.v(getString(R.string.app_name), e.getMessage());
}
}
}
also, how can I make it so that when the app runs, it lists all songs on the sdcard, the phone storage and in the folders on the sdcard? At the moment it only lists the songs on the root of the sdcard.
What you have written is for continuously playing second song.
You need to use a global variable to indicate song number and increment it and pass that to setDataSource();
First,create a public global variable
public static int SONG_NUMBER=0;
In your onCompletion method
#Override
public void onCompletion(MediaPlayer mp) {
try {
mp.reset();
if(SONG_NUMBER == songs.size())
{
SONG_NUMBER=0;
}
else
{
SONG_NUMBER=SONG_NUMBER+1;
}
mp.setDataSource(SD_PATH + songs.get(SONG_NUMBER));// pass SONG_NUMBER++ instead +1
mp.prepare();
mp.start();
play.setEnabled(false);
pause.setEnabled(true);
stop.setEnabled(true);
} catch(IOException e) {
Log.v(getString(R.string.app_name), e.getMessage());
}
}
In your on ListItemClick method,assign SONG_NUMBER based on position
#Override
protected void onListItemClick(ListView list, View view, int position, long id) {
try {
SONG_NUMBER=position; // <------------------------add this line
mp.reset();
mp.setDataSource(SD_PATH + songs.get(position));
mp.prepare();
mp.start();
play.setEnabled(false);
pause.setEnabled(true);
stop.setEnabled(true);
} catch(IOException e) {
Log.v(getString(R.string.app_name), e.getMessage());
}
}