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();
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 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 have radio app stream, and when open this app, first its open splash layout like this :
splash.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ImageView
android:id="#+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:src="#drawable/loading" />
<ProgressBar
android:id="#+id/progressBar1"
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1.67" />
</LinearLayout>
and class for it like :
StartPoint.java
public class StartPoint extends Activity{
ProgressBar progressBar;
private int progressBarStatus = 0;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.splash);
progressBar = (ProgressBar)findViewById(R.id.progressBar1);
Thread timer = new Thread(){
public void run(){
try{
sleep(5000);
while(progressBarStatus < 5000){
StartPoint.this.runOnUiThread(new Runnable(){
public void run()
{
progressBar.setProgress(progressBarStatus);
progressBarStatus += 1000;
}
});
}
}catch(InterruptedException e){
e.printStackTrace();
}finally{
Intent openMainList = new Intent(StartPoint.this,
com.example.kam.MainActivity.class);
startActivity(openMainList);
}
}
};
timer.start();
}
#Override
protected void onStop(){
super.onStop();
finish();
}
}
this class when finished, its call MainActivity.java class, and its show :
MainActivity.java
public class MainActivity extends Activity implements OnClickListener {
// Define ..............................................................
private static ProgressDialog progressDialog;
public MediaPlayer mp;
boolean isPrepared = false;
Button PlayBtn, ExitBtn, PauseBtn, RefreshBtn;
String MEDIA_PATH;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
progressDialog = ProgressDialog.show(MainActivity.this, "",
"Buffering Radio...", true);
progressDialog.setCancelable(false);
// Declare
// ..............................................................
mp = new MediaPlayer();
PlayBtn = (Button) findViewById(R.id.btnPlay);
PlayBtn.setOnClickListener(this);
PauseBtn = (Button) findViewById(R.id.btnPause);
PauseBtn.setOnClickListener(this);
RefreshBtn = (Button) findViewById(R.id.btnRefresh);
RefreshBtn.setOnClickListener(this);
ExitBtn = (Button) findViewById(R.id.btnExit);
ExitBtn.setOnClickListener(this);
MEDIA_PATH = "http://radio.arabhosters.com:8015/";
// Volume Control
// ..............................................................
final AudioManager leftAm = (AudioManager)
getSystemService(Context.AUDIO_SERVICE);
int maxVolume = leftAm.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
int curVolume = leftAm.getStreamVolume(AudioManager.STREAM_MUSIC);
SeekBar volControl = (SeekBar) findViewById(R.id.volumebar);
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
leftAm.setStreamVolume(AudioManager.STREAM_MUSIC, arg1, 0);
}
});
}
#Override
public void onClick(View v) {
if (v == PlayBtn) {
startradio();
}
else if (v == PauseBtn) {
pauseradio();
}
else if (v == ExitBtn) {
exitradio();
}
else if (v == RefreshBtn) {
try {
refreshradio();
}
catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
catch (SecurityException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
public void onCompletion(MediaPlayer mediaPlayer) {
synchronized (this) {
isPrepared = false;
}
}
protected void onResume() {
super.onResume();
try {
mp.setDataSource(MEDIA_PATH);
}
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();
}
mp.setAudioStreamType(AudioManager.STREAM_MUSIC);
try {
mp.prepare();
}
catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} // also consider mp.prepareAsync().
// defult start stream when start App.
mp.start();
mp.setVolume(100, 100);
progressDialog.dismiss();
}
// method for play stream after stop it.
public void startradio() {
try {
if (mp.isPlaying()) {
return;
}
mp.start();
progressDialog.dismiss();
}
catch (IllegalStateException ex) {
ex.printStackTrace();
}
}
// method for Refresh stream.
public void refreshradio() throws IllegalArgumentException,
SecurityException, IOException {
try {
if (mp.isPlaying()) {
return;
}
mp.reset();
mp.setDataSource(MEDIA_PATH);
mp.prepare();
mp.start();
progressDialog.dismiss();
}
catch (IllegalStateException ex) {
ex.printStackTrace();
}
}
// method for pause stream.
public void pauseradio() {
mp.pause();
}
// method for check is radio paly or not stream
public boolean isPlaying() {
return mp.isPlaying();
}
// method for Looping audio if your record it - Soon :)
public boolean isLooping() {
return mp.isLooping();
}
// method for Looping audio if your record it - Soon :)
public void setLooping(boolean isLooping) {
mp.setLooping(isLooping);
}
// method for volume
public void setVolume(float volumeLeft, float volumeRight) {
mp.setVolume(volumeLeft, volumeRight);
}
// method for stop stream.
public void stopradio() {
if (mp.isPlaying()) {
mp.stop();
}
mp.release();
}
// method for exit.
public void exitradio() {
finish();
System.exit(0);
}
// method for back to main menu "Home".
public void backtomenu() {
finish();
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
and layout for it like :
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/screen"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#drawable/bg">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:orientation="vertical"
>
<LinearLayout
android:layout_weight="6"
android:orientation="vertical"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_width="fill_parent"
android:layout_height="0dip" >
</LinearLayout>
<LinearLayout
android:id="#+id/buttons"
android:layout_width="match_parent"
android:layout_height="0dip"
android:layout_alignParentLeft="true"
android:layout_weight="0.62"
android:background="#drawable/iconbgrepate"
android:gravity="center"
android:orientation="horizontal"
android:tileMode="repeat"
android:weightSum="5"
android:alpha=".75">
<SeekBar
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="#+id/volumebar"
android:max="100"
android:paddingBottom="10dip"/>
</LinearLayout>
<LinearLayout
android:id="#+id/buttons"
android:layout_width="match_parent"
android:layout_height="0dip"
android:layout_alignParentLeft="true"
android:layout_weight="0.62"
android:background="#drawable/iconbgrepate"
android:gravity="center"
android:orientation="horizontal"
android:tileMode="repeat"
android:weightSum="5" >
<Button
android:id="#+id/btnRefresh"
android:layout_width="32dp"
android:layout_height="match_parent"
android:layout_margin="5dp"
android:layout_weight="0.10"
android:background="#drawable/refreshbutton" />
<View android:layout_weight="1"
android:layout_width="0dip"
android:layout_height="0dip" />
<Button
android:id="#+id/btnPause"
android:layout_width="32dp"
android:layout_height="match_parent"
android:layout_margin="5dp"
android:layout_weight="0.05"
android:background="#drawable/pausebutton" />
<View android:layout_weight="1"
android:layout_width="0dip"
android:layout_height="0dip" />
<Button
android:id="#+id/btnPlay"
android:layout_width="32dp"
android:layout_height="match_parent"
android:layout_margin="5dp"
android:layout_weight="0.05"
android:background="#drawable/playbutton" />
<View android:layout_weight="1"
android:layout_width="0dip"
android:layout_height="0dip" />
<Button
android:id="#+id/btnExit"
android:layout_width="32dp"
android:layout_height="match_parent"
android:layout_margin="5dp"
android:layout_weight="0.12"
android:background="#drawable/exitbutton" />
</LinearLayout>
</LinearLayout>
</RelativeLayout>
why when trans from splash.xml to activity_main.xml display black background few min and display app.
Since you are changing activity, its gonna show some animation or black screen like you said. Its the default behavior. You can use a custom dialog with your splash layout on top of main activity. you can trigger the dialog to close on progress complete. Using two activities can have other effects. for instance when the user hits the back button, its gonna go back to splash screen instead of exiting the app.
try it
Thread timer = new Thread(){
public void run(){
try{
while(progressBarStatus < 5000){
StartPoint.this.runOnUiThread(new Runnable(){
public void run()
{
progressBar.setProgress(progressBarStatus);
progressBarStatus += 1000;
}
});
sleep(5000);
}
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();
}