Why doesn't the MediaPlayer show the video as soon as it is available. What I mean is on the IPhone when a video is played the video shows up right away. Even when returning from pause. But on the Android the screen stays black for a couple of milliseconds to a second depending on the device used and how many processes are running in the background.
I'm asking this because i want to use one of the beginning frames from my video play as a type of screenshot and currently I'm using a handler to wait 1 second before pausing the video.
Can someone tell me a quick way to make the video show up as soon as it is started or even prepared instead of my workaround?
EDIT:
Here is how I prepare my video player so It should be prepared right.
private void initVideo()
{
Log.i("VideoPlayer", "Initialize Video File" + videoFileName);
AssetFileDescriptor afd;
try {
if(videoFileName != null);
{
afd = getAssets().openFd(videoFileName);
vidplayer = new MediaPlayer();
vidplayer.setDataSource(afd.getFileDescriptor(), afd.getStartOffset(), afd.getDeclaredLength());
vidplayer.setDisplay(holder);
vidplayer.prepare();
vidplayer.setOnCompletionListener(this);
vidplayer.setOnPreparedListener(this);
//Log.i("INITVIDEO", Integer.toString(videoPausedAt));
vidplayer.seekTo(videoPausedAt);
//Log.i("VideoPlayer", "video Prepared");
videoDuration = vidplayer.getDuration()/1000;
isVideoReady = true;
}
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (Exception e)
{
//Log.i("InitPlayer", e.getClass().toString());
e.printStackTrace();
}
}
For the background, you can get a thumbnail of the video:
private Bitmap getThumbnail(String path){
try{
return ThumbnailUtils.createVideoThumbnail(path, MediaStore.Images.Thumbnails.MINI_KIND);
}catch(Exception e){
return null;
}
}
When the video starts, you'll need to set the background back to null or you won't be able to see the video.
As for it not playing right away, it should play as soon as start() is called if you prepared it correctly, but it could be delayed if it has to load data let's say from a stream over the internet.
I have found that it is the phones fault.(mostly) Video's will show up automatically unless phone is bogged down with apps and thus loading of the video takes longer (noticed after having a voip service running).
Related
mpintro= new MediaPlayer();
for(int i=2;i<results.size();i++){
if(results.get(i).equals(emotion[position])){
try {
mpintro.setDataSource(retmus.get(i));
mpintro.start();
} catch (IOException e) {
e.printStackTrace();
}
}
}
I want to play a complete list of songs using mediaplayer class which can be indexed through for loop.It doesnt show any error.but it doesnt play also.
Mostly I want to know if there is a fundamental conflict that I can't share the same resource with the library, if so, I will need to take a different approach.
My goal is to have low quality video with the detector's meta data saved at the same time, so that I can do some post processing and slicing without much of a delay.
Based on the CameraDetectorDemo - camera detector
I have been initializing a MediaRecorder, but it saves a black screen if I start it before the detector, and it crashes on start (with code -19) if I start it after the detector. The detector is attaching the preview, maybe it is to do with that.
I added some buttons to control these functions:
protected void cameraInit() {
String state = Environment.getExternalStorageState();
if (!Environment.MEDIA_MOUNTED.equals(state)) {
Log.d(LOG_TAG, "Drive not mounted - cannot write video");
return;
}
File file = new File(getExternalFilesDir(Environment.DIRECTORY_MOVIES), "demo.gp3");
Log.d(LOG_TAG, String.format("Camera Initializing. Setting output to: %s", file.getAbsolutePath()));
// Set sources
recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
recorder.setVideoSource(MediaRecorder.VideoSource.CAMERA);
// Set profile
recorder.setProfile(CamcorderProfile.get(CamcorderProfile.QUALITY_LOW));
// Set output profile
recorder.setOutputFile(file.getAbsolutePath());
// Set preview output
recorder.setPreviewDisplay(cameraPreview.getHolder().getSurface());
try {
this.recorder.prepare();
} catch (IOException e) {
Log.e(LOG_TAG, "IO exception on camera Initialization");
e.printStackTrace();
} catch (IllegalStateException e) {
// This is thrown if the previous calls are not called with the
// proper order
Log.e(LOG_TAG, "Failed to initialize things properly :( ");
e.printStackTrace();
}
}
protected void cameraStart() {
Log.d(LOG_TAG, "Camera Start");
this.recorder.start();
}
protected void cameraStop() {
Log.d(LOG_TAG, "Camera Stop");
this.recorder.stop();
}
The Affdex SDK's CameraDetector needs access to the camera to get its preview frames and process them, so that's not going to work if the MediaRecorder has control of the camera.
Probably your best bet is to take preview frames from the camera, feed them to an Affdex FrameDetector for processing, and also save them to a video file via a MediaCodec and MediaMuxer, although I haven't tried that.
Outrageous number of similar questions exist here, sadly none did help me.
I am trying to play 3 Audio files simultaneously, one is .wav , the other is .3gp and the other is .mp3 . Since the size exceeds more than 1MB , I cannot use Android SoundPool here. So far, everything works well without any error. Here is my code :
MediaPlayer mp = new MediaPlayer();
MediaPlayer songPlayer = new MediaPlayer();
MediaPlayer voicePlayer = new MediaPlayer();
private String song,voice,text;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_time__date);
String temp;
SharedPreferences preferences1 = getSharedPreferences("musicList", MODE_PRIVATE);
song = preferences1.getString("MUSICONE", "");
SharedPreferences preferences2 = getSharedPreferences("recordList",MODE_PRIVATE);
temp = preferences2.getString("VOICEONE","");
voice = Environment.getExternalStorageDirectory()+"/myAppCache2/"+temp;
SharedPreferences preferences3 = getSharedPreferences("TextList",MODE_PRIVATE);
temp=preferences3.getString("ALARMONE","");
text=Environment.getExternalStorageDirectory()+"/myAppCache/"+temp;
try {
mp.setDataSource(text);
mp.prepare();
mp.setLooping(true);
songPlayer.setDataSource(song);
songPlayer.prepare();
songPlayer.setLooping(true);
voicePlayer.setDataSource(voice);
voicePlayer.prepare();
voicePlayer.setLooping(true);
}
catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (SecurityException e) {
e.printStackTrace();
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
mp.start();
songPlayer.start();
voicePlayer.start();
}
Now, my query is how do I add a delay to one ore more Audio files, Say If I want to add a delay of 5000 for voicePlayer before it loops again?
Now, my query is how do I add a delay to one ore more Audio files, Say If I want to add a delay of 5000 for voicePlayer before it loops again?
You could remove setLooping and use an OnCompletionListener instead. When you get the onCompletion callback, use postDelayed to post a Runnable that starts the player again.
I'm trying to play an rtsp stream using MediaPlayer in android and the application seems to always become stuck on MediaPlayer.prepare();
The url is valid as I tested it using VLC on my desktop.
Any ideas why the application is not preparing the stream.
class InitializeService extends Thread {
#Override
public void run() {
try {
player.prepare();
Log.d("Play", "Player prepared");
} catch (IOException e) {
e.printStackTrace();
fallback();
} catch (IllegalStateException e) {
e.printStackTrace();
fallback();
}
}
}
The log statement is never reached.
Update 1:
Sorry I forgot to mention that the stream will always be in 3gp format. Here is a url rtsp://r2---sn-p5qlsu76.c.youtube.com/CiILENy73wIaGQnTXOVs7Kwo8xMYESARFEgGUgZ2aWRlb3MM/0/0/0/video.3gp
Your stream might not be of a format supported by Android.
Check http://developer.android.com/guide/appendix/media-formats.html to see if Android supports it.
Turns out it was android l that wasn't able to play the streams.
I have an app that will play a tone, it did work perfectly until I installed it on Gingerbread 2.3.4 based android phone. The problem is I don't see any exception thrown, and no sound is generated, exactly same code does play sound in pre gingerbread phone. Here is the code that plays the sound.
MediaPlayer mp = new MediaPlayer();
//mp.release();
try {
String audioFilePath = "content://media/internal/audio/media/20";
mp.setDataSource(audioFilePath);
mp.prepare();
mp.start();
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
I have comments placed before and after the code to play sound, all comments are printed as if sound is played, but there is no sound.
Thanks for any help.
never mind, seems like somehow the volume on alerts was set to 0, I actually went to actual folder, selected a file, played it manually and then turned the volume up while being played and all was well afterwards.