I want to make an app which plays the sound from an server.
Receiving and playing from the internet works, but with my JMF-Programm it does not work (Error (-38,0)).
here is my code-snippet from the app:
MediaPlayer mPlayer = new MediaPlayer();
// String url
// ="http://programmerguru.com/android-tutorial/wp-content/uploads/2013/04/hosannatelugu.mp3";
String url = "rtp://192.168.32.29:22222/audio/16";
mPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
AudioManager audioManager = (AudioManager) this
.getSystemService(Context.AUDIO_SERVICE);
audioManager.setStreamVolume(AudioManager.STREAM_MUSIC, volume, volume);
try {
mPlayer.setDataSource(url);
} catch (IllegalArgumentException e) {
Toast.makeText(getApplicationContext(),
"You might not set the URI correctly!", Toast.LENGTH_LONG)
.show();
} catch (SecurityException e) {
Toast.makeText(getApplicationContext(),
"You might not set the URI correctly!", Toast.LENGTH_LONG)
.show();
} catch (IllegalStateException e) {
Toast.makeText(getApplicationContext(),
"You might not set the URI correctly!", Toast.LENGTH_LONG)
.show();
} catch (IOException e) {
e.printStackTrace();
}
try {
mPlayer.prepare();
} catch (IllegalStateException e) {
Toast.makeText(getApplicationContext(),
"You might not set the URI correctly!", Toast.LENGTH_LONG)
.show();
} catch (IOException e) {
Toast.makeText(getApplicationContext(),
"You might not set the URI correctly!", Toast.LENGTH_LONG)
.show();
}
mPlayer.start();
when I use "http://programmerguru.com/android-tutorial/wp-content/uploads/2013/04/hosannatelugu.mp3" it works, but "rtp://192.168.32.29:22222/audio/16" sends me error (-38,0)
Can someone help me?
Thanks
Related
When I click on the play button again and again, it plays multiple times simultaneously. I want to stop multiple playing. Here's the code:
Objects for Media player and buttons
MediaPlayer mPlayer;
Button playbtn;
Button stopbtn;
Button click event listener to play audio
playbtn = (Button) findViewById(R.id.play);
playbtn.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Uri myUri1 = Uri.parse("file:///sdcard/Birds/parrot.mp3");
mPlayer = new MediaPlayer();
mPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
try {
mPlayer.setDataSource(getApplicationContext(), myUri1);
} catch (IllegalArgumentException e) {
Toast.makeText(getApplicationContext(), "You might not set the URI correctly!", Toast.LENGTH_LONG).show();
} catch (SecurityException e) {
Toast.makeText(getApplicationContext(), "You might not set the URI correctly!", Toast.LENGTH_LONG).show();
} catch (IllegalStateException e) {
Toast.makeText(getApplicationContext(), "You might not set the URI correctly!", Toast.LENGTH_LONG).show();
} catch (IOException e) {
e.printStackTrace();
}
try {
mPlayer.prepare();
} catch (IllegalStateException e) {
Toast.makeText(getApplicationContext(), "You might not set the URI correctly!", Toast.LENGTH_LONG).show();
} catch (IOException e) {
Toast.makeText(getApplicationContext(), "You might not set the URI correctly!", Toast.LENGTH_LONG).show();
}
mPlayer.start();
Button click event listener to stop audio
stopbtn = (Button) findViewById(R.id.stop);
stopbtn.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
if(mPlayer!=null && mPlayer.isPlaying()){
mPlayer.stop();
}
}
});
any help would be appreciated. Thanks!
It is creating a new player with every play click but only keeping the reference to the last player so the solution is to keep player in a list and stop all.
private List<MediaPlayer> players = new ArrayList<>()
stopbtn = (Button) findViewById(R.id.stop);
stopbtn.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
for(player in players){
if(player!=null && player.isPlaying()){
mPlayer.stop();
}
}
players.clear();
}
});
Another solution is, only use one instance of player. move the player initialization outside click as:
playbtn = (Button) findViewById(R.id.play);
Uri myUri1 = Uri.parse("file:///sdcard/Birds/parrot.mp3");
mPlayer = new MediaPlayer();
mPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
try {
mPlayer.setDataSource(getApplicationContext(), myUri1);
} catch (IllegalArgumentException e) {
Toast.makeText(getApplicationContext(), "You might not set the URI correctly!", Toast.LENGTH_LONG).show();
} catch (SecurityException e) {
Toast.makeText(getApplicationContext(), "You might not set the URI correctly!", Toast.LENGTH_LONG).show();
} catch (IllegalStateException e) {
Toast.makeText(getApplicationContext(), "You might not set the URI correctly!", Toast.LENGTH_LONG).show();
} catch (IOException e) {
e.printStackTrace();
}
try {
mPlayer.prepare();
} catch (IllegalStateException e) {
Toast.makeText(getApplicationContext(), "You might not set the URI correctly!", Toast.LENGTH_LONG).show();
} catch (IOException e) {
Toast.makeText(getApplicationContext(), "You might not set the URI correctly!", Toast.LENGTH_LONG).show();
}
playbtn.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
if(!mPlayer.isPlaying())
mPlayer.start();
there is some way i can color in some color part of my code (not the output, the actually code ) in android studio ?
something like this :
case R.id.buttonbackup:
Toast.makeText(this, "first", Toast.LENGTH_SHORT).show();
try {
Toast.makeText(this, "before try", Toast.LENGTH_SHORT).show();
BackUpContacts();
DeleteAllContacts();
Toast.makeText(this, "after try", Toast.LENGTH_SHORT).show();
} catch (IOException e) {
Toast.makeText(this, "EX", Toast.LENGTH_SHORT).show();
e.printStackTrace();
Toast.makeText(this,e.getStackTrace().toString() , Toast.LENGTH_SHORT).show();
} catch (Exception e) {//this the part i want to color
e.printStackTrace();
}
break;
i want to color all the part of the catch . there is some way to do that ?
I use following code for record video but recorded video file is not save or store in sdcard and can i record video in portrait mode.i got this code from link https://github.com/commonsguy/cwac-camera
if (!isRecording()) {
try {
record();
getActivity().invalidateOptionsMenu();
} catch (Exception e) {
Log.e(getClass().getSimpleName(),
"Exception trying to record", e);
Toast.makeText(getActivity(), e.getMessage(),
Toast.LENGTH_LONG).show();
}
} else {
try {
stopRecording();
getActivity().invalidateOptionsMenu();
} catch (Exception e) {
Log.e(getClass().getSimpleName(),
"Exception trying to stop recording", e);
Toast.makeText(getActivity(), e.getMessage(),
Toast.LENGTH_LONG).show();
}
}
This question already has answers here:
Downloading an mp3 file from server in android
(3 answers)
Closed 7 years ago.
i am unable to download the song from internet but i can listen the song but i want to my app to perform as, when i am clicking the play button it should download that song too.
playbtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
mPlayer = new MediaPlayer();
if(web[position] == web[0]){
if(mPlayer.isPlaying()){
mPlayer.stop();
mPlayer.prepareAsync();
mPlayer.seekTo(0);
}
else{
Uri myUri1 = Uri.parse("http://sound21.mp3slash.net/320/indian/bajrangibhaijaan/01%20-%20Selfie%20Le%20Le%20Re%20-%20Bajrangi%20Bhaijaan%20[Songspk.LINK].mp3");
try {
mPlayer.setDataSource(getContext(), myUri1);
} catch (IllegalArgumentException e) {
Toast.makeText(getContext(), "You might not set the URI correctly!", Toast.LENGTH_LONG).show();
} catch (SecurityException e) {
Toast.makeText(getContext(), "You might not set the URI correctly!", Toast.LENGTH_LONG).show();
} catch (IllegalStateException e) {
Toast.makeText(getContext(), "You might not set the URI correctly!", Toast.LENGTH_LONG).show();
} catch (IOException e) {
e.printStackTrace();
}
try {
mPlayer.prepare();
} catch (IllegalStateException e) {
Toast.makeText(getContext(), "You might not set the URI correctly!", Toast.LENGTH_LONG).show();
} catch (IOException e) {
Toast.makeText(getContext(), "You might not set the URI correctly!", Toast.LENGTH_LONG).show();
}
mPlayer.start();
playbtn.setText("Pause");
}
You mean when you click button , start playing and downloading?
Use DownloadManager to download the audio file.
http://developer.android.com/reference/android/app/DownloadManager.html
Let me explain clearly:
I have folder named "Sounds" and there are some .ogg files to play.
I got the folder path using Environment.getExternalStorageDirectory().getAbsolutePath() + mySoundsPath;.
I got all list from that folder and save it to array: List<String> soundList;
My question is:
How to call the sound from soundList that i created so they all can be played??
Should it need to decoded(like images, decoded to Bitmap)??
Sorry for my grammar.
Thanks in advance.
Use the below link for the reference
http://developer.android.com/reference/android/media/MediaPlayer.html
and you can try small code sample as well
http://davanum.wordpress.com/2007/12/29/android-videomusic-player-sample-from-local-disk-as-well-as-remote-urls/
public void play(String path) {
count++;
playFile=path;
//showNotification();
new NotificationPanel(activity);
if(mediaPlayer!=null && mediaPlayer.isPlaying()){
Log.d("*****begin*****", "playing");
stopPlaying();
Log.d("*****begin*****", "stoping");
} else{
Log.d("*****begin*****", "nothing");
}
Uri myUri1 = Uri.parse(path);
mediaPlayer = new MediaPlayer();
mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
try {
mediaPlayer.setDataSource(activity, myUri1);
} catch (IllegalArgumentException e) {
Toast.makeText(activity, "You might not set the URI correctly!", Toast.LENGTH_LONG).show();
} catch (SecurityException e) {
Toast.makeText(activity, "You might not set the URI correctly!", Toast.LENGTH_LONG).show();
} catch (IllegalStateException e) {
Toast.makeText(activity, "You might not set the URI correctly!", Toast.LENGTH_LONG).show();
} catch (IOException e) {
e.printStackTrace();
}
try {
mediaPlayer.prepare();
} catch (IllegalStateException e) {
Toast.makeText(activity, "You might not set the URI correctly!", Toast.LENGTH_LONG).show();
} catch (IOException e) {
Toast.makeText(activity, "You might not set the URI correctly!", Toast.LENGTH_LONG).show();
}
mediaPlayer.start();
}
private void stopPlaying() {
if (mediaPlayer != null) {
mediaPlayer.stop();
mediaPlayer.release();
mediaPlayer = null;
}
}