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;
Related
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.
I am making a android simple media player for playing a audio files and facing a problem with it.
I want to stop Chronometer and media player
When the current Duration of playing file == total duration of file..?
Here is my demo code.
private void playAudio() {
mediaPlayer = new MediaPlayer();
try {
mediaPlayer.setDataSource(outputFile);
} catch (IOException e) {
e.printStackTrace();
}
try {
mediaPlayer.prepare();
} catch (IOException e) {
e.printStackTrace();
}
mChronometer.setBase(SystemClock.elapsedRealtime());
mChronometer.start();
mediaPlayer.start();
long totalDuration = mediaPlayer.getDuration();
Log.i("MediaPlayer", String.valueOf(totalDuration));
}
here is another code added from answer .
i add the listrner
mediaPlayer.setOnCompletionListener(CompletionListener);
MediaPlayer.OnCompletionListener CompletionListener = new MediaPlayer.OnCompletionListener() {
#Override
public void onCompletion(MediaPlayer mp) {
long totalDuration = mp.getDuration();
}
};
Use onCompletionListener to check when media player has reached end of the media file.
mPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
public void onCompletion(MediaPlayer mp) {
// do your task
}
});
For further reference see this link
Media Player
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.
Here is my code:
private void scanningSoundStart() {
scanningMediaPlayer = MediaPlayer.create(this, R.raw.scanning);
scanningMediaPlayer.setOnPreparedListener(new OnPreparedListener() {
#Override
public void onPrepared(MediaPlayer mp) {
}
});
scanningMediaPlayer.setOnVideoSizeChangedListener(new OnVideoSizeChangedListener() {
#Override
public void onVideoSizeChanged(MediaPlayer mp,int width, int height) {
}
});
scanningMediaPlayer.start();
}
private void scanningSoundStop() {
if (scanningMediaPlayer.isPlaying()) {
scanningMediaPlayer.stop();
//scanningMediaPlayer.release();
}
}
I'm trying to play an mp3 file from my raw folder.
The first function which is used for playing the audio file.
And the second function is used for stopping the playing audio.
I get the following error when I repeatedly playing or stopping the audio.
Error : MediaPlayer(10576): error (-19, 0)
I know that my entire approach of handling mediaplayer is wrong.
Please help me to correct this error.
And if possible suggest me how to handle mediaplayer class efficiently.
Try the following code to play audio file.
public void audioPlayer(String path, String fileName){
//set up MediaPlayer
MediaPlayer mp = new MediaPlayer();
try {
mp.setDataSource(path+"/"+fileName);
mp.prepare();
mp.start();
} catch (Exception e) {
e.printStackTrace();
}
}
I have a mp3 file in my android mobile, lets it's a xyz.mp3 somewhere in my sdcard.
How to play it through my application?
Simply you can use MediaPlayer and play the audio file. Check out this nice example for playing Audio:
public void audioPlayer(String path, String fileName){
//set up MediaPlayer
MediaPlayer mp = new MediaPlayer();
try {
mp.setDataSource(path + File.separator + fileName);
mp.prepare();
mp.start();
} catch (Exception e) {
e.printStackTrace();
}
}
If the audio is in the local raw resource:
MediaPlayer mediaPlayer = MediaPlayer.create(context, R.raw.sound_file_1);
mediaPlayer.start(); // no need to call prepare(); create() does that for you
To play from a URI available locally in the system:
Uri myUri = ....; // initialize Uri here
MediaPlayer mediaPlayer = new MediaPlayer();
mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
mediaPlayer.setDataSource(getApplicationContext(), myUri);
mediaPlayer.prepare();
mediaPlayer.start();
#Niranjan, If you are using a raw file from res/raw folder, ie., reading a file stored inside the project, we can use:
mediaplayer.setDataSource(context, Uri.parse("android.resource://urpackagename/res/raw/urmp3name");
If you have to use from SD card:
MediaPlayer mediaPlayer = new MediaPlayer();
File path = android.os.Environment.getExternalStorageDirectory();
mediaPlayer.setDataSource(path + "urmp3filename");
See this related question: MediaPlayer issue between raw folder and sdcard on android
public class MainActivity extends Activity implements OnClickListener {
Button play;
MediaPlayer mp;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
setVolumeControlStream(AudioManager.STREAM_MUSIC);
play=(Button)findViewById(R.id.button1);
play.setOnClickListener(this);
}
#Override
public void onClick(View arg0)
{
mp=MediaPlayer.create(getApplicationContext(),R.raw.song);// the song is a filename which i have pasted inside a folder **raw** created under the **res** folder.//
mp.start();
}
#Override
protected void onDestroy() {
mp.release();
super.onDestroy();
}
}
The replay from https://stackoverflow.com/users/726863/lalit-poptani is great one, it worked the first time, but as I used to have the full path of the file, I did it this way
public void audioPlayer(String path){
//set up MediaPlayer
MediaPlayer mp = new MediaPlayer();
try {
mp.setDataSource(path );
mp.prepare();
mp.start();
} catch (Exception e) {
e.printStackTrace();
}
}
Credit to http://www.helloandroid.com/tutorials/how-play-video-and-audio-android