I have a listview and MediaPlayer created in OnItemClick.
listSongs.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Song song = mAdapter.getItem(position);
mPlayer = MediaPlayer.create(MainActivity.this, song.getmSongResourceId());
if (mPlayer != null) {
mPlayer.release();
}
mPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener () {
#Override
public void onCompletion(MediaPlayer mp) {
mPlayer.start();
mPlayer.pause();
controller.show(0);
}
});
mPlayer.start(); //error here
controller.show(0);
}
});
#Override
public void start() {
mPlayer.start();
}
#Override
protected void onStop() {
super.onStop();
mPlayer.pause();
controller.hide();
}
When ckick on item activity close. Here is logcat:
java.lang.IllegalStateException
at android.media.MediaPlayer._start(Native Method)
at android.media.MediaPlayer.start(MediaPlayer.java:1228)
at com.....MainActivity$2.onItemClick(MainActivity.java:432)
at android.widget.AdapterView.performItemClick(AdapterView.java:314)
at android.widget.AbsListView.performItemClick(AbsListView.java:1162)
at java.lang.reflect.Method.invoke(Native Method)
MainActivity.java:432 is mPlayer.start();.
What is IllegalStateException, and how can fix it? You may say my post is this post duplicate, but I'm not sure how to apply it solutions to my code.
Try
mPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener () {
#Override
public void onCompletion(MediaPlayer mp) {
mp.start();
mp.pause();
controller.show(0);
}
});
Related
Being a novice coder, trying to code an app that plays a sound on buttonclick in a fragment; successfully did it but I want to add more small pieces of sounds to play one after another. How can I add those sounds(sound2, sound3, sound4, etc) after that 'R.raw.sound1'? Please guide..
buttonPlayAudioVar.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if(mp != null) {
if(mp.isPlaying()){
mp.stop();
mp.release();
}
}
mp = MediaPlayer.create(getActivity().getApplicationContext(), R.raw.sound1);
mp.start();
mp.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
public void onCompletion(MediaPlayer mediaPlayer) {
mp.release();
mp = null;
}
});
}
});
//you can add all the songs in a array
ArrayList<String> songsArray = new ArrayList<>();
int position = 0;
buttonPlayAudioVar.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
mediaPlayer(songsArray, position);
}
});
private void mediaPlayer(ArrayList<String> songsArray) {
if (mp != null) {
if (mp.isPlaying()) {
mp.stop();
mp.release();
}
}
if (position > songsArray.size() - 1) {
return;
}
try {
mp.setDataSource(songsArray.get(position));
position++;
mp.prepareAsync();
} catch (IOException e) {
e.printStackTrace();
}
mp.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
#Override
public void onPrepared(MediaPlayer mp) {
mp.start();
}
});
mp.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
public void onCompletion(MediaPlayer mediaPlayer) {
mp.release();
mp = null;
mediaPlayer(songsArray, position);
}
});
}
I use thread to update the SeekBar. If I don't start the thread then this problem does not arise. What can I do now...? I only experience a problem with this part of my project.
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
if (mp != null) {
mp.stop();
mp.release();
toast("Second Time");
}
u = Uri.parse(collectedSong.get(position).toString());
mp = MediaPlayer.create(getApplicationContext(), u);
mp.start();
sb.setMax(mp.getDuration());
**updateSeekBar.start();**
positionNew = position;
}
});
Check out MediaPlayer lifecycle in Android documents:
https://developer.android.com/reference/android/media/MediaPlayer.html
this is how I use media player to play stream Song:
public void onSongClicked(Song song) {
//check if is playing
stopMediaPlayer();
// prepare media player to play sample
mMediaPlayer = new MediaPlayer();
mMediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
mMediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
#Override
public void onPrepared(MediaPlayer mp) {
mp.start();
}
});
mMediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
#Override
public void onCompletion(MediaPlayer mp) {
mp.release();
mMediaPlayer = null;
}
});
try {
mMediaPlayer.setDataSource(song.getPath());
mMediaPlayer.prepareAsync();
} catch (IOException e) {
e.printStackTrace();
}
}
#Override
public void stopMediaPlayer() {
if (mMediaPlayer != null) {
mMediaPlayer.stop();
mMediaPlayer.release();
mMediaPlayer = null;
}
}
I hope it helps :)
#Override
protected void onResume() {
playOrderSound();
super.onResume();
}
private void playOrderSound(Context context){
player= MediaPlayer.create(context, R.raw.order);
player.setAudioStreamType(AudioManager.STREAM_MUSIC);
player.start();
player.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
#Override
public void onCompletion(MediaPlayer mp) {
player.reset();
player.release();
}
});
player.setOnErrorListener(new MediaPlayer.OnErrorListener() {
#Override
public boolean onError(MediaPlayer mp, int what, int extra) {
player.start();
return false;
}
});
}
I put the sound source in the raw directory and use mediaPlayer to play it.When the server send message to me.I start a new activity and play the mp3 resource,but it does't work.
the new activity start with lauchmode singletask.
Try this Playing Well
#Override
protected void onResume() {
super.onResume();
playOrderSound(Mainactivity.this);
}
private void playOrderSound(Context context){
player = MediaPlayer.create(context, R.raw.acc);
player.start();
player.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
#Override
public void onCompletion(MediaPlayer mp) {
player.reset();
player.release();
}
});
player.setOnErrorListener(new MediaPlayer.OnErrorListener() {
#Override
public boolean onError(MediaPlayer mp, int what, int extra) {
player.start();
return false;
}
});
}
protected void onDestroy() {
super.onDestroy();
player.stop();
}
I am fairly new to programming and having a tough time with a small problem. I have created an object which is initialized easily and starts working, how do i stop that object from functioning.
Following is my code:
btn.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
animationFart();
playRandomSound();
}
private void playRandomSound() {
// TODO Auto-generated method stub
int randomInt = (new Random().nextInt(soundList.size()));
int sound = soundList.get(randomInt);
MediaPlayer mp = MediaPlayer.create(btn.getContext(), sound);
mp.start();
mp.setOnCompletionListener(new OnCompletionListener() {
public void onCompletion(MediaPlayer mp) {
mp.release();
}
});
}
});
How can i stop this with the press of a button.
Define MediaPlayer mp; globally and use mp.stop();:
if (mp!=null && mp.isPlaying()) {
mp.stop();
}
use this class
public class AudioPlayer {
private MediaPlayer mediaPlayer;
public void play(Context context, int resId) {
stop();
mediaPlayer = MediaPlayer.create(context, resId);
mediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
#Override
public void onCompletion(MediaPlayer mediaPlayer) {
stop();
}
});
mediaPlayer.start();
}
public void stop() {
if (mediaPlayer != null) {
mediaPlayer.release();
mediaPlayer = null;
}
}
public boolean isPlaying() {
return mediaPlayer != null;
}
}
I have a simple listview with the name of some songs. When you click on one of the childs, the song plays. However, I noticed that when you accidentally tap it again while its playing, it start to play again causing the sounds to overlap. How can I set it up so that when you click on one song and then click on another song, the first song stop and the second song begins. How can I modify my code to accomplish this. Any help would be appreciated.
public class TwelveGrammar extends ActionBarActivity {
private ListView lv;
private MediaPlayer mp;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.twelve_grm);
lv = (ListView) findViewById(R.id.list);
ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String> (
this, android.R.layout.simple_list_item_1, Exp_list);
lv.setAdapter(arrayAdapter);
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View view, int pos,
long id) {
if (mp.isPlaying()) {
mp.stop();
}
else {
if (pos==0) {
mp = MediaPlayer.create(getBaseContext(), R.raw.g_1200);
mp.start();
mp.setOnCompletionListener(new OnCompletionListener() {
public void onCompletion(MediaPlayer mp) {
mp.release();
}
});
}
if (pos==1) {
mp = MediaPlayer.create(getBaseContext(), R.raw.g_1201);
mp.start();
mp.setOnCompletionListener(new OnCompletionListener() {
public void onCompletion(MediaPlayer mp) {
mp.release();
}
});
}
if (pos==2) {
mp = MediaPlayer.create(getBaseContext(), R.raw.g_1202);
mp.start();
mp.setOnCompletionListener(new OnCompletionListener() {
public void onCompletion(MediaPlayer mp) {
mp.release();
}
});
}
if (pos==3) {
mp = MediaPlayer.create(getBaseContext(), R.raw.g_1203);
mp.start();
mp.setOnCompletionListener(new OnCompletionListener() {
public void onCompletion(MediaPlayer mp) {
mp.release();
}
});
}
if (pos==4) {
mp = MediaPlayer.create(getBaseContext(), R.raw.g_1204);
mp.start();
mp.setOnCompletionListener(new OnCompletionListener() {
public void onCompletion(MediaPlayer mp) {
mp.release();
}
});
}
if (pos==5) {
mp = MediaPlayer.create(getBaseContext(), R.raw.g_1205);
mp.start();
mp.setOnCompletionListener(new OnCompletionListener() {
public void onCompletion(MediaPlayer mp) {
mp.release();
}
});
}
if (pos==6) {
mp = MediaPlayer.create(getBaseContext(), R.raw.g_1206);
mp.start();
mp.setOnCompletionListener(new OnCompletionListener() {
public void onCompletion(MediaPlayer mp) {
mp.release();
}
});
}
if (pos==7) {
mp = MediaPlayer.create(getBaseContext(), R.raw.g_1207);
mp.start();
mp.setOnCompletionListener(new OnCompletionListener() {
public void onCompletion(MediaPlayer mp) {
mp.release();
}
});
}
if (pos==8) {
mp = MediaPlayer.create(getBaseContext(), R.raw.g_1208);
mp.start();
mp.setOnCompletionListener(new OnCompletionListener() {
public void onCompletion(MediaPlayer mp) {
mp.release();
}
});
}
if (pos==9) {
mp = MediaPlayer.create(getBaseContext(), R.raw.g_1209);
mp.start();
mp.setOnCompletionListener(new OnCompletionListener() {
public void onCompletion(MediaPlayer mp) {
mp.release();
}
});
}
if (pos==10) {
mp = MediaPlayer.create(getBaseContext(), R.raw.g_1210);
mp.start();
mp.setOnCompletionListener(new OnCompletionListener() {
public void onCompletion(MediaPlayer mp) {
mp.release();
}
});
}
if (pos==11) {
mp = MediaPlayer.create(getBaseContext(), R.raw.g_1211);
mp.start();
mp.setOnCompletionListener(new OnCompletionListener() {
public void onCompletion(MediaPlayer mp) {
mp.release();
}
});
}
if (pos==12) {
mp = MediaPlayer.create(getBaseContext(), R.raw.g_1212);
mp.start();
mp.setOnCompletionListener(new OnCompletionListener() {
public void onCompletion(MediaPlayer mp) {
mp.release();
}
});
}
if (pos==13) {
mp = MediaPlayer.create(getBaseContext(), R.raw.g_1213);
mp.start();
mp.setOnCompletionListener(new OnCompletionListener() {
public void onCompletion(MediaPlayer mp) {
mp.release();
}
});
}
if (pos==14) {
mp = MediaPlayer.create(getBaseContext(), R.raw.g_1214);
mp.start();
mp.setOnCompletionListener(new OnCompletionListener() {
public void onCompletion(MediaPlayer mp) {
mp.release();
}
});
}
if (pos==15) {
mp = MediaPlayer.create(getBaseContext(), R.raw.g_1215);
mp.start();
mp.setOnCompletionListener(new OnCompletionListener() {
public void onCompletion(MediaPlayer mp) {
mp.release();
}
});
}
}
I think you should have one boolean like boolean isAnotherSongPlaying and if it's true the previous song should be stopped and the new one should start. And I believe that the best way to handle mediaplayer is a Service. I'm not yet too familiarized with that but I have implemented a Service for background music and its control depending on user's actions and it works quite well.
MyService.class
import android.app.Service;
import android.content.Intent;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnCompletionListener;
import android.os.IBinder;
public class MyService extends Service implements OnCompletionListener {
MediaPlayer mediaPlayer;
#Override
public IBinder onBind(Intent intent) {
return null;
}
#Override
public void onCreate() {
mediaPlayer = MediaPlayer.create(this, R.raw.music);// raw/s.mp3
mediaPlayer.setLooping(true);
mediaPlayer.setOnCompletionListener(this);
}
#Override
public int onStartCommand(Intent intent, int flags, int startId) {
if (!mediaPlayer.isPlaying()) {
mediaPlayer.start();
}
return START_STICKY;
}
#Override
public void onDestroy() {
super.onDestroy();
if (mediaPlayer.isPlaying()) {
mediaPlayer.stop();
}
mediaPlayer.release();
}
public void onCompletion(MediaPlayer _mediaPlayer) {
stopSelf();
}
}
in my main activity I have one button which starts and stops the service depending whether the music is playing or not
Intent play;
play = new Intent(getApplicationContext(), MyService.class);
...
sound.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View v) {
if(isPlaying){
stopService(play);
isPlaying = false;
sound.setText("Yes");
} else {
startService(play);
isPlaying = true;
sound.setText("No");
}
}
});
Search it also on google you'll find some good tutorials. Hope I helped you.