Media Player working only in debug mode - android

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.

Related

android Media player not reading some mp3 url

i've created a playlist based on Media Player class. The thing is that it succeed in reading some mp3 url and it doesn't for another ones.
For example here is a directory of mp3 i've uploaded but no one is working:
http://rajaapp12.0fees.us/gbv3/
in the other hands an mp3 which work:
http://mp3.mp3zik.com/music/Music-Rap/Casa%20Verde%202009/track%20(14).mp3
I don't think there is a problem with the code since it works already witch some url, but here is the most iportant part of it :
public void beginLrcPlay(){
killMediaPlayer();
mPlayer = new MediaPlayer();
try {
mPlayer.setDataSource(songEnCours.getSource_128());
mPlayer.setLooping(true);
mPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
public void onPrepared(MediaPlayer mp) {
mp.start();
if (mTimer == null) {
mTimer = new Timer();
mTask = new LrcTask();
mTimer.scheduleAtFixedRate(mTask, 0, mPalyTimerDuration);
}
}
});
mPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
public void onCompletion(MediaPlayer mp) {
//fin musique ajouter action !
stopLrcPlay();
chantSuivant();
btnPlay.setImageResource(R.drawable.bg_selector_btn_play);
}
});
mPlayer.setOnBufferingUpdateListener(onBufferingUpdateListener);
mPlayer.prepare();
mPlayer.start();
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}

MediaPlayer and error when playing music from online

mediaPlayer=new MediaPlayer();
mediaPlayer.reset();
mediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
#Override
public void onPrepared(MediaPlayer mp) {
mediaPlayer.start();
}
});
try {
mediaPlayer.setDataSource("192.168.191.1/test.mp3");
} catch (IOException e) {
e.printStackTrace();
}
mediaPlayer.prepareAsync();
above is my code. It works well from such url: http://programmerguru.com/android-tutorial/wp-content/uploads/2013/04/hosannatelugu.mp3
But it don't work when I try to access mp3 file in my server, 192.168.191.1/test.mp3, and I use wamp for my server.
I had the same requirement and it is working on my end :
MediaPlayer player = new MediaPlayer();
player.setAudioStreamType(AudioManager.STREAM_MUSIC);
player.setDataSource("mp3 link"
);
player.prepare();
player.start();
} catch (Exception e) {
// TODO: handle exception
}

MediaPlayer Android programming google text to speech.

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();

MediaPlayer error android

I have an activity which has a series of buttons which when pressed should play an audio file. I have been trying to implement this using MediaPlayer however I cant get it to work.
Here is the code I have been trying:
final MediaPlayer mp = new MediaPlayer();
Button ger1play = (Button) findViewById(R.id.ger1play);ger1play.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
mp.setDataSource(this, R.raw.greet_1);
mp.prepare();
mp.start();
}
});
The setDateSource method doesnt seem to work, can anyone tell me where I am going wrong?
I would like to then set the mediaPlayer to the relevant audio file based on which button is pressed, is this possible?
Updated
final MediaPlayer mp = new MediaPlayer();
Button ger1play = (Button) findViewById(R.id.ger1play);ger1play.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
try {
Uri myUri = Uri.parse(R.raw.greet_1);
mp.setDataSource(GreetingsLesson.this, R.raw.greet_1);
mp.prepare();
mp.start();
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
});
try this:
final MediaPlayer mp = new MediaPlayer();
Button ger1play = (Button) findViewById(R.id.ger1play);ger1play.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
try {
mp.setDataSource(CurrentActivity.this, R.raw.greet_1);
mp.prepare();
mp.start();
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
});
Ifyou want to send the media player object with one of the files fromapplication raw resources or from application assets files you can dothat as follows:
try {
AssetFileDescriptor fd = getResources().openRawResouceFd(R.raw.greet_1);
mp.setDataSource(fd.getFileDescriptor(), fd.getStartOffset(), fd.getLength());
mp.start();
fd.close();
} catch (IllegalArgumentException e) {
// handle exception
} catch (IllegalStateException e) {
// handle exception
} catch (IOException e) {
// handle exception
}
Why not just use
mp = MediaPlayer.create(this, R.raw.greet_1);
Then you don't need the prepare or start.
Are you running this in an emulator? If so check your AVD manager has under hardware, the property "Audio playback support | yes" added

MediaPlayer Video Problems

I've just spent about 2 hours trying to get this to work. I've never had much luck with getting videos to play with MediaPlayer. What am I doing wrong here? It plays just audio the first time through, no video. And then on the second time I get IllegalStateException's when I try to re-setDataSource. I've tried not calling stop() and release() in onCompletion I've tried calling reset() before playing. I just get different StateExceptions (0, 64, 128) I've run out of ideas.
private void playVideo() {
mMediaPlaying = true;
sv.setVisibility(View.VISIBLE); //surfaceview
try {
if(mp.isPlaying()) {
mp.stop();
mp.reset();
}
mp.setDisplay(sh); //surfaceholder
mp.setOnCompletionListener(this);
mp.setOnPreparedListener(this);
mp.setDataSource(getBaseContext(), Uri.parse("android.resource://" + getPackageName() + "/" + R.raw.test));
mp.prepare();
//mp.start();
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SecurityException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
#Override
public void onPrepared(MediaPlayer mp) {
Log.d("", "mp prepared");
mp.start();
}
#Override
public void onCompletion(MediaPlayer arg0) {
sv.setVisibility(View.GONE);
mp.stop();
mp.release();
mMediaPlaying = false;
Log.d("", "Done playing media");
}
You can use VideoView if you want to play video.
http://developer.android.com/resources/samples/ApiDemos/src/com/example/android/apis/media/VideoViewDemo.html
And you can use MediaController to control the video such as play,pause,forward and rewind.
Check this link for example.
http://android-coding.blogspot.in/2011/03/using-videoview-to-play-mp4-from-sdcard.html
mp.reset() must be call before setDataSource,
have a look at the life cycle of MediaPlayer
http://developer.android.com/reference/android/media/MediaPlayer.html
Ok, I basically copy/pasted this here and it seems to be working... Except the first time it plays there is still no video... But I got rid of the errors :)
try this:
private void playVideo() {
mMediaPlaying = true;
sv.setVisibility(View.VISIBLE); //surfaceview
try {
if(mp != null) {
mp.stop();
mp.release();
}
mp = new MediaPlayer();
mp.setDisplay(sh); //surfaceholder
mp.setOnCompletionListener(this);
mp.setOnPreparedListener(this);
mp.setDataSource(getBaseContext(), Uri.parse("android.resource://" + getPackageName() + "/" + R.raw.test));
mp.prepare();
mp.start();

Categories

Resources