I have 10 buttons for playing 10 songs. i want to achieve when i press the same button while playing, it should be stop . when i press the another button it should be stop the previous song and play the new song ..
but i couldn't achieve both things using same code
i have tried it
when i use this code ,the stop won't work for same button, but it worked for another playing new song .
private void playSample(int[] res, int position)
{
AssetFileDescriptor afd = getApplicationContext().getResources().openRawResourceFd(res[position]);
try
{ if(mp.isPlaying())
{
mp.stop();
}
mp.reset();
mp.setDataSource(afd.getFileDescriptor(), afd.getStartOffset(), afd.getDeclaredLength());
mp.prepare();
mp.start();
afd.close();
}
catch (IllegalArgumentException e)
{
Log.e(TAG, "Unable to play audio queue do to exception: " + e.getMessage(), e);
}
catch (IllegalStateException e)
{
Log.e(TAG, "Unable to play audio queue do to exception: " + e.getMessage(), e);
}
catch (IOException e)
{
Log.e(TAG, "Unable to play audio queue do to exception: " + e.getMessage(), e);
}
}
so i have tried like this
private void playSample(int[] res, int position)
{
AssetFileDescriptor afd = getApplicationContext().getResources().openRawResourceFd(res[position]);
try
{ if(mp.isPlaying())
{
mp.stop();
}else
mp.reset();
mp.setDataSource(afd.getFileDescriptor(), afd.getStartOffset(), afd.getDeclaredLength());
mp.prepare();
mp.start();
afd.close();
}
catch (IllegalArgumentException e)
{
Log.e(TAG, "Unable to play audio queue do to exception: " + e.getMessage(), e);
}
catch (IllegalStateException e)
{
Log.e(TAG, "Unable to play audio queue do to exception: " + e.getMessage(), e);
}
catch (IOException e)
{
Log.e(TAG, "Unable to play audio queue do to exception: " + e.getMessage(), e);
}
}
now the stop will work for same button .but when i press another button while playing .it wont start the new song
check if media playing just stop it and play requested media.no need to else.
if(mp.isPlaying())
mp.stop();
mp.reset();
mp.setDataSource(afd.getFileDescriptor(), afd.getStartOffset(), afd.getDeclaredLength());
mp.prepare();
mp.start();
afd.close();
I guess the problem is calling start without waiting for onPrepared listener. Error (-38,0) is invalid operation, so which mostly probably be calling start when it was not even finished preparing.
Related
I am trying to implement a simple media player in android that contains only two buttons one for play another for pause
Play
String url = " http://host/audio/01_-_Pat.mp3";
public void onClick(View v) {
try{
mPlayer.setDataSource(url);
mPlayer.setOnPreparedListener(MainActivity.this);
mPlayer.prepareAsync();
}catch (Exception e)
{
Log.v("Error", String.valueOf(e));
Toast.makeText(getApplicationContext(), "Exception", Toast.LENGTH_LONG).show();
}
mPlayer.start();
}
I didn't get any exception on this code but i will get this error
*
Failed to open file ' http://host.com/audio/01_-_Pat.mp3'. (No such
file or directory)
*
Actually the url is valid and it contains the audio
Add this line to your code:
mPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
String url = " http://host/audio/01_-_Pat.mp3";
public void onClick(View v) {
try{
mPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
mPlayer.setDataSource(url);
mPlayer.setOnPreparedListener(MainActivity.this);
mPlayer.prepareAsync();
}catch (Exception e)
{
Log.v("Error", String.valueOf(e));
Toast.makeText(getApplicationContext(), "Exception", Toast.LENGTH_LONG).show();
}
mPlayer.start();
}
I am using MediaPlayer to playback audio file of .WAV format. When I debug the app I'm able to play file but when I run the app file is not playing.
There is a similar issue regarding this but no solution is provided.
Below is the code which i am using
MediaPlayer mMediaPlayer = new MediaPlayer();
mMediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
mMediaPlayer.setDataSource(mContext, fileUri);// uri of the audio file
mMediaPlayer.prepare();
mMediaPlayer.setOnCompletionListener(this);
mMediaPlayer.start();// to start the playback of the audio file
This code is working only in debug mode but not in normal mode.
Thanks
This is what I am saying:
btn_next.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
mp.setAudioStreamType(AudioManager.STREAM_MUSIC);
try {
mp.reset();
mp.setDataSource(url.toString());
mp.setOnErrorListener(this);
mp.setOnPreparedListener(this);
mp.setOnCompletionListener(this);
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (SecurityException e) {
Log.e(TAG, "SecurityException");
} catch (IllegalStateException e) {
Log.e(TAG, "IllegalStateException");
} catch (IOException e) {
Log.e(TAG, "IOException");
}
mp.prepareAsync();
}
});
Now, override method onPrepared().
#Override
public void onPrepared(MediaPlayer mp) {
// TODO Auto-generated method stub
mp.start();
}
Comment below if you face any issue in this.
Don't get the wrong sound when playing, when I enter text in English then I'll get everything and the result gives sootvetstvuyushaya word,
but when I change the url to: http://translate.google.com/translate_tts?tl=ru&q=привет and open computer browser then I'm fine but when I enter the url in the source code that gives me no intelligible speech, give that to me to do
This is my code:
public void onClick(View v) {
MediaPlayer player = new MediaPlayer();
try {
player.setAudioStreamType(AudioManager.STREAM_MUSIC);
player.setDataSource("http://translate.google.com/translate_tts?tl=ru&q=привет");
player.prepare();
player.start();
} catch (Exception e)
{
Toast.makeText(this, "speaking error!!", Toast.LENGTH_LONG).show();
}
}
Thread x;
MediaPlayer mediaPlayer;
x=new Thread(){
public void run(){
try{
url1="http://www.translate.google.com/translate_tts?ie=UTF-8&q="this is word which is speech"%0A&tl="this is language"&prev=input";
mediaPlayer=new MediaPlayer();
mediaPlayer.reset();
mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
mediaPlayer.setDataSource(url1);
mediaPlayer.prepare(); // might take long! (for buffering, etc)
mediaPlayer.start();
} catch (IllegalArgumentException e) {
mediaPlayer.reset();
} catch (IllegalStateException e) {
mediaPlayer.reset();
} catch (IOException e) {
mediaPlayer.reset();
}
finally{
// x.suspend();
}
}
};
then you can call like this :
x.start();
I used:
player = new MediaPlayer();
try {
player.setDataSource(str_stream_url);
// player.setDataSource("http://cdn.concreteloop.com/wp-content/uploads/2011/01/Jennifer-Lopez-feat.-Pitbull-On-the-Floor.mp3");
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
player.setOnBufferingUpdateListener(new OnBufferingUpdateListener() {
public void onBufferingUpdate(MediaPlayer mp, int percent) {
playSeekBar.setSecondaryProgress(percent);
Log.i("Buffering", "" + percent);
}
});
It's working in Android v4.0 but it is NOT working in Android 2.3.3..
What can I do??
Thx.
Also I tried this code in Android 2.3.3 but It didn't work.. At the end you can see the errors.
Uri myUri = Uri
.parse("http://46.137.18.79:8000/;stream.nsv&type=mp3&autostart=true");
player = new MediaPlayer();
try {
if (player == null) {
this.player = new MediaPlayer();
} else {
player.stop();
player.reset();
}
player.setDataSource(this, myUri); // Go to Initialized state
player.setAudioStreamType(AudioManager.STREAM_MUSIC);
player.setOnPreparedListener(this);
player.setOnBufferingUpdateListener(this);
player.setOnErrorListener(this);
player.prepareAsync();
Log.d(TAG, "LoadClip Done");
} catch (Throwable t) {
Log.d(TAG, t.toString());
}
02-06 13:49:03.730: E/MediaPlayer(2718): stop called in state 1
02-06 13:49:03.730: E/MediaPlayer(2718): error (-38, 0)
02-06 13:49:03.730: D/MediaPlayer(2718): Couldn't open file on client side, trying server side
02-06 13:49:03.870: D/MyMain(2718): LoadClip Done
Thx.
I have a MediaPlayer that plays with the following code:
private void playVideo() {
if (videoURI == null) {
showToast("Please, set the video URI in HelloAndroidActivity.java in onClick(View v) method");
} else {
new Thread(new Runnable() {
public void run() {
try {
player.setOnErrorListener(new OnErrorListener()
{
#Override
public boolean onError(MediaPlayer mp,
int what, int extra) {
Log.e("MEDIAPLAYER ERRORS",
"what: " + what + " extra: " + extra);
return false;
}
});
Log.d("video","---+ in main run");
player.setDataSource(videoURI);
player.prepare();
player.reset();
} catch (IllegalArgumentException e) {
Log.d("video","---+ illegal arg");
showToast("Error while playing video");
Log.i(TAG, "========== IllegalArgumentException ===========");
e.printStackTrace();
} catch (IllegalStateException e) {
Log.d("video","---+ illegal state");
showToast("Error while playing video");
Log.i(TAG, "========== IllegalStateException ===========");
e.printStackTrace();
} catch (IOException e) {
Log.d("video","---+ general error");
showToast("Error while playing video. Please, check your network connection.");
Log.i(TAG, "========== IOException ===========");
e.printStackTrace();
}
}
}).start();
}
}
On error, my app goes into an infinite error loop with the following messages:
03-05 13:27:23.035: E/MediaPlayer(6289): stop called in state 0
03-05 13:27:23.035: E/MediaPlayer(6289): error (-38, 0)
03-05 13:27:23.035: E/MediaPlayer(6289): Error (-38,0)
03-05 13:27:23.043: E/MEDIAPLAYER ERRORS(6289): what: -38 extra: 0
What is the best way to recover from the error that is caught? Should I abort the mediaplayer and if so how do I accomplish this? Also how can I stop from going into this error loop in the first place?
Thanks!
Is there something missing from your code? It doesn't seem to me like it would ever actually start the MediaPlayer anywhere in what you've posted.
It also seems odd to me that you call
player.reset();
after the others. I would think this would undo the setDataSource(), and prepare() that you just got finished with. Try moving reset so that it is the first one called.
and try putting this in your onErrorListener callback:
player.reset();
player.setDataSource(videoURI);
player.prepare();
That way it will just try to remake the whole thing when it gets this error.