Android: Video in TextureView - android

I came to know that we can play video in texture view. But I saw only how to play videos capturing by camera but I want to play an available video in it to perform any animations.
I have tried this code but I don't know where to place video.mp4
public class MainActivity extends Activity implements
TextureView.SurfaceTextureListener {
#Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.activity_main);
TextureView textureView = (TextureView) findViewById(R.id.textureView1);
textureView.setSurfaceTextureListener(this);
}
public void onSurfaceTextureAvailable(SurfaceTexture surface, int width,
int height) {
MediaPlayer mediaPlayer = new MediaPlayer();
mediaPlayer.setSurface(new Surface(surface));
try {
mediaPlayer.setDataSource("video.mp4");
mediaPlayer.prepare();
mediaPlayer.start();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
#Override
public boolean onSurfaceTextureDestroyed(SurfaceTexture arg0) {
// TODO Auto-generated method stub
return false;
}
#Override
public void onSurfaceTextureSizeChanged(SurfaceTexture arg0, int arg1,
int arg2) {
// TODO Auto-generated method stub
}
#Override
public void onSurfaceTextureUpdated(SurfaceTexture arg0) {
// TODO Auto-generated method stub
}
}
Please help me by giving any working code. Or if mine was correct then where to place video.mp4 in the folders.
Thanks in advance...

Place it in raw folder and try the below one and see if it helps.
public void onSurfaceTextureAvailable(SurfaceTexture surface, int width, int height) {
MediaPlayer mediaPlayer = new MediaPlayer();
mediaPlayer.setSurface(new Surface(surface));
try {
Uri video = Uri.parse("android.resource://" + getPackageName() + "/" + R.raw.your_raw_file);
mediaPlayer.setDataSource(this, video);
mediaPlayer.prepare();
mediaPlayer.start();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

Related

played video again automatically after finished

I am doing android application related Video Player.I played Video but I want to do When Video complete,it resumption with automatically in application how can I do? I am Using VideoView in my appliation.
// Displays a video file.
VideoView mVideoView = (VideoView) findViewById(R.id.videoview);
String uriPath = "android.resource://com.example.anket4dammy/"
+ R.raw.dunyagoz;
Uri uri = Uri.parse(uriPath);
mVideoView.setVideoURI(uri);
mVideoView.requestFocus();
mVideoView.start();
}
#Override
public boolean onTouchEvent(MotionEvent event) {
int eventaction = event.getAction();
switch (eventaction) {
case MotionEvent.ACTION_DOWN:
// timer.cancel();
Intent intent = new Intent(getApplicationContext(),
MainActivity.class);
intent.putExtra("EXIT", false);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
break;
}
return true;
}
#Override
public void surfaceChanged(SurfaceHolder holder, int format, int width,
int height) {
// TODO Auto-generated method stub
}
#Override
public void surfaceCreated(SurfaceHolder holder) {
// TODO Auto-generated method stub
}
#Override
public void surfaceDestroyed(SurfaceHolder holder) {
// TODO Auto-generated method stub
}
Try below code
// video finish listener
mVideoView
.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
#Override
public void onCompletion(MediaPlayer mp) {
// not playVideo
// playVideo();
mp.start();
}
});

get the current state of MediaPlayer in Android

I am not much experienced so I copied code from somewhere; but Now I can get the state mediaplayer; All I want to do is to get the current state of mediaplayer and then show pause button when it's playing ; and play button when it's paused/buffering;
I am using shoutcast streaming in it; I want to show pause button or something loading status when it is being loaded initially (buffering); and when it is loaded and started playing the the pause button ; and when for some reason it starts buffering again it should show play button or Loading status on main activity
here is my code;
public class Myradio extends AsyncTask implements OnBufferingUpdateListener {
private static Context mContext;
// private static MyProgressDialog pdialog;
public static MediaPlayer mp = new MediaPlayer();
private MediaPlayer mpLoop = new MediaPlayer();
public Myradio(Context theContext) {
mContext = theContext;
mp.setOnBufferingUpdateListener(new MediaPlayer.OnBufferingUpdateListener() {
public void onBufferingUpdate(MediaPlayer arg0, int arg1) {
Log.v("Buffring Update", "");
// TODO Auto-generated method stub
}
});
}
public static void startRadio(String streamUrl) {
mp.reset();
mp.setOnErrorListener(new MediaPlayer.OnErrorListener() {
public boolean onError(MediaPlayer mp, int what, int extra) {
Log.e(getClass().getName(), "Error in MediaPlayer: (" + what
+ ") with extra (" + extra + ")");
return false;
}
});
try {
mp.setDataSource(streamUrl);
mp.prepare();
mp.start();
} catch (IllegalArgumentException e) {
} catch (IllegalStateException e) {
} catch (IOException e) {
}
}
public static void stopRadio() {
mp.stop();
}
public static void resumeRadio() {
// mp.();
// mp.setLooping(false);
// mpLoop.stop();
}
#Override
protected Object doInBackground(Object... params) {
// TODO Auto-generated method stub
return null;
}
#Override
public void onBufferingUpdate(MediaPlayer mp, int percent) {
// TODO Auto-generated method stub
Log.v("Buffring Update", "Buffring Update");
}
}
You can do this like (see here):
if(MediaPlayer.isPlaying()){
//show the pause button
}
else{
//show the play button
}
Check this tut too.

Pausing MediaPlayer video and keeping screen visible

I was wondering if there was a way to pause the mediaplayer and keep the screen visible. What I mean by that is keep the current image on the screen as it is paused. What i've done is set up a timer that pauses the mediaplayer for a certain amount of time. As it is paused for that time I want current video to be visable. However, everytime I pause the Mediaplayer the screen goes black untill I call the Mediplayer.start() again. Is there anyway around this.
Thanks
Edit with code
public class FullImageActivity extends Activity implements SurfaceHolder.Callback, OnPreparedListener, OnErrorListener{
private static final String TAG = null;
MediaPlayer player;
SurfaceView surfaceview;
SurfaceHolder surfaceHolder;
int width;
int height;
String path;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.full_image);
Intent i = getIntent();
long id = i.getExtras().getLong("id");
path = i.getExtras().getString("videopath");
surfaceview = (SurfaceView)findViewById(R.id.surfaceview);
surfaceHolder = surfaceview.getHolder();
surfaceHolder.addCallback(this);
player = new MediaPlayer();
playVideo();
}
public void playVideo(){
try {
player.reset();
player.setOnErrorListener(this);
player.setDataSource(path);
player.setOnPreparedListener(this);
player.prepareAsync();
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (SecurityException e) {
e.printStackTrace();
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
#Override
protected void onPause() {
player.release();
Log.i(TAG, "Mediaplayer was relased");
super.onPause();
}
#Override
protected void onResume() {
playVideo();
super.onResume();
}
#Override
protected void onStop() {
// TODO Auto-generated method stub
super.onStop();
}
#Override
public void surfaceChanged(SurfaceHolder arg0, int arg1, int arg2, int arg3) {
// TODO Auto-generated method stub
}
#Override
public void surfaceCreated(SurfaceHolder holder) {
player.setDisplay(holder);
}
#Override
public void surfaceDestroyed(SurfaceHolder arg0) {
// TODO Auto-generated method stub
}
#Override
public void onPrepared(MediaPlayer p) {
width=player.getVideoWidth();
height=player.getVideoHeight();
if (width!=0 && height!=0) {
surfaceHolder.setFixedSize(width, height);
player.start();
player.pause();
Timer timer = new Timer();
timer.schedule(new TimerTask()
{
public void run()
{
player.start();
}
}, 3000);
}
}

When seekBar updating Android's MediaPlayer video is not smooth

I'm playing video via MediPlayer in my android application and have SeekBar displayed. Now I want this seeks bar to automatically update as the video progresses so it should automatically move from left to right. At the moment, (code below) the bar updates and this is done via running thread, that every second updates the progress of seekBar. The problem is it is not smooth and as seekBar is updated via its seekProgress() the video stops for split second and all is very jumpy. Now I would like it to have updated more often then every second as well as keep functionality that I already implemented to allow user to tap on the bar and change progress of the video.
I'm after something like Android MediaPLayer application have, seekBar is on transparent background and all is smooth and I have no idea how it is done.
No, currently as you see from the code below thread updates every second as it sleeps inside f run method. I've also tried to use handlers to update UI thread, effect was the same. I also extended SeekBar to its own class, had thread there and this was no good either, exactly same effect.
If anyone can explain to me how to solve this problem and how its done with other player appls that would be great.
public class FightPlayerActivity extends Activity implements Runnable, OnSeekBarChangeListener, SurfaceHolder.Callback, OnPreparedListener {
private MediaPlayer mp=null;
private SeekBar seekBar;
private Thread progressBarUpdater;
private String filePath;
private Handler handler=new Handler();
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
Toast.makeText(this,"Create ", 2000).show();
}
public void surfaceChanged(SurfaceHolder holder, int format, int width,
int height) {
// TODO Auto-generated method stub
}
public void onStop()
{
super.onStop();
mp.stop();
mp.reset();
mp.release();
}
public void run()
{
while(true)
{
try {
progressBarUpdater.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
seekBar.setProgress(mp.getCurrentPosition());
// handler does have same effect, so video stops for split second
//handler.postDelayed(this, 1000);
}
}
public void onStart()
{
super.onStart();
setContentView(R.layout.fight_player);
filePath=getIntent().getStringExtra("filename");
filePath=Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM)+"/FightAll_BJJ_Scoring/"+filePath;
Toast.makeText(this,filePath, 2000).show();
// seek bar
seekBar=(SeekBar) findViewById(R.id.seek_bar);
seekBar.setOnSeekBarChangeListener(this);
try {
SurfaceView sv=(SurfaceView) findViewById(id.video_preview);
SurfaceHolder sh=sv.getHolder();
sh.addCallback(this);
sh.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
} 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();
}
}
public void stop(View view)
{
mp.seekTo(0);
mp.pause();
}
public void pause(View view)
{
mp.pause();
}
public void play(View view)
{
mp.start();
}
public void surfaceCreated(SurfaceHolder holder) {
try {
mp=new MediaPlayer();
mp.setDataSource(filePath);
mp.setDisplay(holder);
mp.setOnPreparedListener(this);
mp.setAudioStreamType(AudioManager.STREAM_MUSIC);
mp.prepare();
//handler.removeCallbacks(this);
//handler.postDelayed(this, 1000);
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void surfaceDestroyed(SurfaceHolder holder) {
// TODO Auto-generated method stub
}
public void onPrepared(MediaPlayer mediaplayer) {
mp.start();
seekBar.setMax(mp.getDuration());
progressBarUpdater=new Thread(this);
progressBarUpdater.start();
//handler.postDelayed(this, 1000);
}
public void onProgressChanged(SeekBar sb,int progress,boolean fromUser)
{
//Toast.makeText(this, progress, 2000).show();
mp.seekTo(progress);
}
public void onStartTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub
onProgressChanged(seekBar,seekBar.getProgress(),true);
}
public void onStopTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub
}
}
Your major problem is in your onProgressChanged() method.
You are seeking to the specified position every time the seekBar progress changes, even when it is done programmatically. Which means that every time you call seekBar.setProgress(mp.getCurrentPosition()), onProgressChanged() will be fired.
So we change it to the following:
public void onProgressChanged(SeekBar sb, int progress, boolean fromUser) {
if (fromUser) {
mp.seekTo(progress);
}
}
That way it will only be fired when the user moves the seekBar.
Moreover, according to this answer, it would be better to replace your while(true) loop with:
public void run() {
seekBar.setProgress(mp.getCurrentPosition());
if (mp.getCurrentPosition() < mp.getDuration()) {
seekBar.postDelayed(this, MILLISECONDS);
}
}

Android media player streaming

i am working on android media player, which streams the video and displays. The application is able to buffer but the video is not being displayed on surface view though i am able to hear the sound.
The following is my code:
public class VideoPlayerActivity extends Activity implements OnPreparedListener, OnCompletionListener, OnBufferingUpdateListener, OnSeekCompleteListener, OnErrorListener, SurfaceHolder.Callback {
private VideoView videoView;
private MediaPlayer mp;
private SurfaceView surfaceView;
private SurfaceHolder surfaceHolder;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// getWindow().setFormat(PixelFormat.UNKNOWN);
surfaceView = (SurfaceView) findViewById(R.id.surfaceView1);
surfaceHolder = surfaceView.getHolder();
surfaceHolder.addCallback(this);
// surfaceHolder.setFixedSize(176, 144);
// surfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
try {
String path = "http://xxxxxxxxxxx/4_xxx.mp4";
mp = new MediaPlayer();
mp.setDataSource(path);
mp.setDisplay(surfaceHolder);
mp.prepare();
mp.setOnPreparedListener(this);
mp.setOnBufferingUpdateListener(this);
mp.setOnCompletionListener(this);
mp.setOnSeekCompleteListener(this);
mp.setOnErrorListener(this);
mp.setAudioStreamType(AudioManager.STREAM_MUSIC);
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
mp.start();
// Uri uri = Uri.parse(path);
//
// videoView = (VideoView) findViewById(R.id.videoView1);
//
// videoView.setVideoURI(uri);
//
// videoView.setMediaController(new MediaController(this));
//
//
//
// videoView.setOnPreparedListener(this);
//
//
//
// videoView.requestFocus();
//
// videoView.start();
}
#Override
public void onPrepared(MediaPlayer mp) {
System.out.println("=======In on prepared======Media Player=====" + mp);
}
#Override
public void onCompletion(MediaPlayer mp) {
System.out.println("=======In on completion====== Media player====" + mp);
}
#Override
public void onBufferingUpdate(MediaPlayer mp, int percent) {
System.out.println("=======In on Buffering update======Media Player====" + mp + "====percent====" + percent);
}
#Override
public boolean onError(MediaPlayer mp, int what, int extra) {
System.out.println("=======In on Error======Media Player====" + mp + "=====what====" + what + "====extra====" + extra);
return false;
}
#Override
public void onSeekComplete(MediaPlayer mp) {
System.out.println("=======In on seek complete======Media Player====" + mp);
}
#Override
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
// TODO Auto-generated method stub
}
#Override
public void surfaceCreated(SurfaceHolder holder) {
// TODO Auto-generated method stub
}
#Override
public void surfaceDestroyed(SurfaceHolder holder) {
// TODO Auto-generated method stub
}
}
#Override
public void surfaceCreated(SurfaceHolder holder) {
// TODO Auto-generated method stub
mp.setDisplay(holder);
play();
}

Categories

Resources