progressbar (cyclic) show while show activity - android

i want to put progressbar while the oncreat finish...it's take about 10 second to strat activty and show View and prepare mediaplayer...
what i can use to show progressbar until It show the views and prepare media player..
If there any tutorial videos please could you send it in comment
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final String url = "url";
play = (Button)findViewById(R.id.play);
pause = (Button)findViewById(R.id.pause);
seekbar = (SeekBar)findViewById(R.id.seekBar1);
am=(AudioManager)getSystemService(Context.AUDIO_SERVICE);
int max = am.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
int min = am.getStreamVolume(AudioManager.STREAM_MUSIC);
seekbar.setMax(max);
seekbar.setProgress(min);
seekbar.setOnSeekBarChangeListener( this);
mediaPlayer = new MediaPlayer();
try {
mediaPlayer.setDataSource(url);
} 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();
}
try {
mediaPlayer.prepare();
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} // might take long! (for buffering, etc)
play.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View v) {
Toast.makeText(getBaseContext(), "text", Toast.LENGTH_LONG).show();
mediaPlayer.start();
}});
pause.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View v) {
mediaPlayer.pause();
}} );
}

after
mediaPlayer = new MediaPlayer();
add this code
ProgressDialog pd = new ProgressDialog(this);
pd.setMessage("your message");
pd.setCancelable(false);
pd.setIndeterminate(true);
//Show Dialog
pd.show();
and if you want to cancel or remove Dialog simply call pd.dismiss();

Related

Play .mp3 using MediaPlayer just one time and not loop

So I have a .mp3 in assets folder (I plan to have many more later) and I have a simple click associated to play it:
public static MediaPlayer m = new MediaPlayer();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button button= (Button) findViewById(R.id.btnDrum);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
try {
PlayMusic(v);
} catch (IOException e) {
e.printStackTrace();
}
}
});
}
private void PlayMusic(View v) throws IOException {
try {
if (m.isPlaying()) {
m.stop();
m.release();
m = new MediaPlayer();
}
AssetFileDescriptor descriptor = getAssets().openFd(Kit.KickDrum);
m.setDataSource(descriptor.getFileDescriptor(), descriptor.getStartOffset(), descriptor.getLength());
descriptor.close();
m.prepare();
m.setVolume(1f, 1f);
m.setLooping(true);
m.start();
} catch (Exception e) {
e.printStackTrace();
}
}
However, every-time I try to click the button, I the music plays in loop! I just need it to play once and stop! How can this be done?
Thanks!
you are setting it for looping using m.setLooping(true); change it to false
try this code
try {
Uri mp3 = Uri.parse("url");
mediaPlayer = new MediaPlayer();
mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
mediaPlayer.setDataSource(context, mp3);
mediaPlayer.prepare(); // might take long! (for buffering, etc)
mediaPlayer.start();
mediaPlayer.setOnCompletionListener(onCompletionListener);
} 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();
}
// onCompletionListerner
private OnCompletionListener onCompletionListener = new OnCompletionListener() {
#Override
public void onCompletion(MediaPlayer mp) {
// TODO Auto-generated method stub
mediaPlayer.release();
mediaPlayer = null;
}
};

How to stream mp3 files via https, on Android Lollipop?

I have an application which streams mp3 file from AWS server via https protocol.
It works fine on Android 4.4 devices, but not on Android 5.0+ devices.
According to this doc Supported Media Formats I can't understand whether mp3 is supported via http/https or not.
Here is my code:
private void playSong(String songPath, final ArtistSongData data) {
if (currentVersionSupportLockScreenControls) {
UpdateMetadata(data);
remoteControlClient
.setPlaybackState(RemoteControlClient.PLAYSTATE_PLAYING);
}
if (mp.isPlaying()) {
mp.stop();
}
mp.reset();
PlayerConstants.isMediaPlayerPrepared = false;
try {
mp.setDataSource(songPath);
// mp.setDataSource("http://p48.ve.vc/data/48/32549/272565/Kede_Pind_Di_-_Jassi_Gill_-_Jassi_Gill_-_48Kbps_-_www.DjPunjab.Com.mp3");
} 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(AudioStream.MODE_NORMAL);
mp.setAudioStreamType(AudioManager.STREAM_MUSIC);
mp.prepareAsync();
mp.setOnPreparedListener(new OnPreparedListener() {
#Override
public void onPrepared(MediaPlayer arg0) {
// TODO Auto-generated method stub
System.out.println("Music player prepared");
mp.start();
PlayerConstants.isMediaPlayerPrepared = true;
System.out.println("starting now ...");
timer = new Timer();
timer.scheduleAtFixedRate(new MainTask(), 0, 100);
if (PlayerConstants.isHomeShown == true) {
HomeFragment.changeUI();
HomeFragment.setSeekBAr(mp.getDuration());
}
}
});
}
Any help will be appreciated.
Thanks

Playing RTSP Audio stream

I am trying to play RTSP stream with URL format like rtsp://xxxxxxxx.sdp
Many people are trying to play RTSP video stream in Android some try with intent and some with video view like below code
video1=(VideoView)findViewById(R.id.videoview);
video1.setVideoURI(Uri.parse("rtsp://208.77.20.52:1935/dmm1/ten"));
video1.requestFocus();
video1.start();
Is it possible to play audio stream in Android using MediaPlayer object? If yes, Please provide some solution for that I have an audio stream working fine on VLC but does not work in Android.
Below is the code what I have tried for audio stream from RTSP in Android
mediaPlayer.setDataSource(RTSPurl);
mediaPlayer.prepare();
mediaPlayer.start();
Here is full code I am trying:
public class Radio extends AsyncTask<String, Void, String> {
String rtspUrl = "rtsp://192.168.9.59:8080/test.sdp";
MediaPlayer player;
Context c;
public Radio(Context c) {
this.c = c;
Log.i("TAG","Initializing...");
}
#Override
protected void onPostExecute(String result) {
// TODO Auto-generated method stub
super.onPostExecute(result);
player.start();
player.setOnPreparedListener(new OnPreparedListener() {
public void onPrepared(MediaPlayer mp) {
player.start();
Toast.makeText(c, "starting..", Toast.LENGTH_LONG).show();
}
});
}
private void setDataSource(String path2) {
// TODO Auto-generated method stub
player = new MediaPlayer();
try {
Uri a = Uri.parse(rtspUrl);
player.setAudioStreamType(AudioManager.STREAM_MUSIC);
player.setDataSource(c, a);
player.prepareAsync();
} 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();
}
}
#Override
protected String doInBackground(String... params) {
// TODO Auto-generated method stub
setDataSource(rtspUrl);
Log.e("inside doinbackground....", rtspUrl);
return null;
}
}

Android -- streaming a shoutcast audio file

i am trying to stream an shoutcast audio file in my device android 2.2...i heard that this shoutcast will not work in android 2.2. so, i tried with android emulator 2.3 then also song not playing why could you tell me where i am going wrong...and the code is below...
public class AudioDemo extends Activity {
final String song_uris="http://stream.radiosai.net:8004/";
private MediaPlayer mediaplayer;
#Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.main);
Button play = (Button) findViewById(R.id.play);
Button pause = (Button) findViewById(R.id.pause);
Button Previous = (Button) findViewById(R.id.Previous);
Button Next = (Button) findViewById(R.id.Next);
mediaplayer = new MediaPlayer();
mediaplayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
play.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
try {
mediaplayer.setDataSource(song_uris);
} catch (IllegalArgumentException 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();
}
try {
mediaplayer.prepare();
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} // might take long! (for buffering, etc)
mediaplayer.start();
}
});
}
}

How to Reduce the Gap of two Video Playing?

How to Reduce the Gap of two Video Playing?
I have two videos, one video is played on onCreate Method & second is onClick Event of Button but Problem is, if I Clicked the Button that time the Black Screen is Come for 5-10 seconds & after second video is start.
My onCreate Code:-
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
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();
String stringPath = "/sdcard/video.mp4";
if (mediaPlayer.isPlaying()) {
mediaPlayer.reset();
}
mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
mediaPlayer.setDisplay(surfaceHolder);
try {
mediaPlayer.setDataSource(stringPath);
mediaPlayer.prepare();
} catch (IllegalArgumentException 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.start();
}
Button Click Event:-
if (v == btnnext) {
String stringPath = "/sdcard/video2.mp4";
if (mediaPlayer.isPlaying()) {
mediaPlayer.reset();
}
mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
mediaPlayer.setDisplay(surfaceHolder);
try {
mediaPlayer.setDataSource(stringPath);
mediaPlayer.prepare();
} catch (IllegalArgumentException 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.start();
}

Categories

Resources