I have an application with a VideoView that will keep looping the same video over and over until a user does something to the device(touch screen, etc) Currently I am using the OnCompletionListener() to restart the video once it ends. This works properly on every device I have tested, except for the Samsung Replenish.
Here is my code:
mVideoView.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
public void onCompletion(MediaPlayer mp) {
mVideoView.setVideoPath(file.getAbsolutePath());
mVideoView.start();
}
});
What happens on the Replenish is the video plays all the way through once but then is never started again and the screen goes all black (But backlight still turned on). On every other device I've tested on with this exact same code it has worked to repeat the video. Does anyone know of anything specific about the Replenish that could cause this error? I thought maybe delaying the calls to setVideoPath(), and start() by 200-300ms might help it, but that had no affect. I am really at a loss here.
I am seeing these messages in my Log:
ERROR/QCvdec(87): Omx Flush issued when vdec is not initialized yet.
ERROR/QCvdec(87): OMXCORE-SM:Recieved command DISABLE (2)
ERROR/QCvdec(87): Omx Flush issued when vdec is not initialized yet.
ERROR/QCvdec(87): OMXCORE-SM:Recieved command ENABLE (3)
But these logs are happening both when the video starts (the first time it plays) and when it fails to start again. so I am not sure if they are related to my problem
Edit:
I just tried setting mVideoView to null, and then getting a new reference to it with findViewById() right before the setVideoPath(). I know this would complicate the way the OnCompletionListener is set up. But regardless of that it didn't work anyway, still the same dark screen.
Edit 2:
I've started to notice that sometimes the video doesn't even start the first time. I am using these same two lines to start it up the first time:
mVideoView.setVideoPath(file.getAbsolutePath());
mVideoView.start();
It seems to start more consistantly, but not quite 100% when its the first time it is being played.
Edit 3: This is how I have it set up now. I am manually setting the OnPreparedListener to start the video for me. So I added this to my onCreate()
mVideoView.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
#Override
public void onPrepared(MediaPlayer arg0) {
mVideoView.start();
}
});
Then when I am ready to restart the video I just call only the setVideoPath() method, Like this:
mVideoView.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
public void onCompletion(MediaPlayer mp) {
mVideoView.setVideoPath(file.getAbsolutePath());
}
});
This seems to be doing the trick. I am letting it run for a while to find out for sure though.
Edit 4:
#MByD In the code for VideoView setVideoPath() is a wrapper for setVideoUri(). setVideoURI() is setting mStartWhenPrepared = false; The default OnPreparedListener checks this switch to decide whether to start playback or not. That is why it doesn't start with the default listener. I haven't looked into it more than that, but there may be a setter method that lets me change the mStartWhenPrepared value to true, which would cause the video to be started from the default listener.
Have you tryed to prepare() your video before you call .start()?
from the docs: http://developer.android.com/reference/android/media/MediaPlayer.html#prepare%28%29
with a OnPreparedListener you can start your video when its ready.
Related
I'm trying to get a simple sound to play. The sound plays on the emulator but not on the phone. It says
start called in state 0
and
Error (-38,0)
Here's what I've tried so far:
MediaPlayer secondSound;
MediaPlayer minuteSound;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
secondSound = new MediaPlayer();
secondSound = MediaPlayer.create(this, R.raw.everysecondsound);
minuteSound = MediaPlayer.create(this, R.raw.everyminutesound);
secondSound.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
public void onPrepared(MediaPlayer player) {
player.start();
}
});
minuteSound.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
public void onPrepared(MediaPlayer player) {
player.start();
}
});
}
Later in the program, the following methods are called:
minuteSound.start();
secondSound.start();
On the emulator, this works fine even without the .setOnPreparedListener part. The emulator is running API 22, the phone is running API 19. Is there a different way to use the media player in the older APIs?
When you call MediaPlayer.create(), that automatically calls prepare() for you, as seen in the source code. As per the state diagram in the MediaPlayer documentation:
This puts you into the Prepared state. At that point, when you call start(), you move into the Started state. After playback completes, you'll be in the Playback Completed state. If you'd like to play your sound again, you have to call create() again, or call stop() and prepare() again.
You'll find SoundPool more appropriate for short tracks you want to play repeatedly.
First of all, I think you have to declare the minute MediaPayer (as you did with the second), this way: minuteSound = new MediaPlayer();
Try this and return. If it doesn't work, maybe you will have to release the MediaPlayer before calling it again.
The problem was the file format for my sound files. They were originally 96000Hz wave files, I changed them to 44100Hz MP3 files and then everything worked. I removed the .setOnPrepareListener part since it worked without it.
I've been playing some mp4 videos through the VideoView, which apparently uses/is a wrapper for the MediaPlayer.
As usual I see the typical ones in the logcat:
I/MediaPlayer﹕ Info (701,0)
I/MediaPlayer﹕ Info (702,0)
But then I see that one as well:
I/MediaPlayer﹕ Info (950,0)
As stated in this answer and others questions, most 9XX MediaPlayer Info/Warning/Error codes aren't officially documented in the SDK docs, but probably is related with "timed text tracks" (subtitles), since the only references to 9XX are MEDIA_INFO_UNSUPPORTED_SUBTITLE (901) and MEDIA_INFO_SUBTITLE_TIMED_OUT (902).
The thing is, I don't use any subtitles or external/extra resources while playing the video, so that would be strange.
Does anyone know any additional information about the 950 or the 9XX codes?
(I'm trying to track a bug that could be related to that since it's the last info I have going in the logcat - just exploring all the possibilities.)
Am facing the same warning but with a different scenario. In the following code, resetting the mediaplayer after onCompletion generates this warning. After it I have problems with the track and trying to restart it.
soundMediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
#Override
public void onCompletion(MediaPlayer mediaPlayer) {
mediaPlayer.reset();
mediaPlayer = MediaPlayer.create(MainActivity.this, soundUri);
mediaPlayer.start();
}
});
Hope it gives you a clue.
I work with a MediaPlayer and set the state of the player often programmatically like for example:
if(mp.isPlaying()) {
mp.pause();
animationPausedMusic();
}
private void animationPausedMusic() {
// Changing button image to play button
btn_play.setBackgroundResource(R.drawable.play);
... // more code
}
But sometimes the logcat gives me the message:
"internal/external state mismatch corrected"
And then the play and pause function is not working anymore.
What does this message mean? And how can I solve it?
After going through the android's native framework for media player I found that in source file mediaplayer.cpp inside function bool MediaPlayer::isPlaying() The developer is checking if the currentState of media player is in STARTED state and yet the media player is not playing any media, so it tries to change the state to PAUSED state so that the state consistency should be maintained for API users.(and here is where he is printing the message "ALOGE("internal/external state mismatch corrected");")
Now If you go through the media player state diagram below:
You would notice that this may happen when the MediaPlayer moved to 'STARTED' state after a call to start() and at this time for some obscure reason has not yet started the playback and you fire a MediaPlayer.isPlaying() method call , The Framework treat this as state inconsistency and moves to 'PAUSED' state and that's why you cannot see anything playing further.
However, if someone has some better understanding please share your thoughts!
I ran into this recently, and like some other questions say, it's this bug (marked obsolete alas)
https://code.google.com/p/android/issues/detail?id=9732
I found this error occurs when playing a MIDI file, but only sometimes. It happens when mp.isPlaying() is called quickly after mp.start()
If you can manage to not call mp.isPlaying() for a little bit, the error doesn't occur. In my case, a 10th of a second or so made the difference between getting the error or not. It's awkward, but it works.
e.g.
//setting a new track
mp.setDataSource(path);
mp.prepare();
mp.start();
//calling mp.isPlaying() here or shortly after starts the problem
//since we know it's playing, we can store that state, or call
updateUiPlaying(); //eg instead of updateUi();
//or just call some code here that takes more time first
updateScaledImages(); //something that might take time
Log.v(TAG, "mp.isPlaying = " + mp.isPlaying()); //now isPlaying() shouldn't cause that error
Also, I put a check in when I pause later.
mp.pause()
if(mp.isPlaying()){
//shouldn't be playing, must be in error
mp.stop();
mp.release();
mp = new MediaPlayer();
//any other initialization here
}
Though the problem doesn't occur if there is a wait before calling isPlaying()
Apparently there is more than one cause of this message. The following solution worked for me. It may or may not work for you. I called the method MediaPlayer.reset() immediately after instantiating the MediaPlayer object:
MediaPlayer mp = new MediaPlayer();
mp.reset();
Im hoping you can provide some guidance. I created a 'final' instance of a mediaplayer (mp1) and on a button click it plays a mp3 file. I then need to stop the file when I click a second button. This works fine until I try to play the file again - nothing happens. I think that because the mp1 instance is 'final', when I stop it, it stops for good until I relaunch the app. I dont want to pause the file, I want to stop it and then restart it afresh. Any ideas welcome. I tried putting the mp1 creation within the button. This worked until the app crashed - probably because multiple mediaplayer creations used all the device memory?
Thanks!!!
// const mediaplayer
mp1 = MediaPlayer.create(getApplicationContext(), R.raw.mysound);
...
// in button 1
if (radSound1.isChecked()) {
radSound2.setChecked(false); // ...set radiobutton 2 to false
mp1.start(); // ...play the mp3 file
}
...
// in button 2
if (mp1 != null){
mp1.reset();
//mp1.setDataSource();
// mp1.prepare();
}
Follow the state diagram here. stop() stops playing and puts you in stopped state. Before starting to play for the next time, you should call prepare() and then start() again.
So in button 1, you should call start() and in button 2, you should call stop() and prepare(). The initialization is fine, do it once and keep it outside the buttons.
Also accept answers to your questions including this so that people like me will be more motivated to reply to them in the future.
The Google Voice Search comes with a significant delay from the moment you call it via startActivityForResult() until its dialog box is displayed, ready to take your speech.
This requires the user to always look at the screen, waiting for the dialog box to be displayed, before speaking.
So I was thinking of generating an audible signal instead of the dialog box by implementing RecognitionListener and sounding a DTMF tone in onReadyForSpeech() as in the following snippet:
#Override
public void onReadyForSpeech(Bundle params) {
Log.d(LCTAG, "Called when the endpointer is ready for the user to start speaking.");
mToneGenerator.startTone(ToneGenerator.TONE_DTMF_1);
try {
Thread.sleep(50);
} catch (InterruptedException e) {
Log.e(LCTAG, "InterruptedException while in Thread.sleep(50).");
e.printStackTrace();
} // SystemClock.sleep(50);
mToneGenerator.stopTone();
}
The tone sounds beautifully but... it is also "heard" by the microphone, arriving to the voice recognition service and always generating a recognition error ERROR_NO_MATCH.
Is there a way to work around this?
Here is a random idea, and it may very well not work.
Can you try disabling the microphone (maybe via AudioManager.setMicrophoneMute) while the tone is played?
Here's my code that's working for me, put into the onReadyForSpeech() callback of the RecognitionListener.
private void playSpeechReadyTone(){
audioManager.setMicrophoneMute(true);
MediaPlayer mediaPlayer = MediaPlayer.create(JarvisService.this, R.raw.doublebeep);
mediaPlayer.setOnCompletionListener(new OnCompletionListener() {
#Override
public void onCompletion(MediaPlayer arg0) {
audioManager.setMicrophoneMute(false);
}
});
mediaPlayer.start();
}
I'm afraid that there isn't an easy&clean way to do that. As srf appointed, you shouldn't rely on AudioManager.setMicrophoneMute(boolean), so, AFAIK, the possibles are:
Play an audio file before call SpeechRecognizer.startListening(intent):
final MediaPlayer mediaPlayer = MediaPlayer.create(JarvisService.this, R.raw.doublebeep);
mediaPlayer.setOnCompletionListener(new OnCompletionListener() {
#Override
public void onCompletion(MediaPlayer player) {
player.release();
// Safety start Speech Recognizer
mSpeechRecognizer.startListening(getSpeechRecognizerIntent());
}
});
mediaPlayer.start();
However, this solution has a problem... If may receive an RecognitionListener.onError(int error) before RecognitionListener.onReadyForSpeech being called and, in that case, you are still playing a beep sound every time (this will happen, for example, if you are not connected to the Internet and Speech Recognition is configured to work online)!
Besides, you should manage the case of cancelling the speech recognition process during audio (doublebeep) play.
Play the audio in the onReadyForSpeech callback (read original question) but use RecognizerIntent.EXTRA_SPEECH_INPUT_MINIMUM_LENGTH_MILLIS with an appropriate value. In my case, my beep sound is really short (1 second max) and I set RecognizerIntent.EXTRA_SPEECH_INPUT_MINIMUM_LENGTH_MILLIS to 4/5 seconds.
Also note that, as Google doc says:
Note also that certain values may cause undesired or unexpected results - use judiciously! Additionally, depending on the recognizer implementation, these values may have no effect.