how to clear surface holder when media player is finished? - android

I made a video player with surfaceview and mediaplayer.
i have 10 videos and 10 buttons.
if click on each buttons, each videos are playing.
here is my code..
//onCreate
holder = surfaceview.getHolder();
holder.addCallback(this);
holder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
//Button1
if(mp == null)mp = new MediaPlayer();
mp.setDataSource(mediaplay_path);
mp.setDisplay(holder);
mp.setScreenOnWhilePlaying(true);
mp.setAudioStreamType(AudioManager.STREAM_MUSIC);
mp.prepare();
mp.start();
//Button2
if(mp != null){
mp.stop();
mp.reset();
}
mp.setDataSource(mediaplay_path2);
mp.setDisplay(holder);
mp.setScreenOnWhilePlaying(true);
mp.setAudioStreamType(AudioManager.STREAM_MUSIC);
mp.prepare();
mp.start();
//Button3~Button10 is same as Button2..
everything is fine.
my custom videoview is working alright.
but when the video turns to the next, the last scene of the previous video is remain for a while and turns to the next video scene.
i think it's because the previous surfaceview should be clear before next video is playing.
but i have no idea how to clear the surfaceview or surface holder.
i've searched for this but only could find how to play the video, not how to clear the surfaceview which is set the disaply from mediaplayer.
please help me~~!

Took me two weeks to figure this out. By setting the surfaceholder to TRANSPARENT, Android will destroy the surface. Then setting it back to OPAQUE creates a new surface "clearing" the surface. Note surfacecreate and surfacedestroy events will fire, so if you have code there, beware. I put a imageview set to black to give it a black background. There maybe better ways for that.
private void playVideoA() {
imageViewBlack.bringToFront();
surfaceHolder.setFormat(PixelFormat.TRANSPARENT);
surfaceHolder.setFormat(PixelFormat.OPAQUE);
surfaceView.bringToFront();
mediaPlayerA.setDisplay(surfaceHolder);
//surfaceView.setAlpha((float) 0.01);
mediaPlayerA.start();
};
private void prepareVideoA(String url) {
try {
mediaPlayerA = new MediaPlayer();
mediaPlayerA.setDataSource(url);
mediaPlayerA.prepareAsync();
mediaPlayerA.setOnPreparedListener(this);
mediaPlayerA.setOnCompletionListener(this);
mediaPlayerA.setOnBufferingUpdateListener(this);
mediaPlayerA.setOnInfoListener(this);
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SecurityException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
};
#Override
public void onPrepared(MediaPlayer mp) {
playVideoA()
}

video.getHolder().setFormat(PixelFormat.TRANSPARENT);
video.getHolder().setFormat(PixelFormat.OPAQUE);
video.setVideoURI(Uri.parse(temp));
video.setOnPreparedListener(new OnPreparedListener() {
#Override
public void onPrepared(MediaPlayer mp) {
mp.start();
}
});
it work for me

The SurfaceHolder has lockCanvas methods that will allow you to draw directly to the Surface. Use the drawColor method of the Canvas to fill it with black.
That said, it may be preferable to remove the SurfaceView (as suggested by smile2you), because that should destroy the Surface altogether and free up unused resources. And definitely make sure you are calling release on the MediaPlayers after they are done with playback. Holding on to too many video resources will crash your app.

may be you can use removeView to remove the old custome videoview ,then add the new view

surfaceview.setVisibility(View.GONE);

Related

Audio file in application doesn't stop on click, it starts playing again

So I put an audio file in my application and it's supposted play when I touch the button and stop when I touch it again.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button one = (Button) findViewById(R.id.buttonId);
final MediaPlayer mp = new MediaPlayer();
one.setOnClickListener(new View.OnClickListener(){
public void onClick(View v) {
if(mp.isPlaying())
{
mp.stop();
}
try {
mp.reset();
AssetFileDescriptor afd;
afd = getAssets().openFd("mosq.mp3");
mp.setDataSource(afd.getFileDescriptor(),afd.getStartOffset(),afd.getLength());
mp.prepare();
mp.setLooping(true);
mp.start();
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
});
here is my code, this part:
if(mp.isPlaying())
{
mp.stop();
}
didn't work for some reason.
Make sure you put a return statement below mp.stop().
From what I can understand the sound does stop but then it starts again because the next part of the code still gets executed
As George D correctly pointed out, you start the media playing unconditionally, even if you just stopped it. You could use his solution or do something like:
if(mp.isPlaying())
{
mp.stop();
}
else {
try {
mp.reset();
AssetFileDescriptor afd;
afd = getAssets().openFd("mosq.mp3");
mp.setDataSource(afd.getFileDescriptor(),afd.getStartOffset(),afd.getLength());
mp.prepare();
mp.setLooping(true);
mp.start();
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
This has several other potential bugs in it:
* I'm not sure if this is what you intended but the player won't ever pause, just stop and restart from the beginning. If you try to resume or play again it'll completely reload the audio file every time. At a minimum this is a waste of resources, plus it's likely not the expected behavior from a UI perspective.
* You don't want to define the MediaPlayer object as a local variable within the OnCreate method. The only reason this works at all is you have a memory leak (you never unsubscribe your event handler for the click); if you didn't have the memory leak the object would become eligible for garbage collection as soon as you completed the onCreate method and, as far as the framework was concerned, it would no longer exist.

Android: Record and play video in the same SurfaceView

I want to see the video that I'm currently recording in a SurfaceView and if the recodring finished I wanto to replay the video in the same SurfaceView.
Even though I thought it would be a common feature in Apps, I couldn't find any example and couldn't make my code work.
private void prepareRecorder() {
try {
recorder = new MediaRecorder();
recorder.setPreviewDisplay(holder.getSurface());
camera.unlock();
recorder.setCamera(camera);
recorder.setAudioSource(MediaRecorder.AudioSource.DEFAULT);
recorder.setVideoSource(MediaRecorder.VideoSource.DEFAULT);
camcorderProfile = CamcorderProfile.get(CamcorderProfile.QUALITY_LOW);
recorder.setProfile(camcorderProfile);
File newFile = new File(videoFilePath);
recorder.setOutputFile(newFile.getAbsolutePath());
try {
recorder.prepare();
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}catch(Exception e){
releaseCamera();
}
}
//
public void onClick(View v) {
if(mediaPlayer!=null) {
mediaPlayer.reset();
mediaPlayer.release();
}
mediaPlayer = new MediaPlayer();
try {
mediaPlayer.setDataSource(videoFile.getAbsolutePath());
SurfaceView surfaceView = (SurfaceView) getView().findViewById(R.id.videoInvitationFragSurfaceView);
mediaPlayer.setSurface(surfaceView.getHolder().getSurface());
mediaPlayer.prepareAsync();
mediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
#Override
public void onPrepared(MediaPlayer mediaPlayer) {
mediaPlayer.start();
}
});
} catch (IOException e) {
e.printStackTrace();
}
}
If I just record the video my code works, if just play the video it works as well. Any ideas?
Thanks in advance
Edit: As a current workaround I use two SurfaceViews and "hide" them (set size to 0) if needed
If I just record the video my code works, if just play the video it works as well. Any ideas?
As much I understand, you can reuse same surface view only after it is destroyed and regenerated, i.e you can reuse same SurfaceView only after surfaceDestroyed() is called and then surfaceCreated() is called only after this destroy and create cycle you can reuse SurfaceView.
To achive this all you have to do is: after video recording is done set SurfaceView visibility to GONE which will ensure surfaceDestroyed() call back and again set SurfaceView visibility to VISIBLE which will call surfaceCreated() after that start video playback.
This is very cumbersome process so I used same SurfaceView for recording and playback in following way:
when Activity is created check if VideoFile is available in Intent
if yes use SurfaceView for playback
now if user deletes video restart activity
As VideoFile is not found in intent use SurfaceView for recording
Once recording is done and user want to playback video restart
Activity with VideoFile information attached in Intent

MediaPlayer : Should have subtitle controller already set: KitKat

I am having an odd issue where my audio file sometimes plays and sometimes does not play. The catch is that when it decides to not play, the DDMS gives me an:
E/MediaPlayer﹕ Should have subtitle controller already set
Because this is one-to-one with the music not playing, I have determined that this is probably the issue...
If the music is not playing and I hit the volume button it begins to play.
If I wait about 30 seconds of no-play, it begins to start again (not looping).
Whats going on here? I am on KitKat using
player = new MediaPlayer();
AssetFileDescriptor afd = null;
try {
afd = getAssets().openFd("Theme.mp3");
} catch (IOException e) {
e.printStackTrace();
}
try {
player.setDataSource(afd.getFileDescriptor(), afd.getStartOffset(), afd.getLength());
} catch (IOException e) {
e.printStackTrace();
}
try {
player.prepare();
} catch (IOException e) {
e.printStackTrace();
}
player.setLooping(true); //restart playback end reached
//player.setVolume(1, 1); //Set left and right volumes. Range is from 0.0 to 1.0
player.start(); //start play back
Looking at a previous discussion on StackOverflow, and the referenced Android commit where this was introduced, the code above might not completely initialize the MediaPlayer object.
The KitKat example code for media playback suggests that you should call:
mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
immediately after you construct the MediaPlayer, and before you call its setDataSource method.
I had the same issue and I fixed it by adding the following right after instantiating MediaPlayer.
mediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
#Override
public void onPrepared(MediaPlayer mp) {
if (mp == mediaPlayer) {
mediaPlayer.start();
}
}
});
Previously I was implementing MediaPlayer.OnPreparedListener and overriding onPrepared() but it didn't work.
I hope this helps!
This should fix your problem (did for me): Replace the line that says "player.start()" following the rest of your code with an async callback like so:
player.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
#Override
public void onPrepared(MediaPlayer mediaPlayer) {
mediaPlayer.start();
}
});
This error is just a Log.e, not a real error. It shouldn't cause your player to not play, I'm guessing it's just because the player hadn't finished preparing when you try to call start().
E/MediaPlayer﹕ Should have subtitle controller already set
Its been a long time since I was working on this app. Here is what I ended up doing to get this to work. (Tested on KitKat and Lollipop). I think switching from MediaPlayer to APMediaPlayer was part of the trick.
#Override
public void onDestroy() {
if(player != null) {
player.release();
player = null;
}
super.onDestroy();
}
#Override
public void onStart() {
super.onStart();
if(player != null) {
player.start();
}
else {
player = new APMediaPlayer(this); //create new APMediaPlayer
player.setMediaFile("Theme.mp3"); //set the file (files are in data folder)
player.start(); //start play back
player.setLooping(true); //restart playback end reached
player.setVolume(1, 1); //Set left and right volumes. Range is from 0.0 to 1.0
}
}
#Override
public void onResume() {
super.onResume();
if(player != null) {
player.start();
}
}
set in manifest file may help you
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />

nulll pointer exception for pause method in Mediaplayer for android

I keep getting null pointer exceptions for methods of MediaPlayer. I was finally able to get the play function to work by moving the code for play and initialize of play functions into a separate method and call that method from inside the onClick listener.
However am still getting null pointer exception for the apps pause function. I am using pause method of media player. How to the get pause to work? I think the problem is somewhere in the structure of my code and how it is organized.
I tried moving the initialization of the Media player to a different place in the code. and and nothing seems to work. Any ideas?
// onclick listener for the playing the selected song
playB.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
playSong();
}
});
// onclick listener for pausing the song that is playing
pauseB.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
pauseSong();
}
});
// method to pause song
public void pauseSong(){
player.pause();
length = player.getCurrentPosition();
}
// method to play song and initialize the MediaPlayer class with one file
// from the drawable folder, need to initialize with something or it will be null
// for that i decided to use an mp3 in R.raw folder
public void playSong(){
// Play song
MediaPlayer player = MediaPlayer.create(this, R.raw.g2);
player.reset();
try {
player.setDataSource(selectedAudioPath);
player.prepare();
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
player.seekTo(length);
player.start();
} // play method
You have to make a MediaPlayer player global variable. player is not visible for the pauseSong() method therefore you have nullPointerException. Create a MediaPlayer player in your main class and then in onPlaySong() initialize it only like this:
player = MediaPlayer.create(this, R.raw.g2);

android media player local object many different sounds should I release?

in one activity I have 5 buttons. Each of these buttons make a different sound.
atm I'm doing on each buttonClicked method:
MediaPlayer mp = MediaPlayer.create(this, R.raw.click);
if(mp != null) mp.start();
MediaPlayer mp = MediaPlayer.create(this, R.raw.click2);
if(mp != null) mp.start();
etc..
is this the right way to do it, and I wonder since mp is local object, doesn't it die when the method dies, ergo no need to call mp.release() ?
note: my sounds are 0.5 sec or less and they seem to complete more often than not (haven't tested in many devices though). I'm targeting 2.1+
You need to declare your Mediaplayer reference globally need to assign mediaplayer object in the onCreate() then in button click call MediaPlayer.create(this/getApplicationContext(),R.Raw.yourfile); follow http://developer.android.com/reference/android/media/MediaPlayer.html#StateDiagram
mp.reset();
mp=MediaPlayer.create(getApplicationContext(),R.raw.hummingbird);
try {
mp.prepare();
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
mp.start();

Categories

Resources