I want to hide ImageView Thumbnail after Remote Video ready to play, means when onPrepared execute but imageView.setVisibility(View.GONE) doesn't works at all.
I have seen this answers one, two and I think cause is SurfaceView or VideoView. as per answers
I have tried using both MediaPlayer and VideoView
code using MediaPlayer and SurfaceView
mMediaPlayer = new MediaPlayer();
holder.surfaceView.setDrawingCacheEnabled(true);
try {
mMediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
mMediaPlayer.setDataSource(mContext, Uri.parse(video_url));
mMediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
#Override
public void onPrepared(MediaPlayer mp) {
mp.start();
((Activity)mContext).runOnUiThread(new Runnable() {
#Override
public void run() {
holder.imgThumbnail.getParent().requestTransparentRegion(holder.imgThumbnail);
holder.imgThumbnail.setVisibility(View.GONE);
holder.imgThumbnail.getParent().requestTransparentRegion(holder.imgThumbnail);
}
});
Toast.makeText(mContext,"onPrepared",Toast.LENGTH_LONG).show();
}
});
} catch (IllegalArgumentException | SecurityException | IllegalStateException | IOException e) {
e.printStackTrace();
}
mMediaPlayer.prepareAsync();
XML of SurfaceView
<RelativeLayout
android:layout_width="410dp"
android:id="#+id/v_view"
android:visibility="visible"
android:layout_height="307.50dp">
<SurfaceView
android:id="#+id/video_view"
android:layout_width="410dp"
android:layout_height="307.50dp"
/>
<ImageView
android:id="#+id/imgThumbnail"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:visibility="visible"
android:scaleType="centerCrop" />
</RelativeLayout>
code using VideoView
videoView.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
#Override
public void onPrepared(MediaPlayer mediaPlayer) {
Log.i(TAG,"onPrepared");
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
videoView.start();
Toast.makeText(mContext,"start video",Toast.LENGTH_LONG).show();
((Activity)mContext).runOnUiThread(new Runnable() {
#Override
public void run() {
holder.imgThumbnail.getParent().
requestTransparentRegion(holder.imgThumbnail);
holder.imgThumbnail.setVisibility(View.GONE);
holder.imgThumbnail.getParent().
requestTransparentRegion(holder.imgThumbnail);
}
});
}
},100);
}
});
XML of VideoView
<RelativeLayout
android:layout_width="410dp"
android:id="#+id/v_view"
android:visibility="visible"
android:layout_height="307.50dp">
<VideoView
android:id="#+id/video_view"
android:layout_width="410dp"
android:layout_height="307.50dp"
/>
<ImageView
android:id="#+id/imgThumbnail"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:visibility="gone"
android:background="#color/back_orange"
android:scaleType="centerCrop" />
</RelativeLayout>
Please help! I'm stuck
Related
I have RecyclerView inside a Fragment. The RecyclerView's every item has a ProgressBar. In the itemView's onClick listener a MediaPlayer plays a sound. I want to indicate the MediaPlayer's progress with a ProgressBar, which is inside the same ViewHolder. How can I achieve this?
I have tried to work in the Activity's runOnUiThread, but the ProgressBar stops in the beginning.
#Override
public void onClick(View view) {
if(mMediaPlayer.isPlaying())
{
mMediaPlayer.stop();
}
try {
mMediaPlayer.reset();
mMediaPlayer.setDataSource(mContext,mSound.getSoundUri());
mMediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
mMediaPlayer.prepare();
} catch (IOException e) {
e.printStackTrace();
}
mMediaPlayer.start();
mMediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
#Override
public void onPrepared(MediaPlayer mediaPlayer) {
final int duration = mMediaPlayer.getDuration();
final int amoungToupdate = duration / 100;
Timer mTimer = new Timer();
mTimer.schedule(new TimerTask() {
#Override
public void run() {
((MainActivity)mContext).runOnUiThread(new Runnable() {
#Override
public void run() {
if (!(amoungToupdate * mProgressBar.getProgress() >= duration)) {
int p = mProgressBar.getProgress();
p += 1;
mProgressBar.setProgress(p);
}
}
});
};
}, amoungToupdate);
}
});
}
Here the xml example code where imageview and progress bar are in center of its parent relative layout, you could change its view as you like.
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ProgressBar
android:id="#+id/progress"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_marginBottom="50dp"
android:layout_centerInParent="true" />
<ImageView
android:id="#+id/pic"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:contentDescription="#string/todo" />
</RelativeLayout>
When your video is done with loading you can hide the progress bar like this.
viewHolder.progressBar.setVisibility(View.GONE);
I have tried so many answer but there is no success. I want to play video after it loaded from remote URL. What is frustrating is Toast is showing but
video doesn't play.
Code
videoView.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
#Override
public void onPrepared(MediaPlayer mediaPlayer) {
Log.i(TAG,"onPrepared");
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
videoView.start();
Toast.makeText(mContext,"start video",Toast.LENGTH_LONG).show();
}
},100);
}
});
xml
<VideoView
android:id="#+id/video_view"
android:layout_width="410dp"
android:layout_height="307.50dp"
/>
I want to show a ProgressBar while MediaPlayer is preparing to play and when MediaPlayer starts playing ProgressBar disappear .
this is my code
public class MainActivity extends Activity {
String path="http://live.mp3quran.net:8006/;";
Button play_button, pause_button;
MediaPlayer player =new MediaPlayer();
private ProgressBar mProgress;
#Override public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
play_button=(Button)findViewById(R.id.play_button);
pause_button=(Button)findViewById(R.id.pause_button);
mProgress=(ProgressBar)findViewById(R.id.progressBar);
mProgress.setVisibility(View.INVISIBLE);
play_button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
mProgress.setVisibility(View.VISIBLE);
try {
player.setDataSource(path);
player.prepare();
player.start();
} catch (IOException e) {
e.printStackTrace();
}
finally {
if(mProgress.getVisibility()==View.VISIBLE)
mProgress.setVisibility(View.INVISIBLE);
}
}
});
pause_button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
player.stop();
player.reset();
}
});
}
}
my XML file Code
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin">
<Button android:id="#+id/play_button"
android:layout_width="120px"
android:layout_height="60px"
android:layout_marginTop="60px"
android:gravity="center"
android:text="Play" />
<Button android:id="#+id/pause_button"
android:layout_width="120px"
android:layout_height="60px"
android:layout_alignParentRight="true"
android:layout_marginTop="60px"
android:text="Pause" />
<ProgressBar
style="?android:attr/progressBarStyleLarge"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/progressBar"
android:layout_alignBottom="#+id/text_shown"
android:layout_toRightOf="#+id/play_button"
android:layout_toLeftOf="#+id/pause_button"
android:max="100"
android:progress="20"
android:layout_toStartOf="#+id/pause_button" />
</RelativeLayout>
My problem is that ProgressBar doesn't appear.
I have tried this solution
How can i show a ProgressBar when mediaPlayer is prepairing to play
but it doesn't help.
You can use like this
final ProgressDialog ringProgressDialog = ProgressDialog.show(this, "Please wait ...", "Fetching your contact..", true);
ringProgressDialog.setCancelable(true);
new Thread(new Runnable() {
#Override
public void run() {
try {
// Here you should write your time consuming task...
// Let the progress ring for 10 seconds...
readContact();
// Thread.sleep(300);
} catch (Exception e) {
}
ringProgressDialog.dismiss();
}
}).start();
My layout that contains videoView
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1" >
<ImageView
android:id="#+id/imgv_image"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:contentDescription="#string/app_name"
android:scaleType="centerCrop"
android:visibility="invisible" />
<VideoView
android:id="#+id/videoview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentBottom="true"
android:layout_marginBottom="20dp"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true" />
</RelativeLayout>
And the Medicontrolleri in java code:
final MediaController mediaController = new MediaController(context);
mediaController.setAnchorView(videoView);
mediaController.setMediaPlayer(videoView);
mediaController.show(0);
videoView.setMediaController(mediaController);
videoView.setVideoURI(mediaUri);
videoView.setOnPreparedListener(new OnPreparedListener() {
#Override
public void onPrepared(MediaPlayer mp) {
mediaController.show(3000);
}
});
videoView.start();
But it doesn't work, there is screenshot:
Thank for your helping!
Try this :
public static void playVideo(String urlPath) {
VideoView mVideoview; // Added this line
mVideoView =(VideoView) findViewByid(R.yourvideoviewid);
try {
// Start the MediaController
MediaController mediacontroller = new MediaController(mContext);
mediacontroller.setAnchorView(mVideoview);
// Get the URL from String VideoURL
Uri mVideo = Uri.parse(urlPath);
mVideoview.setMediaController(mediacontroller);
mVideoview.setVideoURI(mVideo);
} catch (Exception e) {
Log.e("Error", e.getMessage());
e.printStackTrace();
}
mVideoview.requestFocus();
mVideoview.setOnPreparedListener(new OnPreparedListener() {
// Close the progress bar and play the video
public void onPrepared(MediaPlayer mp) {
mVideoview.start();
}
});
mVideoview.setOnCompletionListener(new OnCompletionListener() {
public void onCompletion(MediaPlayer mp) {
}
});
}
<VideoView
android:id="#+id/videoView"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
Java
// Get VideoView in layout xml
VideoView mVideoView = (VideoView) findViewById(R.id.videoView);
// MediaController provide controller, contains the buttons
// like "Play/Pause", "Rewind", "Fast Forward" and a progress slide
MediaController mediaController = new MediaController(this);
mediaController.setAnchorView(mVideoView);
mVideoView.setMediaController(mediaController);
mVideoView.setVideoPath("/path/to/your/videofile");
mVideoView.requestFocus();
mVideoView.setOnPreparedListener(new OnPreparedListener() {
public void onPrepared(MediaPlayer mp) {
mVideoView.start();
}
});
Add frame layout for anchor view in your xml
<FrameLayout android:id="#+id/controllerAnchor"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentStart="true"
android:layout_alignParentRight="true"
/>
and set this to your media controller
mediacontroller.setAnchorView(findViewById(R.id.controllerAnchor);
I have a VideoView and a TextView on the same activity. Here s the xml for the layout.
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/rellayout"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<VideoView
android:id="#+id/videoview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:padding="5dp" />
<TextView
android:id="#+id/numberTicker"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="#color/purple"
android:textStyle="bold"
android:gravity="top|right"
android:textSize="35dp"
android:maxEms="3"/>
</RelativeLayout>
As soon as the video start playing I wish to display a number on the numberTicker text view. Mind you the numbers are likely to change 4 times per second. Anyway, the primary issue I have is to identify that the video has started playing and to keep updating the TextView.
My attempt :
vid = (VideoView) findViewById(R.id.videoview);
ratings = (TextView) findViewById(R.id.ratingTicker);
vid.setOnPreparedListener(new OnPreparedListener() {
#Override
public void onPrepared(MediaPlayer mp) {
vid.start();
vid.postDelayed(onEveryQuarterSecond,250);
}
});
...
...
private Runnable onEveryQuarterSecond = new Runnable() {
Random r = new Random();
int uVal, aVal;
#Override
public void run() {
ratings.setText(Integer.toString(r.nextInt()));
}
};
The code Runnable would only run once, and never get updated.
Am I missing something?
Thanks for the help!
You get a MediaPlayer object in onPrepared(). You could use that like this:
vid.setOnPreparedListener(new OnPreparedListener() {
#Override
public void onPrepared(MediaPlayer mp) {
this.mediaPlayer = mp; //have mediaPlayer be a class variable
vid.start();
vid.postDelayed(onEveryQuarterSecond,250);
}
});
private Runnable onEveryQuarterSecond = new Runnable() {
Random r = new Random();
int uVal, aVal;
#Override
public void run() {
while(mediaPlayer.isPlaying()) {
ratings.setText(Integer.toString(r.nextInt()));
Thread.sleep(250);
}
}
};
Consider to use a new Thread to not block the main UI Thread:
public void updateEverySecond() {
new Thread(new Runnable() {
#Override
public void run() {
while (mediaPlayer.isPlaying()) {
try {
ratings.setText(Integer.toString(r.nextInt()));
Thread.sleep(1000);
} catch (Exception e) {
e.printStackTrace();
}
}
}
}).start();
}