How to play video in viewflipper - android

I have a viewflipper in which there are videos and images.Everything works fine when it run for first time.After the last child is shown and the view are repeated,in that case videos are not playing and a black screen remain all the time.
Thanks in advance.
My code
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
requestWindowFeature(Window.FEATURE_NO_TITLE);
super.onCreate(savedInstanceState);
setContentView(R.layout.kiosk_mode);
mSelectedPressoId=PreferenceConnector.readString(KioskModeActivity.this, PreferenceConnector.SELECTED_PRESSO_ID, "");
bindControl();
fetchSlidesFromDb();
}
public void bindControl(){
mFlipper=(ViewFlipper)findViewById(R.id.viewflipper);
mParentView=(FrameLayout)findViewById(R.id.parent_background);
mKioskModeIcon=(ImageView)findViewById(R.id.kiosk_mode_icon);
mExitBar=(LinearLayout)findViewById(R.id.exit_bar);
detector = new GestureDetector(new SwipeGestureDetector());
mCounter=new MyCounter(10000, 1000);
mActivePointers = new SparseArray<PointF>();
mKioskModeIcon.setOnClickListener(this);
mHomeIcon=(ImageView)findViewById(R.id.homeIcon);
mHomeIcon.setOnClickListener(this);
mCommonMethod=new CommonMethod(getApplicationContext());
position=new ArrayList<String>();
mTransitionTime=PreferenceConnector.readString(getApplicationContext(), PreferenceConnector.TRANSITION_TIME, "");
Log.v("transition time from prefrence isss",mTransitionTime+">>>");
if(mTransitionTime.equalsIgnoreCase("")){
mFlipper.setFlipInterval(10000);
}else{
int mTransitionTimeInMills=Integer.parseInt(mTransitionTime)*1000;
mFlipper.setFlipInterval(mTransitionTimeInMills);
}
mFlipper.setOutAnimation(AnimationUtils.loadAnimation(
getBaseContext(), R.anim.push_left_out));
mFlipper.setInAnimation(AnimationUtils.loadAnimation(
getBaseContext(), R.anim.push_left_in));
mFlipper.getInAnimation().setAnimationListener(new Animation.AnimationListener() {
public void onAnimationStart(Animation animation) {}
public void onAnimationRepeat(Animation animation) {
Log.d("new view is ",">>>>>>>>>>>>>>>");
}
public void onAnimationEnd(Animation animation) {
mVideoPosition=mFlipper.getDisplayedChild();
Log.d("new view is flipped",mVideoPosition+">>>>>>>>>>>>>>>"+mPressoPathList.get(mVideoPosition));
if(mSlideTypeList.get(mVideoPosition).equalsIgnoreCase("video")){
Log.d("onside iffffff",myVideoView+">>>>>");
myVideoView.start();
}
}
});
mFlipper.startFlipping();
mFlipper.setOnTouchListener(new OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
detector.onTouchEvent(event);
return true;
}
});
}
public void fetchSlidesFromDb(){
PresentationDatabase databaseClass = new PresentationDatabase(getApplicationContext(), DatabaseUtil.PresenationTable.DATABASE_NAME, null, DatabaseUtil.PresenationTable.DATABASE_VERSION);
SQLiteDatabase db = databaseClass.getReadableDatabase();
mPressoPathList=new ArrayList<String>();
mSlideTypeList=new ArrayList<String>();
String where = DatabaseUtil.PresenationTable.PRESENTATION_ID + "="
+"'"+mSelectedPressoId+"'";
Cursor mCursor=db.query(DatabaseUtil.PresenationTable.TABLE_NAME, null, where,
null, null, null, null);
Log.e("cursor size isssssss",mCursor.getCount()+">>");
if(mCursor.getCount()>0){
mCursor.moveToFirst();
do{
String slide_content_path=mCursor.getString(mCursor.getColumnIndex(DatabaseUtil.PresenationTable.CONTENT_LOCAL_PATH));
String mSlideType=mCursor.getString(mCursor.getColumnIndex(DatabaseUtil.PresenationTable.SLIDE_TYPE));
if(!mSlideType.equalsIgnoreCase("pdf")){
mPressoPathList.add(slide_content_path);
mSlideTypeList.add(mSlideType);
}
}while(mCursor.moveToNext());
}
Log.d("slide path issssssss",mPressoPathList+"");
setFlipperImage(mSlideTypeList, mPressoPathList);
mCursor.close();
db.close();
}
//create view to be put in view flipper
private void setFlipperImage(ArrayList<String>mSlideType,ArrayList<String>mSlidePath) {
int size=mSlideType.size();
for(int i=0;i<size;i++){
if(mSlideType.get(i).equalsIgnoreCase("image")){
ImageView image = new ImageView(getApplicationContext());
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);
image.setLayoutParams(layoutParams);
image.setScaleType(ScaleType.FIT_XY);
//image.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
BitmapFactory.Options options=new BitmapFactory.Options();
options.inSampleSize = 2;
bmp = BitmapFactory.decodeFile(mSlidePath.get(i),options);
image.setImageBitmap(bmp);
image.setTag(i);
position.add(i+"");
mFlipper.addView(image);
}else{
myVideoView = new VideoView(this);
myVideoView.setVideoPath(mSlidePath.get(i));
Log.e("my video path isssss",mSlidePath.get(i));
myVideoView.requestFocus();
myVideoView.setLayoutParams(new LayoutParams(
LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
// l.setBackgroundColor(Color.BLACK);
myVideoView.setKeepScreenOn(true);
mFlipper.addView(myVideoView);
myVideoView.start();
myVideoView.seekTo(500);
myVideoView.setOnPreparedListener(new OnPreparedListener() {
#Override
public void onPrepared(MediaPlayer mp) {
mFlipper.stopFlipping();
}
});
myVideoView.setOnCompletionListener(new OnCompletionListener() {
#Override
public void onCompletion(MediaPlayer mp) {
// TODO Auto-generated method stub
Log.d("inside oncomplete listner",">>>>>");
myVideoView.stopPlayback();
mVideoPosition=mVideoPosition+1;
mFlipper.setDisplayedChild(mVideoPosition);
mFlipper.startFlipping();
myVideoView.seekTo(500);
}
});
}
//
}
}
#Override
public void onBackPressed() {
// TODO Auto-generated method stub
//do nothing*
}

I solved this issue by adding
myVideoView.start();
into the Event Listener
myVideoView.setOnPreparedListener(new OnPreparedListener() {
#Override
public void onPrepared(MediaPlayer mp) {
mFlipper.stopFlipping();
myVideoView.start();
}
});

Related

Seekbar for mp3 from sd card

This code is to read a mp3 from raw folder,But I want to change my mp3 file to another file from sd card
its new code, music played but seekbar does not work properly, when i toch it seekbar come back to frist position
public class MainActivity extends Activity implements OnClickListener {
SeekBar seek_bar;
Button play_button, pause_button;
MediaPlayer mediaPlayer;
TextView text_shown;
Handler seekHandler = new Handler();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
getInit();
seekUpdation();
}
public void getInit() {
seek_bar = (SeekBar) findViewById(R.id.seek_bar);
play_button = (Button) findViewById(R.id.play_button);
pause_button = (Button) findViewById(R.id.pause_button);
text_shown = (TextView) findViewById(R.id.text_shown);
play_button.setOnClickListener(this);
pause_button.setOnClickListener(this);
String filePath = Environment.getExternalStorageDirectory() + "/Android/music.mp3";
mediaPlayer = new MediaPlayer();
try {
mediaPlayer.setDataSource(filePath);
} 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();
}
mediaPlayer.setOnPreparedListener(new OnPreparedListener() {
#Override
public void onPrepared(MediaPlayer mediaPlayer) {
}
});
mediaPlayer.prepareAsync();
seek_bar.setMax(mediaPlayer.getDuration());
}
Runnable run = new Runnable() {
#Override
public void run() {
seekUpdation();
}
};
public void seekUpdation() {
seek_bar.setProgress(mediaPlayer.getCurrentPosition());
seekHandler.postDelayed(run, 1000);
seek_bar.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
#Override
public void onStopTrackingTouch(SeekBar seek_bar) {
// TODO Auto-generated method stub
}
#Override
public void onStartTrackingTouch(SeekBar seek_bar) {
// TODO Auto-generated method stub
}
#Override
public void onProgressChanged(SeekBar seek_bar, int progress, boolean fromUser) {
if(fromUser){
mediaPlayer.seekTo(progress);
seek_bar.setProgress(progress);
}
}
});
}
#Override
public void onClick(View view) {
switch (view.getId()) {
case R.id.play_button:
text_shown.setText("Playing...");
mediaPlayer.start();
break;
case R.id.pause_button:
mediaPlayer.pause();
text_shown.setText("Paused...");
}
}
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
if(fromUser){
mediaPlayer.seekTo(progress);
seekBar.setProgress(progress);
}
}
#Override
public void onBackPressed() {
mediaPlayer.stop();
finish();
}
}
Just move this line
seek_bar.setMax(mediaPlayer.getDuration());
to seekUpdation() after
seekHandler.postDelayed(run, 1000);
Replace
mediaPlayer = MediaPlayer.create(this, R.raw.my_file);
with:
String filePath = Environment.getExternalStorageDirectory() + "/randomDirectory/yourFile.mp3";
mediaPlayer = new MediaPlayer();
mediaPlayer.setDataSource(filePath);
mediaPlayer.setOnPreparedListener(new OnPreparedListener() {
#Override
public void onPrepared(MediaPlayer mp) {
mediaPlayer.start();
}
});
mediaPlayer.prepareAsync();

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

How can I make a view invisible after 3 seconds?

I am using a handler in the adapter file to make the view invisible after 3 sconds, but it does not update the view. Where is my problem and how can I fix it?
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
try {
Bundle bun = getIntent().getExtras();
if (!bun.isEmpty()) {
selecte_pos = bun.getInt("pos");
}
} catch (Exception e) {
}
Display display = getWindowManager().getDefaultDisplay();
width = display.getWidth();
height = display.getHeight();
System.out.println("full image activity");
ar = getIntent().getStringArrayListExtra("PhotoList");
System.out.println("full list size: " + ar.size());
imageLoader = new ImageLoader(this);
setContentView(R.layout.full_image_view_layout);
mPager = (ViewPager) findViewById(R.id.full_image_view_pager);
adapter = new MyPagerAdapter();
mPager.setAdapter(adapter);
mPager.setCurrentItem(selecte_pos);
}
class MyPagerAdapter extends PagerAdapter {
public int getCount() {
return ar.size();
}
Handler pagerHandler=new Handler(){
public void handleMessage(Message msg){
if(msg.what==1){
//if (istimer == true) {
System.out.println("page handler");
System.out.println("");
Toast.makeText(getApplicationContext(), "hi", 1).show();
shareLayout.setVisibility(View.INVISIBLE);
adapter.notifyDataSetChanged();
//}
}
}
};
#Override
public Object instantiateItem(ViewGroup container, final int position) {
System.out.println("view page clciking");
LayoutInflater layoutInflater = (LayoutInflater) container
.getContext().getSystemService(LAYOUT_INFLATER_SERVICE);
View v = layoutInflater.inflate(R.layout.imageview, null);
try {
final ImageView imageView = (ImageView) v
.findViewById(R.id.full_pager_adapter_iv);
shareLayout = (LinearLayout) v.findViewById(R.id.buttonslayout);
shareLayout.setVisibility(View.VISIBLE);
imageLoader.DisplayImage(ar.get(position), imageView);
if (first) {
System.out.println("fisr condition");
first = false;
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
pagerHandler.sendEmptyMessage(1);
//adapter.notifyDataSetChanged();
}
}, SPLASH_DISPLAY_LENGHT);
}
/**if(istimer1== true)
{
System.out.println("else part");
istimer1 = false;
shareLayout.setVisibility(View.VISIBLE);
adapter.notifyDataSetChanged();
}**/
imageView.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
System.out.println("img clicking");
if (shareLayout.getVisibility() == View.INVISIBLE) {
System.out.println("invisible");
istimer = false;
istimer1 = true;
shareLayout.setVisibility(View.VISIBLE);
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
//istimer = true;
//istimer1 = false;
pagerHandler.sendEmptyMessage(1);
//adapter.notifyDataSetChanged();
}
}, SPLASH_DISPLAY_LENGHT);
adapter.notifyDataSetChanged();
}
if (shareLayout.getVisibility() == View.VISIBLE) {
System.out.println("visible");
istimer = true;
istimer1 = false;
shareLayout.setVisibility(View.INVISIBLE);
adapter.notifyDataSetChanged();
}
}
});
LinearLayout btnshare = (LinearLayout) v
.findViewById(R.id.btnshare);
btnshare.setTag(position);
btnshare.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
int pos = (Integer) v.getTag();
String s = ar.get(pos);
// String s = bean.getImagePath();
File file = new File(s);
Intent sendMailIntent = new Intent(Intent.ACTION_SEND);
sendMailIntent.setType("image/jpeg");
sendMailIntent.putExtra(
android.content.Intent.EXTRA_TEXT, "");
sendMailIntent.putExtra(Intent.EXTRA_STREAM,
Uri.fromFile(file));
/* sendMailIntent.setType("image/*"); */
startActivity(Intent.createChooser(sendMailIntent, ""));
}
});
} catch (Exception e) {
}
((ViewPager) container).addView(v, 0);
return v;
}
#Override
public void destroyItem(ViewGroup collection, int position, Object view) {
((ViewPager) collection).removeView((View) view);
}
#Override
public boolean isViewFromObject(View view, Object object) {
return view == object;
}
#Override
public int getItemPosition(Object object) {
// TODO Auto-generated method stub
return POSITION_NONE;
}
}
#Override
public void onBackPressed() {
// TODO Auto-generated method stub
super.onBackPressed();
finish();
}
private Handler mTimerHandler = new Handler();
private void stopTimer() {
if (mTimer1 != null) {
mTimer1.cancel();
mTimer1.purge();
}
}
private void startTimer() {
mTimer1 = new Timer();
mTt1 = new TimerTask() {
public void run() {
mTimerHandler.post(new Runnable() {
public void run() {
System.out.println("timesatrs");
// TODO
if (shareLayout.getVisibility() == View.VISIBLE) {
System.out.println("time completed");
shareLayout.setVisibility(View.INVISIBLE);
}
}
});
}
};
mTimer1.schedule(mTt1, 6000);
}
}
getWindow().getDecorView().getRootView().postDelayed(new Runnable() {
#Override
public void run() {
// TODO Auto-generated method stub
yourView.setVisibility(View.VISIBLE);
}
}, 5000);
// Call this when u initalise view in onCreate
You don't need to create a new handler every time, when you need to send message to handler which you already created. You can send delayed message like this pagerHandler.sendEmptyMessageDelayed(1, SPLASH_DISPLAY_LENGHT);
And are you sure, you are trying to update current shareLayout ?
Add the below coding in your class file.
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
view.setVisibility(View.GONE); }
}, 3000);
This will helps you in achieving what you want.

Android - after animation finished video is not playing

I'm new to android. I'm trying to play video after animation , but before animation video is playing, after animation video is not playing, i have used animationlistener, it's also not working for me.
public class MainActivity extends Activity{
ImageView image;
VideoView video;
ImageButton play;
RelativeLayout videolay;
Animation LR;
ZoomView zoomview;
String uriPath = null;
static boolean playpause = false;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
image = (ImageView) findViewById(R.id.image);
video = (VideoView) findViewById(R.id.video);
play = (ImageButton) findViewById(R.id.play);
videolay = (RelativeLayout) findViewById(R.id.videolay);
zoomview = (ZoomView) findViewById(R.id.zoomlay);
LR = AnimationUtils.loadAnimation(this, R.anim.move);
videolay.startAnimation(LR);
LR.setAnimationListener(new AnimationListener() {
#Override
public void onAnimationStart(Animation animation) {
// TODO Auto-generated method stub
}
#Override
public void onAnimationRepeat(Animation animation) {
// TODO Auto-generated method stub
}
#Override
public void onAnimationEnd(Animation animation) {
uriPath = "android.resource://" + MainActivity.this.getPackageName() + "/" + "raw/video";
Log.v("uripath",""+uriPath);
MediaController ctrl = new MediaController(MainActivity.this);
ctrl.setVisibility(View.GONE);
video.setMediaController(ctrl);
video.setVideoURI(Uri.parse(uriPath));
}
});
video.setOnTouchListener(new OnTouchListener()
{
#Override
public boolean onTouch(View v, MotionEvent event)
{
video.pause();
playpause = false;
play.setVisibility(View.VISIBLE);
return false;
}
});
play.setOnClickListener(new OnClickListener()
{
#Override
public void onClick(View v)
{
Log.v("buttonclicked","");
if(playpause == false)
{
video.start();
Log.v("videostarted","");
playpause = true;
play.setVisibility(View.GONE);
}
}
});
}
#Override
protected void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
}
#Override
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
Please help to fix this.

How to show seekbar along with the custom mediaplayer

i have build a custom media player and i want to show the seekbar also.
The seekbar is showing for now but is not functional i.e. it is not progressing when the song is played. Let me know where am i wrong . Following is the code
public class myPlayer extends Activity implements Runnable,SeekBar.OnSeekBarChangeListener {
MediaPlayer mPlayer;
Button play,stop;
SeekBar seekbar;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
seekbar = (SeekBar)findViewById(R.id.seekbar);
mPlayer = MediaPlayer.create(this,R.raw.sajnave);
play=(Button)findViewById(R.id.play);
stop=(Button)findViewById(R.id.stop);
Toast.makeText(myPlayer.this, ""+mPlayer.getDuration(), Toast.LENGTH_SHORT).show();
play.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
mPlayer.start();
}
});
stop.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Toast.makeText(myPlayer.this, ""+mPlayer.getCurrentPosition(), Toast.LENGTH_SHORT).show();
mPlayer.stop();
}
});
}
#Override
public void run() {
// TODO Auto-generated method stub
int currentPosition = 0;
int total = mPlayer.getDuration();
seekbar.setMax(total);
while (mPlayer != null && currentPosition < total) {
try {
Thread.sleep(1000);
currentPosition = mPlayer.getCurrentPosition();
} catch (InterruptedException e) {
return;
} catch (Exception e) {
return;
}
seekbar.setProgress(currentPosition);
}
}
#Override
public void onProgressChanged(SeekBar arg0, int arg1, boolean arg2) {
// TODO Auto-generated method stub
mPlayer.seekTo(arg1);
}
#Override
public void onStartTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub
}
#Override
public void onStopTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub
}
}
Call updatePosition() method after mediplayer starts..It works for me.
private final Handler handler = new Handler();
private final Runnable updatePositionRunnable = new Runnable() {
public void run() {
updatePosition();
}
};
private void updatePosition() {
handler.removeCallbacks(updatePositionRunnable);
if (player != null) seekbar.setProgress(player.getCurrentPosition());
handler.postDelaye(updatePositionRunnable, 500);
}
#Override
public void onProgressChanged(SeekBar arg0, int arg1, boolean arg2) {
// TODO Auto-generated method stub
player.seekTo(arg1);
}
#Override
public void onStartTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub
}
#Override
public void onStopTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub
}
SurfaceView mPreview=(SurfaceView)findViewById(R.id.surface);
mPreview.postDelayed(runnable,200);
Runnable runnable = new Runnable() {
#Override
public void run() {
if (mMediaPlayer != null) {
if (isPlaying) {
// converting current position into seconds so dividing by
// 1000
try {
currentPosition = mMediaPlayer.getCurrentPosition();
mVideoProgress
.setProgress((int) ((currentPosition / (float) mMediaPlayer
.getDuration()) * 100f));
} catch (Exception e) {
e.printStackTrace();
}
mPreview.postDelayed(runnable, 150);
}
}
}
};

Categories

Resources