audio player app for android - java

hi i have created a new mp3 player app for my owm. In that i am playing songs from a folder where its been stored, the problem is when one song is in play and when i click the next song both gets played, i want the other one to be stopped. the following is my code
lv.setOnItemClickListener(new OnItemClickListener()
{
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,long arg3)
{
Log.e("", "aaaaa "+arg2);
if(arg2==0)
{
mplayer = MediaPlayer.create(aply.this, R.raw.a);
mplayer.start();
}
else
{
mplayer = MediaPlayer.create(aply.this, R.raw.download);
mplayer.start();
}
mplayer.start();
}
});
pls help me in this....

you can check the playing status, if it is playing you have to stop the player and reset the contents and issue the command Play.
if (mplayer.isPlaying()) {
mp.stop();
}
mplayer.reset();
mplayer.setDataSource(abs_filepath);//set the path
mplayer.prepare();
mplayer.start();
above a sample code

Don't you want a check like this:
if (mplayer != null && mplayer.isPlaying()) {
mplayer.stop();
}
at some point before you start your next song?

Related

MediaPlayer cannot paused in Adapter Recyclerview with once Click Button

please help, I always experience errors here, I create mediaplayer in the audio list using 1 button, I have created the condition and isPlaying function on the mediaplayer in the adapter class, when the audio button is clicked successfully run, but when I click a second time, audio can not be paused, just repeat from the beginning, help me, what is the right solution?
this is my code:
public void onBindViewHolder(#NonNull MurottalAdapter.MurotalHolder holder, int position) {
Item item = itemList.get(position);
holder.titles.setText(item.getTitle());
holder.btnplays.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
//I've tried replacing this with holder.itemview.getContext() and I put it outside the onclick method, but the problem is the same
mediaPlayer = MediaPlayer.create(view.getContext(),item.getAudio());
if (mediaPlayer.isPlaying()){
if (mediaPlayer != null) {
mediaPlayer.pause();
holder.btnplays.setImageResource(R.drawable.ic_play_circle_filled_black_24dp);
}
} else {
if (mediaPlayer != null){
mediaPlayer.start();
holder.btnplays.setImageResource(R.drawable.ic_pause_circle_filled_black_24dp);
}
}
}
});
}
Pls try to move following code outside of onClick() method?
mediaPlayer = MediaPlayer.create(view.getContext(),item.getAudio());

Return MediaController and MediaPlayer to the start of song after finish it

I've created MediaPlayer with MediaControll. It works but I want to return MediaController and MediaPlayer to the start of song after finish it. So when song finished there are pause button and seek bar in the end position are still presented. I want to set seek bar to start position and change button to play(so user can click on it to replay this song) when song finished. I have the same problem with this developer: Android MediaController End Of song . I applied solution of his problem but still can't reset Controller. What's wrong in my code? As I know I should write something in onCompletion method.
mPlayer = new MediaPlayer();
mPlayer.setOnCompletionListener(this);
controller = new MediaController(this){
#Override
public void show(int timeout) {
super.show(0);
}
};
listSongs.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
if (mPlayer != null) {
mPlayer.release();
}
mPlayer = MediaPlayer.create(MainActivity.this, arrayList.get(position).getmSongResourceId());
mPlayer.start();
controller.show(0);
}
});
controller.setMediaPlayer(this);
And onCompletion method
#Override
public void onCompletion(MediaPlayer mp) {
mp.reset();
controller.show(0);
}
Thanks for any help.

How to check if an audio is playing with Android MediaPlayer?

In a situation where I have a listview that in each list item when clicking is an audio and is played when clicked on the desired item.
When you click on multiple items, the audio blends.
I would like to know how I can click on an item and play and if I want another item, when clicking it the previous audio stops and the audio of the clicaco item starts.
I tried to use the code below, but audios do not stop when I click on another item.
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
mediaPlayer = MediaPlayer.create(MainActivity.this, caminhoAudio[position]);
if ( !(mediaPlayer.isPlaying()) )
{
tocarSom();
}
}
});
}
public void tocarSom() {
if (mediaPlayer != null)
{
mediaPlayer.start();
}
// LIBERAR MEMÓRIA
mediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
public void onCompletion(MediaPlayer mediaPlayer) {
mediaPlayer.release();
};
});
}
Thank You!!!
Well, if your methode startAudio() automatically do everything needed to start to play a song, you have to call startAudio() after the mediaPlayer.release();
Do something like this.
if(mMediaPlayer!=null)
{
mMediaPlayer.release();
mMediaPlayer=null;
}
if(mMediaPlayer != null) {
//Fist stop the current playing raw file
mMediaPlayer.stop();
mMediaPlayer.release();
mMediaPlayer = null;
//Then Play the selected raw file.
tocarSom();
} else {
//If nothing is playing then Play the selected raw file or audio
tocarSom();
}

Each item of a listView playing a different (short) media file. After so many clips I hear no more sound

I am making a sound board app. I have a String List of sound descriptions that populate the listView. My onItemClick takes in a switch statement that assigns each sound to it's string description from the list. The sounds work when the list item is clicked, but after so many clicks, all the sounds stop. the switch statement looks like this in my ItemOnClick:
switch (soundName){
case "magic whoosh":
mediaPlayer = MediaPlayer.create(context, R.raw.magic_whoosh);
mediaPlayer.start();
break;
case "magic poof":
mediaPlayer = MediaPlayer.create(context, R.raw.magic_poof);
mediaPlayer.start();
break;
}
As requested a possible answer;
//declare instance variable
static MediaPlayer mediaPlayer;
//handle listview on item click
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
if(mediaPlayer==null){
mediaPlayer=new MediaPlayer();
try{
mediaPlayer.setDataSource(//datasource based on position);
mediaPlayer.prepare(); //this uses the same UI thread
mediaPlayer.start();
}catch(Throwable e){
//handle possible errors
}
}else{
if(mediaPlayer.isPlaying())mediaPlayer.stop();
mediaPlayer.reset();
try{
mediaPlayer.setDataSource(//datasource based on position);
mediaPlayer.prepare(); //this uses the same UI thread
mediaPlayer.start();
}catch(Throwable e){
//handle possible errors
}
}
}
});

How to associate .mp3 files in Android Studio

I want to play an mp3 file based on a list view item click however based on my code went I run my app this window appears and so due to lack of an audio option I really don't know which one of these I need to select in order to associate my .mp3 files.
mainList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) {
//When clicked, go to specific activity
if (position == 0) {
if(mp.isPlaying())
{
mp.stop();
mp.reset();
}
try {
AssetFileDescriptor afd = getAssets().openFd("chimes.mp3");
mp.setDataSource(afd.getFileDescriptor(),afd.getStartOffset(),afd.getLength());
mp.prepare();
mp.start();
} catch (Exception e) {
e.printStackTrace();
}
}
}
});
You don't have to associate anything this is only used to OPEN the file on the host machine, has nothing to do with the device.
you can drag and drop mp3 file in res folder android studio automatically dectect it

Categories

Resources