played video again automatically after finished - android

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();
}
});

Related

Android: Video in TextureView

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();
}
}

media player pause is not working

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.

Application taking phone_state as idle from the very starting point of application. This is not needed

private ImageButton malayalam, hindi, tamil;
private MediaPlayer mp = null;
ImageButton pause;
ImageView facebook, twitter, linkedin;
private String TAG = "debug";
boolean playVideo = false, pauseVideo = true, isVideoPlaying = false, streaming = true;
TextView stream, events;
AudioManager audioManager;
ProgressBar progress;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
progress = (ProgressBar)findViewById(R.id.progress);
facebook = (ImageView)findViewById(R.id.facebook);
twitter = (ImageView)findViewById(R.id.twitter);
linkedin = (ImageView)findViewById(R.id.linkedin);
pause = (ImageButton)findViewById(R.id.pause);
pause.setVisibility(View.GONE);
stream = (TextView)findViewById(R.id.textview2);
events = (TextView)findViewById(R.id.events);
playFm();
;
audioManager = (AudioManager)getSystemService(Context.AUDIO_SERVICE);
int maxVolume = audioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
int curVolume = audioManager.getStreamVolume(AudioManager.STREAM_MUSIC);
SeekBar volControl = (SeekBar)findViewById(R.id.seekbar);
volControl.setMax(maxVolume);
volControl.setProgress(curVolume);
volControl.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
#Override
public void onProgressChanged(SeekBar arg0, int arg1, boolean arg2) {
// TODO Auto-generated method stub
audioManager.setStreamVolume(AudioManager.STREAM_MUSIC, arg1, 0);
}
#Override
public void onStartTrackingTouch(SeekBar arg0) {
// TODO Auto-generated method stub
}
#Override
public void onStopTrackingTouch(SeekBar arg0) {
// TODO Auto-generated method stub
}
});
pause.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
if (playVideo) {
playVideo = false;
pauseVideo = true;
isVideoPlaying = true;
pause.setVisibility(View.VISIBLE);
onPrepared(mp);
} else {
pauseFm();
playVideo = true;
pauseVideo = false;
isVideoPlaying = false;
pause.setVisibility(View.VISIBLE);
Toast.makeText(MyTuneIndia.this, "paused", Toast.LENGTH_SHORT).show();
pause.setBackgroundResource(R.drawable.play);
}
}
});
PhoneStateListener phoneStateListener = new PhoneStateListener() {
#Override
public void onCallStateChanged(int state, String incomingNumber) {
if (state == TelephonyManager.CALL_STATE_RINGING) {
// do all necessary action to pause the audio
if (mp != null) {// check mp
if (mp.isPlaying()) {
Toast.makeText(MyTuneIndia.this, "Incoming call..!!.",
Toast.LENGTH_SHORT).show();
pauseFm();
playVideo = true;
pauseVideo = false;
isVideoPlaying = false;
pause.setVisibility(View.VISIBLE);
Toast.makeText(MyTuneIndia.this, "paused", Toast.LENGTH_SHORT).show();
pause.setBackgroundResource(R.drawable.play);
}
}
// Incoming call: Pause music
} else if (state == TelephonyManager.CALL_STATE_IDLE) {
Toast.makeText(MyTuneIndia.this, "Playing Again..!!", Toast.LENGTH_SHORT)
.show();
playVideo = false;
pauseVideo = true;
isVideoPlaying = true;
pause.setVisibility(View.VISIBLE);
onPrepared(mp);
} else if (state == TelephonyManager.CALL_STATE_OFFHOOK) {
Toast.makeText(MyTuneIndia.this, "Outgoing Call..!!", Toast.LENGTH_SHORT)
.show();
pauseFm();
playVideo = true;
pauseVideo = false;
isVideoPlaying = false;
pause.setVisibility(View.VISIBLE);
Toast.makeText(MyTuneIndia.this, "paused", Toast.LENGTH_SHORT).show();
pause.setBackgroundResource(R.drawable.play);
}
super.onCallStateChanged(state, incomingNumber);
}
};// end PhoneStateListener
TelephonyManager mgr = (TelephonyManager)getSystemService(TELEPHONY_SERVICE);
if (mgr != null) {
mgr.listen(phoneStateListener, PhoneStateListener.LISTEN_CALL_STATE);
}
}
public void playFm() {
Uri myUri = Uri.parse("http://xxx.mp3");
try {
if (mp == null) {
this.mp = new MediaPlayer();
} else {
mp.stop();
mp.reset();
}
mp.setDataSource(this, myUri); // Go to Initialized state
mp.setAudioStreamType(AudioManager.STREAM_MUSIC);
mp.setOnPreparedListener(this);
mp.setOnBufferingUpdateListener(this);
mp.setOnErrorListener(this);
mp.prepareAsync();
Log.d(TAG, "LoadClip Done");
} catch (Throwable t) {
Log.d(TAG, t.toString());
}
}
public void pauseFm() {
mp.pause();
}
#Override
public void onBufferingUpdate(MediaPlayer mp, int percent) {
// TODO Auto-generated method stub
}
#Override
public boolean onError(MediaPlayer arg0, int arg1, int arg2) {
// TODO Auto-generated method stub
return false;
}
#Override
public void onPrepared(MediaPlayer arg0) {
// TODO Auto-generated method stub
pause.setBackgroundResource(R.drawable.pause);
pause.setVisibility(View.VISIBLE);
stream.setVisibility(View.GONE);
progress.setVisibility(View.GONE);
streaming = false;
isVideoPlaying = true;
if (isVideoPlaying)
mp.start();
}
#Override
public void onCompletion(MediaPlayer mp) {
// TODO Auto-generated method stub
}
public boolean onKey(View v, int keyCode, KeyEvent event) {
// TODO Auto-generated method stub
return false;
}
#Override
public void onBackPressed() {
if (isVideoPlaying) {
mp.stop();
finish();
} else if (streaming) {
Toast.makeText(MyTuneIndia.this, "Streaming stopped..", Toast.LENGTH_SHORT).show();
mp.reset();
mp.release();
finish();
} else {
finish();
}
}
This is my code for an online audio player which streams audio and plays. Now the problem is that it should run the code when the app is just launched. But it is doing so. When I launch the application from launcher, it will say "playing again" which is not in my main code but this PHONE_STATE_IDLE part of the code. So can any one please suggest me what to do so that for the first time app runs as designed and this PHONE_STATE_IDLE part of code is implemented only when any call is received or made while app is already running.
Thanx a lot in advance..
Hope u all got wat i meant..

Why SurfaceView doesn't display 3gp video in Android

I am using the following sample code to play a 3gp video. on android 2.3 .. but only the audio is playing, but the video doesn't appear . what is the problem ?
package pit.opensource.video;
import android.app.Activity;
import android.content.Context;
import android.graphics.PixelFormat;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.widget.Button;
import android.widget.SeekBar;
public class L2VideoExampleActivity extends Activity implements SurfaceHolder.Callback{
MediaPlayer mediaPlayer;
SurfaceView surfaceView;
SurfaceHolder surfaceHolder;
boolean pausing = false;
AudioManager audioManager;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
audioManager = (AudioManager)getSystemService(Context.AUDIO_SERVICE);
int maxVolume = audioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
int curVolume = audioManager.getStreamVolume(AudioManager.STREAM_MUSIC);
SeekBar volControl = (SeekBar)findViewById(R.id.volbar);
volControl.setMax(maxVolume);
volControl.setProgress(curVolume);
volControl.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
#Override
public void onStopTrackingTouch(SeekBar arg0) {
// TODO Auto-generated method stub
}
#Override
public void onStartTrackingTouch(SeekBar arg0) {
// TODO Auto-generated method stub
}
#Override
public void onProgressChanged(SeekBar arg0, int arg1, boolean arg2) {
// TODO Auto-generated method stub
audioManager.setStreamVolume(AudioManager.STREAM_MUSIC, arg1, 0);
}
});
Button buttonPlayVideo = (Button)findViewById(R.id.playvideoplayer);
Button buttonPauseVideo = (Button)findViewById(R.id.pausevideoplayer);
getWindow().setFormat(PixelFormat.UNKNOWN);
surfaceView = (SurfaceView)findViewById(R.id.surfaceview);
surfaceHolder = surfaceView.getHolder();
surfaceHolder.addCallback(this);
surfaceHolder.setFixedSize(176, 144);
surfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
mediaPlayer = new MediaPlayer();
buttonPlayVideo.setOnClickListener(new Button.OnClickListener(){
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
pausing = false;
if(mediaPlayer.isPlaying()){
mediaPlayer.reset();
}
mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
mediaPlayer.setDisplay(surfaceHolder);
try {
// mediaPlayer.setDataSource(stringPath);
// mediaPlayer.prepare();
mediaPlayer = MediaPlayer.create(getApplicationContext(), R.raw.samplevideo);
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
mediaPlayer.start();
}});
buttonPauseVideo.setOnClickListener(new Button.OnClickListener(){
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
if(pausing){
pausing = false;
mediaPlayer.start();
}
else{
pausing = true;
mediaPlayer.pause();
}
}});
}
#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
}
}
I think you must play video in surfaceview.you now play video in oncreate and may be surfaceview not created yet.
use something like this in surfaceCreated
#Override
public void surfaceCreated(SurfaceHolder holder)
{
if(ArrayManager.array_for_playing != null)
if(SDcard.icurrentIndex != -1)
playVideo(SDcard.icurrentIndex);
}
and this is playVideo method
private void playVideo(Integer Media)
{
try {
mPlayer.reset();
mPlayer.setDataSource(ArrayManager.array_for_playing.get(SDcard.icurrentIndex).getPath());
mPlayer.setDisplay(holder);
songProgressBar.setProgress(0);
songProgressBar.setMax(100);
mPlayer.prepare();
mPlayer.start();
btnPlay.setBackgroundResource(R.drawable.img_btn_pause);
// Updating progress bar
updateProgressBar();
}
catch (Exception e)
{
Log.e("video player", "error: " + e.getMessage(), e);
}
}
And in IniatializeUi method
private void InitializeUI()
{
mPreview = (SurfaceView) findViewById(R.id.surface_view);
holder = mPreview.getHolder();
holder.addCallback(this);
holder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
}
and in top of activity :
private SurfaceView mPreview;
private SurfaceHolder holder;
I hope this help users.

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