This question already has answers here:
Android: Video View: how to play a video on a loop
(2 answers)
Closed 5 years ago.
How to make the video to loop seamlessly as of now when it comes to an end of the clip and starts the clip again
`public class TvPlay extends Activity implements OnCompletionListener,
OnErrorListener, OnPreparedListener {
private VideoView mVideoView;
private String url;
private ProgressBar load;
private TextView empty;
private AdView mAdView;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
// Vitamio.isInitialized(this);
Vitamio.isInitialized(getApplicationContext());
this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.tvplay);
mAdView = (AdView) findViewById(R.id.adView);
mAdView.loadAd(new AdRequest.Builder().build());
Log.d("url=", getIntent().getStringExtra("url"));
url = getIntent().getStringExtra("url");
init();
}
public void init() {
load = (ProgressBar) this.findViewById(R.id.load);
empty = (TextView) this.findViewById(R.id.empty);
mVideoView = (VideoView) this.findViewById(R.id.surface_view);
mVideoView.setOnCompletionListener(this);
mVideoView.setOnPreparedListener(this);
mVideoView.setOnErrorListener(this);
Uri videoUri = Uri.parse(url);
mVideoView.setVideoURI(videoUri);
mVideoView.requestFocus();
loading();
}
private void loading() {
load.setVisibility(View.GONE);
empty.setVisibility(View.GONE);
}
private void loadComplete(MediaPlayer arg0) {
load.setVisibility(View.GONE);
// vv.setVisibility(View.VISIBLE);
empty.setVisibility(View.GONE);
mVideoView.start();
mVideoView.resume();
}
private void error(String msg) {
load.setVisibility(View.GONE);
mVideoView.setVisibility(View.GONE);
empty.setVisibility(View.VISIBLE);
if (msg != null)
empty.setText(msg);
}
#Override
public void onPrepared(MediaPlayer mp) {
// TODO Auto-generated method stub
Log.d("ONLINE TV", "Prepared");
loadComplete(mp);
}
#Override
public boolean onError(MediaPlayer mp, int what, int extra) {
// TODO Auto-generated method stub
Log.d("ONLINE TV", "Error");
error("Unable to play this channel.");
return false;
}
#Override
public void onCompletion(MediaPlayer mp) {
// TODO Auto-generated method stub
Log.d("ONLINE TV", "Complete");
}
}
`
this will make your video keep playing
mVideoView.setOnPreparedListener(new OnPreparedListener() {
#Override
public void onPrepared(MediaPlayer mp) {
mp.setLooping(true);
}
});
Related
On click of a linear layout I need to pause my streaming in the mediaplayer which is associated with a surface view.
But the player is not pausing.(I'm testing in emulator).
here is the code
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
Thread.setDefaultUncaughtExceptionHandler(onBlooey);
setContentView(R.layout.main);
surface = (TappableSurfaceView) findViewById(R.id.surface);
surface.addTapListener(onTap);
holder = surface.getHolder();
holder.addCallback(this);
holder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
topPanel = findViewById(R.id.top_panel);
bottomPanel = findViewById(R.id.bottom_panel);
timeline = (SeekBar) findViewById(R.id.timeline);
timeline.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
#Override
public void onProgressChanged(SeekBar seekBar, int progress,
boolean fromUser) {
// TODO Auto-generated method stub
if (player != null) {
if (player.isPlaying()) {
player.seekTo(progress);
} else {
player.start();
player.seekTo(progress);
}
}
}
#Override
public void onStartTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub
}
#Override
public void onStopTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub
}
});
media = (ImageButton) findViewById(R.id.media);
media.setOnClickListener(onMedia);
seekbarTopPanel = (LinearLayout) findViewById(R.id.seekbarTopPanel);
playVideo();
clearPanels(true);
seekbarTopPanel.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
topPanel.setVisibility(View.VISIBLE);
if (player != null) {
System.out.println("in player pause");
player.pause();
}
}
});
}
public void surfaceCreated(SurfaceHolder holder) {
player.setDisplay(holder);
}
private void playVideo() {
try {
media.setEnabled(false);
if (player == null) {
System.out.println("media player created");
player = new MediaPlayer();
player.setScreenOnWhilePlaying(true);
} else {
player.stop();
player.reset();
}
// url="C:\\Users\\Sai Dinakar\\Downloads\\Relaxing Music & Beautiful Nature 6 1080p HD.mp3";
Uri url = Uri.parse("android.resource://" + getPackageName() + "/"
+ R.raw.nature);
// player.setDataSource(url);
player.setDataSource(getApplicationContext(), url);
player.setAudioStreamType(AudioManager.STREAM_MUSIC);
player.setOnPreparedListener(this);
player.prepareAsync();
// player.setOnBufferingUpdateListener(this);
player.setOnCompletionListener(this);
VideoTimerHandler videoTimerHandler = new VideoTimerHandler();
videoTimerHandler.playVideoTimer();
} catch (Throwable t) {
Log.e(TAG, "Exception in media prep", t);
}
}
The issue is in oncreate -->seekbarTopPanel.setOnClickListener(new OnClickListener()
The on pause is getting called but still the video is not pausing.
public class VideoDialog extends YouTubeFailureRecoveryActivity {
List<String> playlist;
AlertDialog dialog;
YouTubePlayer player;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
requestWindowFeature(Window.FEATURE_PROGRESS);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
Bundle bundle = getIntent().getExtras();
playlist=new ArrayList<String>();
playlist=bundle.getStringArrayList("VIDEO_LINK");
if(!playlist.isEmpty())
{
dialog = new AlertDialog.Builder(this).create();
LayoutInflater inflater = LayoutInflater.from(this);
dialog.getWindow().requestFeature(Window.FEATURE_NO_TITLE);
View view = inflater.inflate(R.layout.exvideo_lauout, null); // xml Layout file for imageView
Button btnCloseimg=(Button)view.findViewById(R.id.eximageclose);
btnCloseimg.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
if(player.isPlaying())
{
player.release();
}
playlist.clear();
playlist=null;
finish();
}
});
YouTubePlayerView youTubeView = (YouTubePlayerView)view. findViewById(R.id.youtube_view);
youTubeView.initialize(DEVELOPER_KEY,this);
dialog.setView(view);
WindowManager.LayoutParams lp = new WindowManager.LayoutParams();
lp.copyFrom(dialog.getWindow().getAttributes());
lp.width = WindowManager.LayoutParams.MATCH_PARENT;
lp.height = WindowManager.LayoutParams.MATCH_PARENT-150;
dialog.show();
dialog.setCanceledOnTouchOutside(false);
dialog.getWindow().setAttributes(lp);
}
else
{
Toast.makeText(this,"Error in URL..",Toast.LENGTH_SHORT).show();
}
}
#Override
protected void onDestroy() {
super.onDestroy();
}
public void onBackPressed()
{
super.onBackPressed();
if(player.isPlaying())
{
player.release();
}
playlist.clear();
playlist=null;
finish();
}
public void onInitializationSuccess(YouTubePlayer.Provider provider, final YouTubePlayer player,boolean wasRestored) {
if (!wasRestored)
{
this.player=player;
player.loadVideos(playlist);
player.setPlayerStyle(PlayerStyle.CHROMELESS);
player.play();
player.setPlayerStateChangeListener(new PlayerStateChangeListener() {
public void onVideoStarted() {
// TODO Auto-generated method stub
}
public void onVideoEnded() {
// TODO Auto-generated method stub
if(!player.hasNext())
{
//player.cueVideos(playlist);
player.loadVideos(playlist);
player.play();
}
else
{
player.play();
}
}
public void onLoading() {
// TODO Auto-generated method stub
}
public void onLoaded(String arg0) {
// TODO Auto-generated method stub
}
public void onError(ErrorReason arg0) {
// TODO Auto-generated method stub
Toast.makeText(VideoDialog.this,"Loading Error!!",Toast.LENGTH_SHORT).show();
}
public void onAdStarted() {
// TODO Auto-generated method stub
}
});
}
}
protected YouTubePlayer.Provider getYouTubePlayerProvider() {
return (YouTubePlayerView) findViewById(R.id.youtube_view);
}
}
we need to play multiple videos inside an android app without any delay,but initial loading time is high in youtube Player APi.is there any way to reduce video quality depend on device???
Wait I found something in the docs provided with the API. Look in
/docs/reference/com/google/android/youtube/player/YouTubePlayer.PlaybackEventListener.html#onBuffering(boolean)
or just YouTubePlayer.PlaybackEventListener.html#onBuffering(boolean)
It seems to have the callback that you need to override.
Seems like there's a PlayBackEventListener which provides you with the onBuffering you're looking for.
I am a Android newbie. I want to design my personal videoplayer to play streaming music, so I use MediaPlayer and SurfaceView. The project plays streaming video successfully, but it can't be setloop. I have google many articles and read Android Developer. I can't figure out why mediaPlayer.setLooping(true) don't work here. Thanks^^
mediaPlayer.reset();
mediaPlayer.setDataSource(videoUrl);
mediaPlayer.setLooping(true);
mediaPlayer.prepare();
The code includes two parts: 1.Test_Videoplayer.java 2.Player.java
1.Test_Videoplayer.java
public class Test_Videoplayer extends Activity {
private SurfaceView surfaceView;
private Button btnPause, btnPlayUrl, btnStop;
private SeekBar skbProgress;
private Player player;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
surfaceView = (SurfaceView) this.findViewById(R.id.surfaceView1);
btnPlayUrl = (Button) this.findViewById(R.id.btnPlayUrl);
btnPlayUrl.setOnClickListener(new ClickEvent());
btnPause = (Button) this.findViewById(R.id.btnPause);
btnPause.setOnClickListener(new ClickEvent());
btnStop = (Button) this.findViewById(R.id.btnStop);
btnStop.setOnClickListener(new ClickEvent());
skbProgress = (SeekBar) this.findViewById(R.id.skbProgress);
skbProgress.setOnSeekBarChangeListener(new SeekBarChangeEvent());
player = new Player(surfaceView, skbProgress);
}
class ClickEvent implements OnClickListener {
#Override
public void onClick(View arg0) {
if (arg0 == btnPause) {
player.pause();
} else if (arg0 == btnPlayUrl) {
String url = "http://sites.google.com/site/ronforwork/Home/android-2/littleMonster.3gp";
player.playUrl(url);
} else if (arg0 == btnStop) {
player.stop();
}
}
}
class SeekBarChangeEvent implements SeekBar.OnSeekBarChangeListener {
int progress;
#Override
public void onProgressChanged(SeekBar seekBar, int progress,
boolean fromUser) {
this.progress = progress * player.mediaPlayer.getDuration()
/ seekBar.getMax();
}
#Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
#Override
public void onStopTrackingTouch(SeekBar seekBar) {
player.mediaPlayer.seekTo(progress);
}
}
}
2.Player.java <-- here is mediaPlayer.setLooping(true)
public class Player implements OnBufferingUpdateListener, OnCompletionListener,
MediaPlayer.OnPreparedListener, SurfaceHolder.Callback {
private int videoWidth;
private int videoHeight;
public MediaPlayer mediaPlayer;
private SurfaceHolder surfaceHolder;
private SeekBar skbProgress;
private Timer mTimer = new Timer();
public Player(SurfaceView surfaceView, SeekBar skbProgress) {
this.skbProgress = skbProgress;
surfaceHolder = surfaceView.getHolder();
surfaceHolder.addCallback(this);
surfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
mTimer.schedule(mTimerTask, 0, 1000);
}
TimerTask mTimerTask = new TimerTask() {
#Override
public void run() {
if (mediaPlayer == null)
return;
if (mediaPlayer.isPlaying() && skbProgress.isPressed() == false) {
handleProgress.sendEmptyMessage(0);
}
}
};
Handler handleProgress = new Handler() {
public void handleMessage(Message msg) {
int position = mediaPlayer.getCurrentPosition();
int duration = mediaPlayer.getDuration();
if (duration > 0) {
long pos = skbProgress.getMax() * position / duration;
skbProgress.setProgress((int) pos);
}
};
};
public void play() {
mediaPlayer.start();
}
public void playUrl(String videoUrl) {
try {
mediaPlayer.reset();
mediaPlayer.setDataSource(videoUrl);
mediaPlayer.setLooping(true);
mediaPlayer.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();
}
}
public void pause() {
mediaPlayer.pause();
}
public void stop() {
if (mediaPlayer != null) {
mediaPlayer.stop();
mediaPlayer.release();
mediaPlayer = null;
}
}
#Override
public void surfaceChanged(SurfaceHolder arg0, int arg1, int arg2, int arg3) {
Log.e("mediaPlayer", "surface changed");
}
#Override
public void surfaceCreated(SurfaceHolder arg0) {
try {
mediaPlayer = new MediaPlayer();
mediaPlayer.setDisplay(surfaceHolder);
mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
mediaPlayer.setOnBufferingUpdateListener(this);
mediaPlayer.setOnPreparedListener(this);
} catch (Exception e) {
Log.e("mediaPlayer", "error", e);
}
Log.e("mediaPlayer", "surface created");
}
#Override
public void surfaceDestroyed(SurfaceHolder arg0) {
Log.e("mediaPlayer", "surface destroyed");
}
#Override
public void onPrepared(MediaPlayer arg0) {
videoWidth = mediaPlayer.getVideoWidth();
videoHeight = mediaPlayer.getVideoHeight();
if (videoHeight != 0 && videoWidth != 0) {
arg0.start();
}
Log.e("mediaPlayer", "onPrepared");
}
#Override
public void onCompletion(MediaPlayer arg0) {
// TODO Auto-generated method stub
}
#Override
public void onBufferingUpdate(MediaPlayer arg0, int bufferingProgress) {
skbProgress.setSecondaryProgress(bufferingProgress);
int currentProgress = skbProgress.getMax()
* mediaPlayer.getCurrentPosition() / mediaPlayer.getDuration();
Log.e(currentProgress + "% play", bufferingProgress + "% buffer");
}
}
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();
}
I don't find any solution to show a loading animation (or just an imageView) before a VideoView start to play : during the video is buffering.
Does anyone have a clue ?
Thanks.
Or you can just use Progress Dialog;
public class VideoEkrani extends Activity {
VideoView ekran;
String kaynakYolu = "http://commonsware.com/misc/test2.3gp";
ProgressDialog progDailog;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.sayfa_video_goruntule);
ekran = (VideoView) findViewById(R.id.videoViewESPU);
ekran.setMediaController(new MediaController(this));
ekran.setVideoURI(Uri.parse(kaynakYolu));
ekran.requestFocus();
ekran.start();
progDailog = ProgressDialog.show(this, "Please wait ...", "Retrieving data ...", true);
ekran.setOnPreparedListener(new OnPreparedListener() {
public void onPrepared(MediaPlayer mp) {
// TODO Auto-generated method stub
progDailog.dismiss();
}
});
}
}
Finally I found the method setOnPreparedListener() on VideoView.
I added a ProgressBar inside my layout, and hide it with the OnPreparedListener
You cant detect the video buffer using OnBufferingUpdateListener on MediaPlayer. If you use VideView set that listener inside OnPreparedListener.
videoview.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
public void onPrepared(MediaPlayer mp) {
mp.setOnBufferingUpdateListener(new MediaPlayer.OnBufferingUpdateListener() {
#Override
public void onBufferingUpdate(MediaPlayer mp, int percent) {
if(percent == 100){
//video have completed buffering
}
}
});
}
});