I tried all the solutions provided in this site & still i am getting this error.Don't know why it happen! please help me.
Here my code.
MediaPlayer mediaPlayer;
mediaPlayer = MediaPlayer.create(getApplicationContext(), R.raw.square);
mediaPlayer.setOnErrorListener(MainActivity.this);
mediaPlayer.start();
mediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
#Override
public void onCompletion(MediaPlayer mp) {
mWinMusic[blocks].release();
}
});
public boolean onError(MediaPlayer mp, int what, int extra) {
mp = MediaPlayer.create(getApplicationContext(), R.raw.square);
mp.start();
return true;
}
Create player like this and add mediaPlayer.prepare(); and when ready then start in onPrepared()
public class MainActivity extends Activity {
MediaPlayer mediaPlayer;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
try {
mediaPlayer = MediaPlayer.create(getApplicationContext(), R.raw.square);
mediaPlayer.prepare();
mediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
#Override
public void onPrepared(MediaPlayer mp) {
mediaPlayer.start();
}
});
mediaPlayer.setOnErrorListener(new MediaPlayer.OnErrorListener() {
#Override
public boolean onError(MediaPlayer mp, int what, int extra) {
Toast.makeText(MainActivity.this, "Some problem ", Toast.LENGTH_LONG).show();
return false;
}
});
mediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
#Override
public void onCompletion(MediaPlayer mp) {
Toast.makeText(MainActivity.this, "track completed", Toast.LENGTH_SHORT).show();
//mWinMusic[blocks].release();
}
});
} catch (Exception e) {
Toast.makeText(MainActivity.this, "Some problem " + e.getMessage(), Toast.LENGTH_LONG).show();
}
}
}
Related
#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();
}
this is the method am using..and will call whenever required...in my case i want to control that audio by one button whether it may toggle or button...
click to start and stop in one button
private void playSound() {
if (isFlashLightOn) {
mp = MediaPlayer.create(MainActivity.this, R.raw.light_switch_off);
} else {
mp = MediaPlayer.create(MainActivity.this, R.raw.light_switch_on);
}
mp.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
#Override
public void onCompletion(MediaPlayer mp) {
// TODO Auto-generated method stub
mp.release();
}
});mp.start(); playing=true;
}
An edited version of your code
private void playSound() {
if (isFlashLightOn) {
mp = MediaPlayer.create(MainActivity.this, R.raw.light_switch_off);
} else {
mp = MediaPlayer.create(MainActivity.this, R.raw.light_switch_on);
}
mp.prepareAsync(); //prepares the MediaPlayer
mp.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
#Override
public void onPrepared(MediaPlayer mediaPlayer) {
//MediaPlayer instance prepared
mp.start(); //play the content
playing=true;//update the flag
}
});
mp.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
#Override
public void onCompletion(MediaPlayer mp) {
mp.release(); //playback finished
playing=false; //update the flag
}
});
}
more info on MediaPlayer here
Onclick pause:
public void pause(View view) {
Toast.makeText(getApplicationContext(),
"Pausando..", Toast.LENGTH_SHORT)
.show();
if (mp != null && mp.isPlaying()) {
mp.pause();
}
}//pause
Onclik stop-
public void stop(View view) {
Toast.makeText(getApplicationContext(),
"Stop", Toast.LENGTH_SHORT)
.show();
if (mp != null) {
mp.stop();
}//if
}//stop
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.
I've tried to make a class that extends Mediaplayer and when I've played a sound I want to trigger an event handler in the activity calling the sound.
I Activity:
SoundPlayer soundPlayer = new SoundPlayer(BookInterface.this);
soundPlayer.playSound(this, R.raw.vroom);
And this is the SoundPlayer class:
public class SoundPlayer extends MediaPlayer{
BookInterface ownerActivity;
public SoundPlayer(BookInterface act){
ownerActivity = act;
}
public void playSound(Context context, int resId){
Log.d("Debug", "playSound is called");
MediaPlayer mp = new MediaPlayer();
try {
mp = MediaPlayer.create(context, resId);
mp.start();
} catch (Exception e) {
// TODO Auto-generated catch block
Log.d("Debug","Exception" + e);
e.printStackTrace();
}
mp.setOnCompletionListener(new OnCompletionListener() {
#Override
public void onCompletion(MediaPlayer mp) {
Log.d("Debug", "playSound.onCompletion is called");
mp.stop();
mp.release();
ownerActivity.eventHandler();
}
});
}
}
Why isnt the sound playing?
What am I doing wrong?
BR
Why you declare your MediaPlayer inside playSound method?
I think when playSound method execution is done your MediaPlayer cleared from memory, try to declare MediaPlayer as class variable like this:
public class SoundPlayer extends MediaPlayer{
BookInterface ownerActivity;
private MediaPlayer mp;
public SoundPlayer(BookInterface act){
ownerActivity = act;
}
public void playSound(Context context, int resId){
Log.d("Debug", "playSound is called");
try {
mp = MediaPlayer.create(context, resId);
mp.setOnCompletionListener(new OnCompletionListener() {
#Override
public void onCompletion(MediaPlayer mp) {
Log.d("Debug", "playSound.onCompletion is called");
mp.stop();
mp.release();
ownerActivity.eventHandler();
}
});
mp.start();
} catch (Exception e) {
// TODO Auto-generated catch block
Log.d("Debug","Exception" + e);
e.printStackTrace();
}
}
}
I have an application that show a streaming rtsp. How can I check if it ends?
Buffer empty? Anything else?
Socket is closed
Anything on control channel from media server?
private VideoView vidView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
vidView = (VideoView) findViewById(R.id.myVideo);
uri = Uri.parse(RTSP_URL);
vidView.setMediaController(new MediaController(this));
vidView.setVideoURI(uri);
vidView.requestFocus();
vidView.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
#Override
public void onPrepared(MediaPlayer mp) {
Runnable runnable = new Runnable() {
#Override
public void run() {
vidView.start();
}
};
runOnUiThread(runnable);
}
});
vidView.setOnErrorListener(new MediaPlayer.OnErrorListener() {
#Override
public boolean onError(MediaPlayer mp, int what, int extra) {
Log.d("Error ", what + " " + extra);
return false;
}
});
vidView.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
#Override
public void onCompletion(MediaPlayer mp) {
Log.d(getClass().getName(), " callback COMPLETE HERE ");
}
});
}