How To Attach Mediaplayer With Surfaceview In Android?
I'm building video player with android media player object. i'm able to hear the audio but the video does not appear on surfaceView. here is my code public class PlayerActivity
Solution 1:
finally i fixed it myself. just called the mp.setDisplay(holder);
inside the surfaceCreated() function. and the final code is
publicclassPlayerActivityextendsActivityimplementsSurfaceHolder.Callback {
String path;
privateMediaPlayer mp;
privateSurfaceView mPreview;
privateSurfaceHolder holder;
boolean pausing = false;
publicstaticString filepath;
@OverridepublicvoidonCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_player);
getWindow().setFormat(PixelFormat.UNKNOWN);
mPreview = (SurfaceView)findViewById(R.id.surfaceView);
holder = mPreview.getHolder();
holder.setFixedSize(800, 480);
holder.addCallback(this);
holder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
mp = newMediaPlayer();
try{
Intent intent = getIntent();
Uri fileuri = intent.getData();
filepath=fileuri.getPath();
}catch(Exception e){}
}
protectedvoidonPause(){
super.onPause();
mp.release();
}
@OverridepublicvoidsurfaceChanged(SurfaceHolder holder, int format, int width,
int height) {
// TODO Auto-generated method stub
}
@OverridepublicvoidsurfaceCreated(SurfaceHolder holder) {
// TODO Auto-generated method stub
mp.setDisplay(holder);
play();
}
@OverridepublicvoidsurfaceDestroyed(SurfaceHolder holder) {
// TODO Auto-generated method stub
}
voidplay(){
try {
mp.setDataSource(filepath);
mp.prepare();
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
mp.start();
}
}
Solution 2:
It is not working for me, throwing error illegal state exception in
surfaceCreated()
method at linemp.setDisplay(holder);
Declare mp.setDisplay(holder)
as given below :
@Override
public void surfaceCreated(SurfaceHolder holder)
{
mediaPlayer.setDataSource(this,uri);
mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
mediaPlayer.prepare();
mp.setDisplay(holder);
mp.start;
}
Post a Comment for "How To Attach Mediaplayer With Surfaceview In Android?"