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();
}
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'm making a splash screen for my app and I'm just testing out putting the primary thread to sleep instead of using a timer. My code is:
package com.example.somu.activityswitcher;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;
public class LauncherActivity extends AppCompatActivity {
public void firstActivity() {
Intent intent = new Intent(getApplicationContext(), MainActivity.class);
startActivity(intent);
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_launcher);
TextView countDown = findViewById(R.id.count);
for (int cd=3;cd>0;cd--) {
try {
Thread.sleep(1000);
countDown.setText(Integer.toString(cd));
} catch (Exception e) {
e.printStackTrace();
}
}
firstActivity();
}
}
While the MainActivity loads after 3 seconds, the splash screen (LauncherActivity) is a mere blank screen! What's going on here?!
activity_launcher.xml:
<?xml version="1.0" encoding="utf-8"?>
<android.widget.RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.somu.activityswitcher.LauncherActivity">
<ImageView
android:id="#+id/imageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:scaleType="fitCenter"
android:scaleX="0.25"
android:scaleY="0.25"
app:srcCompat="#drawable/logo" />
<TextView
android:id="#+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/count"
android:layout_centerHorizontal="true"
android:text="Switching in..." />
<TextView
android:id="#+id/count"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="70dp"
android:text="3"
android:textColor="#android:color/black"
android:textSize="50sp" />
</android.widget.RelativeLayout>
How do I fix this?!
NOTE: I'm not bothered about any way to fix this.. I want to know why exactly this method won't work, and what is the next best way without explicitly using a timer.
try this,
may be this is useful to you.
public class LauncherActivity extends AppCompatActivity {
private TextView countDown;
int cd;
public void firstActivity() {
Intent intent = new Intent(getApplicationContext(), MainActivity.class);
startActivity(intent);
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_launch);
countDown = (TextView) findViewById(R.id.count);
try {
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
for (cd = 3; cd > 0; cd--) {
countDown.setText(Integer.toString(cd));
}
firstActivity();
}
}, 3000);
} catch (Exception e) {
e.printStackTrace();
}
}
}
if you want to add countdown than put this insted of handler,
new CountDownTimer(3000, 1000) {
public void onTick(long millisUntilFinished) {
countDown.setText("seconds remaining: " + millisUntilFinished / 1000);
}
public void onFinish() {
countDown.setText("done!");
firstActivity();
}
}.start();
Try this instead of Thread.sleep(1000);
new CountDownTimer(3000, 1000) {
#Override
public void onTick(long l) {
countDown.setText(Integer.toString(l/1000));
}
#Override
public void onFinish() {
firstActivity();
finish();
}
}.start();
Try to use CountDownTimer to display timer instead of Thread or Handler as below :
onTick() run on UI thread so you can update UI in this method as you trying to show (3,2,1) on countDown TextView.
onFinish called when given timer is complete so you can write your code here after timer completed as you trying show another activity.
new CountDownTimer(3000, 1000) {
public void onTick(long millisUntilFinished) {
countDown.setText(""+ (millisUntilFinished / 1000));
}
public void onFinish() {
firstActivity();
}
}.start();
You are using Thread.sleep(1000); in the main thread which is freezing your UI.
Since the UI thread is frozen, in the meantime, the activity will fail to inflate and render, resulting a mere blank screen.
However, you can use Thread.sleep in a background thread but you can't update UI directly from a background Thread. You can use runOnUiThread to update UI from background thread if you place you countdown code inside a Thread
new Thread(new Runnable(){
public void run(){
//countdown code.
runOnUiThread(new Runnable(){
public void run(){
textView.setText(...
}
});
}
}).start()
try this code.
Intent intent = new Intent(this, WelcomeActivity.class);
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
try{
startActivity(intent);
finish();
}
catch (Exception e){
}
}
}, 1000);
Thread.sleep(1000); freezing your UI.
try this
Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tv = (TextView) findViewById(R.id.tv);
timer = new Timer();
timer.scheduleAtFixedRate(new RemindTask(), 3000, 1000); // delay*/
}
private class RemindTask extends TimerTask {
#Override
public void run() {
runOnUiThread(new Runnable() {
public void run() {
i++;
Log.e("title", "" + i);
tv.setText(i + "");
if (i == 3) {
timer.cancel();
firstActivity();
}
}
});
}
}
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
I am playing an audio (mySound) as background music. On clicking a button, the background audio should stop and a different audio (mySound11) should start playing. After this audio gets done, the background music should automatically resume from where it had left. I have tried this code, but the background audio doesn't resume after mySound11 is over. Please help. Thanks in advance.
MainActivity.java
public class MainActivity extends ActionBarActivity {
MediaPlayer mySound;
MediaPlayer mySound11;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mySound11 = MediaPlayer.create(this, R.raw.tiktik);
mySound = MediaPlayer.create(this, R.raw.jhakmaarke);
mySound.setLooping(true);
mySound.start();
}
public void playMusic (View view){
mySound.pause();
mySound11.start();
}
#Override
public void onResume() {
super.onResume();
mySound.start();
}
#Override
protected void onPause() {
super.onPause();
mySound.release();
mySound11.release();
finish();
}
activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:paddingBottom="#dimen/activity_vertical_margin" tools:context=".MainActivity">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button Dabaa be"
android:onClick="playMusic"
android:id="#+id/button"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="72dp" />
I Solved your problem with the help of Thread.
I tested it working as you wished.
int mySound11Duration;
MediaPlayer mySound;
MediaPlayer mySound11;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mySound11 = MediaPlayer.create(this, R.raw.tiktik);
mySound = MediaPlayer.create(this, R.raw.jhakmaarke);
mySound.setLooping(true);
mySound.start();
}
public void playMusic(View view) {
if (mySound.isPlaying()) { //check mysound is playing or not
mySound11Duration = mySound11.getDuration();
Thread thread=new Thread() {
#Override
public void run() {
try {
mySound.pause();
int mySoundPausePosition = mySound.getCurrentPosition();//get the time where the song is paused.
mySound11.start();
sleep(mySound11Duration);//sleep method causes the currently executing thread to sleep for specified number of time(miliseconds).
mySound.seekTo(mySoundPausePosition);
mySound.start();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
};
thread.start();
}
}
}
I need to show a second activity after the progress bar is filled. I tried the code below but it doesn't show the progress bar and just shows my second activity.
This is the code:
public class MiSuper2 extends Activity {
String strListas[] = null;
private ProgressBar mProgress;
private int mProgressStatus = 0;
private Handler mHandler = new Handler();
private StoreData stdArticulos = null;
public Cursor cursor = null;
private long fileSize = 0;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
stdArticulos = new StoreData(this);
fileSize = 0;
setContentView(R.layout.main);
stdArticulos = new StoreData(this);
cursor = stdArticulos.leerArticulos();
mProgress = (ProgressBar) findViewById(R.id.progressbar_activity);
new Thread(new Runnable() {
public void run() {
while (mProgressStatus < 100) {
mProgressStatus = doWork();
mHandler.post(new Runnable() {
public void run() {
mProgress.setProgress(mProgressStatus);
}
});
}
}
}).start();
if(cursor.moveToFirst()) {
do{
strListas[cursor.getPosition()] = cursor.getString(cursor.getPosition());
}while(cursor.moveToNext());
}
Intent intent = new Intent(MiSuper2.this, PntArticulo.class);
startActivity(intent);
}
public int doWork() {
while (fileSize <= 1000000) {
fileSize++;
return (int) fileSize;
}
return 100;
}
}
This is the main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center_horizontal"
android:orientation="vertical" >
<ImageView
android:id="#+id/imvLogo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/presentacion"
android:contentDescription="#string/logo"/>
<ProgressBar
android:id="#+id/progressbar_activity"
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="200dp"
android:layout_height="wrap_content" android:layout_marginTop="100dp"/>
</LinearLayout>
Please help
It looks like you are using the doWork() function to take up time so that you're progress bar does something. Even though you wrote a big loop, it still executes very quickly so you don't see your progress bar move. Rather, you want to simulate your Thread doing something computationally intensive by using Thread.sleep() which takes an argument that is the time to sleep in milliseconds.
Try changing your code to this:
new Thread(new Runnable() {
public void run() {
while (mProgressStatus < 100) {
try {
mProgressStatus += doWork();
} catch (InterruptedException e) {
e.printStackTrace();
}
mHandler.post(new Runnable() {
public void run() {
mProgress.setProgress(mProgressStatus);
}
});
}
runOnUiThread(new Runnable() {
#Override
public void run() {
startActivity(new Intent(MiSuper2.this, Second.class));
}
});
}
}).start();
And...
public int doWork() throws InterruptedException {
Thread.sleep(1000);
return 1;
}
This will increment your progress bar by 1% every second. And finally, the documentation on Thread.sleep(): https://developer.android.com/reference/java/lang/Thread.html#sleep(long)
EDIT: Ramz beat me to this answer, but doesn't provide an explanation of why it's the answer. Hopefully my explanation helps.
EDIT2: I think you edited your questions since I started looking at it a second time. You had some errors in your XML before, but now it is gone. Regardless, your problem is now that you need the call to startActivity() inside your worker thread. Otherwise, the UI thread does not wait for the doWork() function to return and immediately starts the other Activity when your app starts. Sorry, I should have mentioned this before. The code I posted above is updated with this change.
Please try this code SplashScreen.java
package com.cud.point;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.widget.ProgressBar;
import android.widget.TextView;
public class SplashScreen extends Activity {
ProgressBar bar;
TextView txt;
int total=0;
boolean isRunning=false;
// handler for the background updating
Handler handler=new Handler() {
#Override
public void handleMessage(Message msg) {
total=total+20;
String perc=String.valueOf(total).toString();
txt.setText(perc+"% completed");
bar.incrementProgressBy(20);
}
};
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.splash);
bar=(ProgressBar)findViewById(R.id.progress);
txt=(TextView)findViewById(R.id.txt);
Handler x = new Handler();
x.postDelayed(new SplashHandler(), 5000);
}
class SplashHandler implements Runnable
{
public void run() {
// TODO Auto-generated method stub
startActivity(new Intent(getApplication(),YourSecound Activity.class));
SplashScreen.this.finish();
}
}
public void onStart() {
super.onStart();
// reset the bar to the default value of 0
bar.setProgress(0);
// create a thread for updating the progress bar
Thread background=new Thread(new Runnable() {
public void run() {
try {
for (int i=0;i<5 && isRunning;i++) {
// wait 100ms between each update
Thread.sleep(1000);
handler.sendMessage(handler.obtainMessage());
}
}
catch (Throwable t) {
} } });
isRunning=true;
// start the background thread
background.start();
}
public void onStop() {
super.onStop();
isRunning=false;
}
}
splash.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:padding="15px" >
<TextView
android:id="#+id/textView1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Loading......" />
<TextView
android:id="#+id/txt"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/imageView1"/>
<ImageView
android:id="#+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/splash" />
<ProgressBar
android:id="#+id/progress"
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/imageView1"
android:layout_alignLeft="#+id/imageView1"
android:max="100" />
</RelativeLayout>
this is an example of my project so please make necessary change in xml file