VideoVIew issue on android - android

I am working on video player in android.when i created surface holder in on create method ,it did not create.but when i created the surface holder in button onclick method,its created
My coding is,
preview=(VideoView)findViewById(R.id.surface);
preview.setEnabled(true);
preview.bringToFront();
holder=preview.getHolder();
holder.setFixedSize(400, 400);
mp=new MediaPlayer();
mp.setDataSource("path");
mp.setDisplay(holder);
mp.setAudioStreamType(AudioManager.STREAM_MUSIC);
mp.setOnBufferingUpdateListener(playerActivity.this);
mp.setOnPreparedListener(playerActivity.this);
mp.prepare();
mp.start();

mp.prepare();
mp.start();
prepare() is asynchronous, which means, it might not be finished when you already call the mp.start. What do you mean by 'surface not created'? Do you just mean the video doesn't play?
Anyway, you should use an MediaPlayer.OnPreparedListener and start the media in onPrepared().

Related

MediaPlayer main thread android

I was using SoundPoolto play sound effects but now I need to switch to MediaPlayer as I need to listen to the onCompletion event to trigger a GUI change.
My questions are:
The sound files are very small, less than 30kB, Can I use
MediaPlayer in the main thread?
Can I just call mediaPlayer.stop() after the play is done like so,
mediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
public void onCompletion(MediaPlayer mediaPlayer) {
//Do some GUI changes
mediaPlayer.stop();
mediaPlayer.release();
}
});
Then to play another effect will do this:
mediaPlayer = MediaPlayer.create(context, R.raw.sound_file_1);
mediaPlayer.start();
So, is it ok to create and release every time I play an effect?
Initialize your MediaPlayer
MediaPlayer mediaPlayer = new MediaPlayer();
AssetFileDescriptor afd = context.getResources().openRawResourceFd(resid);
mediaPlayer.setDataSource(afd.getFileDescriptor(), afd.getStartOffset(), afd.getLength());
afd.close();
Look at the MediaPlayer lifecycle:
When the track is complete the MediaPlayer is in Stopped state and you wan't to change the track, so:
call reset()' to get toIdlethensetDataSourcethenprepareand finallystart()`.
The sound files are very small, less than 30kB, Can I use MediaPlayer in the main thread?
Irrespective of the size of your audio file, you should run MediaPlayer on its own thread, especially if you are playing a file from network. Not doing so may cause ANR (In your case I don't think much to worry about)
Can I just call mediaPlayer.stop() after the play is done
There is no need to release the MediaPlayer after every audio file. Release it once you are no longer going to use it.
how can I load a new sound
AssetFileDescriptor audio = context.getResources().openRawResourceFd(R.raw.next_audio);
mediaPlayer.setDataSource(audio);

MediaPlayer setLooping(true) freezing my phone

I want to loop a music in my app so I use the following code:
mediaPlayer = MediaPlayer.create(this, R.raw.music);
mediaPlayer.setVolume(8f, 8f);
mediaPlayer.start();
mediaPlayer.setLooping(true);
I've try to start and then setLooping, the problem still there.
mediaPlayer = MediaPlayer.create(this, R.raw.music);
mediaPlayer.setVolume(8f, 8f);
mediaPlayer.setLooping(true);
mediaPlayer.start();
But when the music ends it don't restart and my app slows rapidly until total freeze, but there is no crash.
If I look at the log there is a huge succession of :
MediaPlayer_Java: MEDIA_PAUSED
MediaPlayer_Java: MEDIA_STARTED
My phone is a Xperia M4 Aqua.
Thank you for your help !
mediaPlayer.setLooping(true);
is before start media player

Media Player replay button

I am making an app that involves sound. There is a replay button. All I want is, whenever that is hit, my song whenever finished or in play, to be played from the beginning. I have tried many thing like
mp.stop();
mp.start();
or
mp.stop();
mp.prepare();
mp.start();
but none of them worked. Can you help me?
try to seek player to 0 before you stop it:
mp.seekTo(0);
mp.stop();
mp.start();

Attach second instance of MediaPlayer to SurfaceView (Error on Android Gingerbread)

I have got an error concerning MediaPlayer in combination with a surfaceview:
I am playing a video in a surfaceview by Streaming it from the Internet as follows:
mPreview = (SurfaceView) findViewById(R.id.surface);
videoFL = (FrameLayout) findViewById(R.id.videoFL);
SurfaceHolder holder = mPreview.getHolder();
holder.addCallback(this);
holder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
Lateron I try to attach my first MediaPlayer to it:
MediaPlayer mediaPlayer = new MediaPlayer();
mediaPlayer.setDataSource(videos.get(position));
mediaPlayer.prepareAsync();
mediaPlayer.setOnBufferingUpdateListener(this);
mediaPlayer.setOnCompletionListener(this);
mediaPlayer.setOnPreparedListener(this);
mediaPlayer.setOnVideoSizeChangedListener(this);
mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
mediaPlayer.setDisplay(holder);
While Streaming the first video I am already buffering the second one to save my users time. To attach the second already prepared MediaPlayer instance to my SurfaceView I do the following an release the old one:
mediaPlayer.release();
mediaPlayer = null;
....
mediaPlayer2.setDisplay(holder);
On ICS and Jelly Bean this solution works fine but on Gingerbread it doesn't work as planned. The video is being played but not displayed (black screen and only once a picture is shown somewhere within the video) which I recognized via:
mediaPlayer2.getCurrentPosition();
where I always get the correct position on my SeekBar. I have tried it on an HTC Wildfire S.
Has anyone possibly a guess why this error could happen on Gingerbread?
With regards,
tsemann
I did reduce my version now to one MediaPlayer because as I stated in my question it is true that some 2.x devices do not have the capability to use a second MediaPlayer instance in the same Activity.
So I always reinstantiate my MediaPlayer by calling the prepare()-method for the upcoming video.

Android Media Player

I am attempting to play a sound using a MediaPlayer object, but I cannot seem to get it to work despite my best efforts. The sound simply refuses to play.
It's a short sound, that is supposed to be played when the screen is touched, meaning it will have to be repeated many times without too much delay. Knowing this I followed the state diagram, http://developer.android.com/reference/android/media/MediaPlayer.html. I can't seem to see what exactly is wrong with my sequencing of method calls.
MediaPlayer mp = MediaPlayer.create(this.getContext(), R.raw.select2);
try {
mp.prepare();
mp.start();
Log.e("debug","sound played");
}
catch(Exception e) {}
mp.stop();
You call prepare() on the mediaplayer, but the create() call you use prepares the player automatically, this causes an IllegalStateException when you try to call prepare() again, and you are sent to your catch() (you would have noticed this if you handled the exception in some way, i.e. printing the stack trace).
MediaPlayer player = MediaPlayer.create(this.getContext(), R.raw.select2);
we configure the music player by setting some of its properties as shown below
player.setWakeMode(getApplicationContext()PowerManager.PARTIAL_WAKE_LOCK);
player.setAudioStreamType(AudioManager.STREAM_MUSIC);
player.setOnPreparedListener(this);
player.setOnCompletionListener(this);
player.setOnErrorListener(this);
try {
player.prepare();
player.start();
Log.e("debug","sound played");
} catch(Exception e) {}
player.stop();

Categories

Resources