Playing RTSP Audio stream - android

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

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

progressbar (cyclic) show while show activity

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

Media Player called in state 0, error (-38,0) error

i'm trying to use this code which i called from a button but it doesn't work and when i see the LogCat window i found Media Player called in state 0, error (-38,0)
public void audioPlayer(String path, String fileName){
//set up MediaPlayer
MediaPlayer mp = new MediaPlayer();
try {
mp.setDataSource(HeyActivity.this, Uri.parse(path+"/"+fileName));
} 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 {
mp.prepare();
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
System.out.print("hey");
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
mp.start();
}
Try setting OnPreparedListener and call the start method within it. Like this:
MediaPlayer mp = new MediaPlayer();
mp.setOnPreparedListener( new OnPreparedListener() {
public void onPrepared(MediaPlayer mp) {
mp.start();
}
};
);
I finally solved the problem by running on an emulator that supported audio!!
Can't believe it was that simple!
Window > AVD Manager > Edit your AVD (I created a new one to be on the safe side cause I was running from snapshot) > Hardware > New > Audio Playback Support

Known streaming mp3 link, Correct Android Version. What is wrong with streaming?

I'm using Android 2.3, so according to this post: Streaming Audio from A URL in Android using MediaPlayer? streaming should work. I'm using a proven mp3 stream from kexp.org.
05-19 13:29:17.141: INFO/StagefrightPlayer(68): setDataSource('http://kexp-mp3-1.cac.washington.edu:8000')
05-19 13:29:17.141: INFO/AwesomePlayer(68): prepare was cancelled before doing anything
try {
mp = new MediaPlayer();
mp.reset();
mp.setOnPreparedListener(new OnPreparedListener() {
#Override
public void onPrepared(MediaPlayer mp) {
mp.start();
}
});
mp.setDataSource("http://kexp-mp3-1.cac.washington.edu:8000");
mp.prepareAsync();
} 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();
} finally {
if(mp!=null) {
mp.release();
mp =null;
}
}
Try this:
MediaPlayer.setDataSource() and prepare() not working - android

Categories

Resources