This is my method in service for playing files, on AVD everything works fine, no errors, when I click the control on PDA I get these LogCat events: http://pastebay.com/125483. I have no clue what is happening. Please advise.
public void playAudio (int playAudio) {
player = MediaPlayer.create(this, playAudio);
try {
if (player != null) {
player.stop();
}
player.prepare();
player.start();
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
}
}
I solved it, the problem was I called prepare(); after MediaPlayer.create since it prepares the MediaPlayer already.
Related
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.
I have an application that uses the Orientation Sensor, and I want to play the default notification sound when the 'y' value is upper than 170 degrees. The thing is that the first time it happens it makes the sound correctly but twice (I suppose this is because of the time it takes to stablish canSound to false) and also after the first time it happens, if 'y' value is again upper than 170,it doesn't sound as I expected to happen.So, the question is, what am I doing wrong with mediaPlayer?
Thanks in advance.
P.D.:This is the part of code I have inside public void onSensorChanged(SensorEvent event) related with my problem.
if(y>170&&canSound)
{
canSound=false;
Uri defaultRingtoneUri=RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
mediaPlayer = new MediaPlayer();
try
{
mediaPlayer.setDataSource(context, defaultRingtoneUri);
mediaPlayer.setAudioStreamType(AudioManager.STREAM_NOTIFICATION);
mediaPlayer.prepare();
mediaPlayer.setOnCompletionListener(new OnCompletionListener()
{
#Override
public void onCompletion(MediaPlayer mp)
{
mp.release();
}
});
mediaPlayer.start();
}
catch (IllegalArgumentException e)
{
e.printStackTrace();
}
catch (SecurityException e)
{
e.printStackTrace();
}
catch (IllegalStateException e)
{
e.printStackTrace();
}
catch (IOException e)
{
e.printStackTrace();
}
}
}
else
{
if(y<170&&!canSound)
{
mediaPlayer.stop();
mediaPlayer.reset();
canSound=true;
}
My media player for some reason completes while it is paused. Now i have seen another post that said about it being async and 300ms difference and that I understand. But my main problem is that i have 20 seconds left on the video and while paused it finishes after the 20 seconds run out.
here is the relevant code that I might have messed up on:
private void initPlayer()
{
try {
if(videoFile != null)
{
afd = getAssets().openFd(videoFile);
instructionVideoPlayer = new MediaPlayer();
instructionVideoPlayer.setDataSource(afd.getFileDescriptor(), afd.getStartOffset(), afd.getDeclaredLength());
instructionVideoPlayer.setDisplay(holder);
instructionVideoPlayer.prepare();
Log.i("Instruction Video", "videoPrepared");
instructionVideoPlayer.setOnCompletionListener(instructionVideoComplete);
instructionVideoPlayer.setOnPreparedListener(this);
}
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
Log.i(this.toString(), "IOEXception");
e.printStackTrace();
//initPlayer();
} catch (Exception e)
{
Log.i("InitPlayer", e.getClass().toString());
e.printStackTrace();
}
restoreOverlayState();
}
The listeners:
MediaPlayer.OnCompletionListener instructionVideoComplete = new MediaPlayer.OnCompletionListener() {
public void onCompletion(MediaPlayer arg0) {
Log.i("onCompletion", "called");
if(!videoFile.contentEquals(LangSelect.INSTRUCTION_05))
{
Intent returnIntent = new Intent();
returnIntent.putExtra(LangSelect.ACTION, LangSelect.GO_VIDEO_NEXT);
setResult(RESULT_OK, returnIntent);
continuePlaying = true;
toFinish();
}
else
toFinish();
}
};
public void onPrepared(MediaPlayer mediaPlayer)
{
playVideo();
}
the play and pause functions
private void playVideo()
{
instructionVideoPlayer.seekTo(pausedAt);
instructionVideoPlayer.start();
restoreOverlayState();
}
private void pauseVideo()
{
pausedAt = instructionVideoPlayer.getCurrentPosition();
instructionVideoPlayer.pause();
}
private void restoreOverlayState()
{
prefs = getSharedPreferences(SAVE_PREFS, Context.MODE_PRIVATE);
if(prefs.getBoolean(IS_PAUSED, false))
{
pauseOverlay.setVisibility(View.VISIBLE);
pauseVideo();
}
else
{
pauseOverlay.setVisibility(View.GONE);
}
}
Edit: this only happens on the Nexus S (2.3.3) one of the phones I am testing on. The other Galaxy S(Froyo) does not have this error.
I figured out what I was doing Wrong. the OnCompletionListener was being called. The problem was I was running 2 media players and setting the same onCompletionlistener to them accidentally as I was calling initPlayer twice. Once in surfaceCreated and again at the end of onResume(). Either the Galaxy S is messed up or it has weird error checking becasue this problem didnt show up there.
While I use almost the same code to play two videos, the first works perfectly, but the second does not. When I press the next video button, the program crashed with:
WARN/System.err(15726): java.lang.IllegalStateException
WARN/System.err(15726): at android.media.MediaPlayer.setDataSource(Native Method)
Source code:
the first play code:
mediaPlayer = new MediaPlayer();
playURI = receiveIntent.getStringExtra("playURI");
showDebugInfo("play uri "+playURI);
Log.e("Gplayer on create", "play uri "+playURI);
try {
mediaPlayer.setDataSource(playURI);
} catch (IllegalArgumentException e) {
Log.v(LOGTAG, e.getMessage());
finish();
} catch (IllegalStateException e) {
Log.v(LOGTAG, e.getMessage());
finish();
} catch (IOException e) {
Log.v(LOGTAG, e.getMessage());
finish();
}
when press next button it runs the following code:
String uri = listAdapter.getItem(position).getItem().getFirstResource().getValue();
showDebugInfo(" Uri "+uri);
if(mediaPlayer != null){
mediaPlayer.stop();
}
try {
mediaPlayer.reset();
mediaPlayer.setDataSource(uri);
mediaPlayer.prepareAsync();
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
finish();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
finish();
}
Why does this happen? and I check the two parts only the different that, the first one is new a mediaplayer and the second is let the mediaplayer stop and reset then the same.
I had the same problem.
You need to call mediaPlayer.reset(); before you are calling in the second video mediaPlayer.setDataSource(uri); the reason is, because the function mediaPlayer.setDataSource(uri); can be called only on idle state, and mediaPlayer.reset(); is the function that take you to idle state.
I understand it from this answer, even though the question is a bit different.
I'm a noob trying to work something out and learn from it.
I have two imagebuttons and when i click them I get a kind of "schick" sound rather than the sound files that i have in the /res/raw/ directory.
This is my code:
public void button_clicked1(View v)
{
text1.setText("1"+width);
mp = MediaPlayer.create(GameScreen.this, R.raw.a);
mp.start();
}
public void button_clicked2(View v)
{
text1.setText("2"+height);
mp = MediaPlayer.create(GameScreen.this, R.raw.b);
mp.start();
}
What am I doing wrong?
Thanks!
Ok, changed the above code to this:
public void button_clicked1(View v)
{
text1.setText("1"+width);
mp = MediaPlayer.create(GameScreen.this, R.raw.piano_a);
try {
mp .prepare();
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
mp.start();
}
public void button_clicked2(View v)
{
text1.setText("2"+height);
mp = MediaPlayer.create(GameScreen.this, R.raw.piano_b);
try {
mp .prepare();
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
mp.start();
}
And it still does not work
EDIT: Try this:
setVolumeControlStream(AudioManager.STREAM_MUSIC);
in your main application code. This will tell the AudioManager that when your application has focus, the volume keys should adjust music volume (found that here).
After that, make sure that your volume is up - it may just be playing the sounds with no volume.