Play AudioStream (RTP, HTTP) on Android - android

I'm using Android MediaPlayer to play a stream on my device but I get an error upon playing a stream.
I'm using VLC on my PC to stream Audio to my device. With the Android version of the VLC I am able to play the sound on my device, so my streaming is OK.
Here's a my code:
try {
player = new MediaPlayer();
player.setAudioStreamType(AudioManager.STREAM_MUSIC);
player.setDataSource("http://192.168.3.111:8080/xx");
//player.setSurface(null);
//player.prepare();
player.prepareAsync();
//player.start();
player.setOnPreparedListener(new OnPreparedListener() {
public void onPrepared(MediaPlayer mp) {
player.start();
}
});
player.setOnErrorListener(new OnErrorListener() {
#Override
public boolean onError(MediaPlayer mp, int what, int extra) {
Log.e("RTP", "ERROR: " + what + " " + extra);
return false;
}
});
} catch (Exception e) {
Log.d("RTP", e.getMessage());
}
Every time I get the following error:
error (1, -2147483648)
I tried RTP, RTSP, HTTP and always same result.
Update1: Is it a codec problem? I stream mp3 without any transcoding.

Related

MediaPlayer not calling OnPreparedListener after setting DataSource as URL

I am using media player to play a mp3 from URL.But my media player not calling OnPreparedListener after I am setting a URL as my data source. And I called PrepareAsync(). The URL is working in the browser. And I am not getting any errors.So please help me.
This is the way I am setting liseners. And I implemented all.
mediaPlayer.setOnCompletionListener(this);
mediaPlayer.setOnErrorListener(this);
mediaPlayer.setOnPreparedListener(this);
mediaPlayer.setOnInfoListener(this);
mediaPlayer.setOnBufferingUpdateListener(this);
And this is my ErrorListener
//Handle errors
#Override
public boolean onError(MediaPlayer mp, int what, int extra) {
//Invoked when there has been an error during an asynchronous operation.
switch (what) {
case MediaPlayer.MEDIA_ERROR_NOT_VALID_FOR_PROGRESSIVE_PLAYBACK:
Log.d("MediaPlayer Error", "MEDIA ERROR NOT VALID FOR PROGRESSIVE PLAYBACK " + extra);
break;
case MediaPlayer.MEDIA_ERROR_SERVER_DIED:
Log.d("MediaPlayer Error", "MEDIA ERROR SERVER DIED " + extra);
break;
case MediaPlayer.MEDIA_ERROR_UNKNOWN:
Log.d("MediaPlayer Error", "MEDIA ERROR UNKNOWN " + extra);
break;
}
return false;
}
And this is my OnPreparedListener
#Override
public void onPrepared(MediaPlayer mp) {
//Invoked when the media source is ready for playback.
startMedia();
}
By using ExoAudioPlayer I fixed the Issue.

Audio Playing But Video not Showing in VideoView Android (Black Screen in Low Internet)

I am Working on Video Streaming in Android. when I want to play video in Low Internet Speed video is not appear in the VideoView show black Screen but Audio is playing fine. i have see these links link1 and link2 . But these link couldn't help me. Here is my code snapshot:-
private void playvideo(String url){
final MediaController mediacontroller;
try {
mediacontroller = new MediaController(MainActivity.this);
mediacontroller.setAnchorView(videoView);
final Uri video = Uri.parse(url);
videoView.setMediaController(mediacontroller);
videoView.setVideoURI(video);
videoView.requestFocus();
videoView.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
// Close the progress bar and play the video
public void onPrepared(MediaPlayer mp) {
mp.start();
mp.setOnErrorListener(new MediaPlayer.OnErrorListener() {
#Override
public boolean onError(MediaPlayer mediaPlayer, int what, int extra) {
if (extra == MediaPlayer.MEDIA_ERROR_SERVER_DIED
|| extra == MediaPlayer.MEDIA_ERROR_MALFORMED) {
Log.e(TAG , "MEDIA_ERROR_SERVER_DIED && MEDIA_ERROR_MALFORMED");
Toast.makeText(MainActivity.this , "Server Died or Malformed" , Toast.LENGTH_LONG).show();
return true;
} else if (extra == MediaPlayer.MEDIA_ERROR_IO) {
Log.e(TAG , "MediaPlayer.MEDIA_ERROR_IO");
Toast.makeText(MainActivity.this , "Media Error IO" , Toast.LENGTH_LONG).show();
return true;
}else if (extra == MediaPlayer.MEDIA_ERROR_UNKNOWN){
Log.e(TAG , ".MEDIA_ERROR_UNKNOWN");
Toast.makeText(MainActivity.this , "MEDIA ERROR UNKNOWN" , Toast.LENGTH_LONG).show();
return true;
}
return false;
}
});
mp.setOnBufferingUpdateListener(new MediaPlayer.OnBufferingUpdateListener() {
#Override
public void onBufferingUpdate(MediaPlayer mediaPlayer, int percentage) {
Log.e(TAG , "onBufferingUpdate :- " + percentage);
}
});
progressBar.setVisibility(View.GONE);
videoView.start();
}
});
} catch (Exception e) {
Log.e("Error", e.getMessage());
progressBar.setVisibility(View.GONE);
Toast.makeText(getApplicationContext() ,"URL is Wrong Test with other URL" , Toast.LENGTH_LONG).show();
e.printStackTrace();
}
}
because Audio and Video both are different Streams so in Low Internet somehow it able to download the audio stream but not a video stream, is it ?? so in this case, it is possible to show a message to the user like "you have Low Internet Try Again" and not to play Audio ??
thanks
Try to use Exoplayer which is an alternative to Android’s MediaPlayer API for playing audio and video both locally and over the Internet.
https://github.com/google/ExoPlayer
I recommend you to use SurfaceView.
surfaceView = (SurfavceView) findView.....;
holder = surfaceView.getHolder();
holder.addCallback(this); // implement SurfaceHolder.Callback methods
Then in surfaceCreated method:
mediaPlayer.setDisplay(holder);
mediaPlayer.setDataSource(mVideoUrl);
mediaPlayer.prepareAsync();
You also can use https://github.com/anthorlop/EasyExoPlayer
It use ExoPlayer for Android SDK >= 16 and MediaPlayer for Android < 16.
I hope it works for you.

MediaPlayer not playing live stream for some audio file

some audio file url not playing in mediaplayer
here my code
mediaPlayer = new MediaPlayer();
if (mediaPlayer != null) {
try {
String audioUrl = Constants.AudioLink.toString().replace(" ", "%20");
mediaPlayer.setDataSource(audioUrl);
mediaPlayer.prepareAsync();
mediaPlayer.setOnErrorListener(new MediaPlayer.OnErrorListener() {
#Override
public boolean onError(MediaPlayer mediaPlayer, int i, int i1) {
Progressdialogs.getInstance().closeDialog();
Toast.makeText(AudioPlayActivity.this, "Failed to load audio", Toast.LENGTH_SHORT).show();
return false;
}
});
mediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
#Override
public void onPrepared(MediaPlayer mediaPlayer) {
Progressdialogs.getInstance().closeDialog();
finalTime = mediaPlayer.getDuration();
seekbar.setMax((int) finalTime);
seekbar.setClickable(false);
play();
duration.setVisibility(View.VISIBLE);
}
});
} catch (Exception e) {
e.printStackTrace();
}
}
I noticed that small audio files easily played but large audio file not playing. i also try FFmpegMediaPlayer library intead of MediaPlayer then large audio file played but my apk file size increase to 32 MB. so i can't use FFmpegMediaPlayer for play audio through url. Please suggest me best library or native code for play large audio file as live stream.
Thanks in advance
i use following library
compile 'com.devbrackets.android:exomedia:3.1.1'
now large file playing perfectly.
Use
import com.devbrackets.android.exomedia.EMAudioPlayer;
private EMAudioPlayer mediaPlayer;

Sound Plays from Smarthpone Speakers instead Android Auto Speakers

I am new to Android Auto and still trying to figure it out.
I have successfully played music from my own programmed app but the music is coming out from my smartphone speakers instead of car speakers. Other (sample) apps do it the right way.
Which part of of the Media System is responsible to handle this behaviour? Android documentation says the sound is sent to the car speakers.
#Override
public void onPlayFromMediaId(String mediaId, Bundle extras) {
this.mPlayingQueue.add(item);
session.setActive(true);
session.setQueue(mPlayingQueue);
session.setQueueTitle("My Queue");
session.setPlaybackState(buildState(PlaybackState.ACTION_PLAY));
session.setMetadata(createRammsteinMetaData());
this.mediaPlayer = MediaPlayer.create(getBaseContext(), R.raw.rammstein_sonne);
this.mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
if (tryToGetAudioFocus()) {
this.mediaPlayer.start();
Log.d("AUDIOTAG", "Playing");
} else {
Log.d("AUDIOTAG", "Playing not possible, no focus");
}
}
private boolean tryToGetAudioFocus() {
int result = audioManager.requestAudioFocus(this, AudioManager.STREAM_MUSIC, AudioManager.AUDIOFOCUS_GAIN);
return result == AudioManager.AUDIOFOCUS_REQUEST_GRANTED;
}
Thanks in advance. Orrimp
There seems to be a pretty big bug there! Local music from resources does not play properly using MediaPlayer.create(...);
Thx Reaz Murshed I just tried to use the STREAM_MUSIC as a real stream with Internet Music and it works.
Translate to use setDataSource with Resource URI!
Use the following snippet:
#Override
public void onPlayFromMediaId(String mediaId, Bundle extras) {
/* Set session stuff like queue, metadata and so on*/
Uri myUri = resourceToUri(getBaseContext(), R.raw.rammstein_sonne);
try {
mediaPlayer = new MediaPlayer();
mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
mediaPlayer.setDataSource(getBaseContext(), myUri);
mediaPlayer.prepareAsync();
mediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
#Override
public void onPrepared(MediaPlayer mp) {
if (tryToGetAudioFocus()) {
mediaPlayer.start();
Log.d("AUDIOTAG", "Playing");
} else {
Log.d("AUDIOTAG", "Playing not possible, no focus");
}
}
});
} catch (IOException e) {
e.printStackTrace();
Log.d("AUTO", "EERORROR");
}
}
public Uri resourceToUri(Context context, int resID) {
return Uri.parse(ContentResolver.SCHEME_ANDROID_RESOURCE + "://" +
context.getResources().getResourcePackageName(resID) + '/' +
context.getResources().getResourceTypeName(resID) + '/' +
context.getResources().getResourceEntryName(resID));
}

Android SDK MediaPlayer ﹕ error (1, -1004)

I use Android sdk.
I change data source of the media player in loop.
Sometimes ( I couldnt find any cause ) application doesn't play new source.
It is not about the source, the sources are correct. Also, the application may play the source next time.
Some parts of my code:
player.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
public void onPrepared(MediaPlayer player) {
player.start();
}
});
--
player.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
#Override
public void onCompletion(MediaPlayer player) {
//Do the work after completion of audio
player.stop();
player.reset();
playSound("en", new word);
public void playSound(String oLanguage, String word){
String link = LINK; //WORKING, I am sure
try {
player.setAudioStreamType(AudioManager.STREAM_MUSIC);
player.setDataSource(link);
player.prepare();
} catch (Exception e) {
Log.i("Error while playing word: ", word);
Log.i("Error while playing link: ",link);
// TODO: handle exception
}
}

Categories

Resources