I'm trying to play an rtsp stream using MediaPlayer in android and the application seems to always become stuck on MediaPlayer.prepare();
The url is valid as I tested it using VLC on my desktop.
Any ideas why the application is not preparing the stream.
class InitializeService extends Thread {
#Override
public void run() {
try {
player.prepare();
Log.d("Play", "Player prepared");
} catch (IOException e) {
e.printStackTrace();
fallback();
} catch (IllegalStateException e) {
e.printStackTrace();
fallback();
}
}
}
The log statement is never reached.
Update 1:
Sorry I forgot to mention that the stream will always be in 3gp format. Here is a url rtsp://r2---sn-p5qlsu76.c.youtube.com/CiILENy73wIaGQnTXOVs7Kwo8xMYESARFEgGUgZ2aWRlb3MM/0/0/0/video.3gp
Your stream might not be of a format supported by Android.
Check http://developer.android.com/guide/appendix/media-formats.html to see if Android supports it.
Turns out it was android l that wasn't able to play the streams.
Related
I've got a little problem with MediaPlayer.
Problem appears on Android 4.0.3 - 4.0.4.
Whats happening is that my onPrepare() method is not fired at all on those two versions.
Tested on 4.3 and 4.4 and no problem at all.
I have no idea what might be the reason for it to not work. So any ideas, thoughts etc are welcome.
PLAY CODE:
try {
if (!player.isPlaying()) {
player.reset();
player.setOnPreparedListener(this);
player.setOnCompletionListener(mFragment);
player.setDataSource(String.valueOf(tmpUri));
player.prepareAsync();
}
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
start() is in on prepare method.
Logcat error:
ERROR/MediaPlayer(10830): Error (1,-2147483648)
ERROR/MediaPlayer(10830): mOnErrorListener is null. Failed to send MEDIA_ERROR message.
Ive found the answer to my problem, so I'm going to share my info.
Anyway, the problem was that on Android 4.0.4 and 4.0.3 when reading mp3 files from
device memory setDataSource() did not read the length of file right.
Which made MediaPlayer go psycho and call Error (1,-2147483648).
Android was giving all files length = 0x7ffffffffffffffL.
Why? I don't know ;)
Fix:
File file = new File(uri);
long length = file.getTotalSpace()
FileInputStream is = new FileInputStream(file);
player.setDataSource(is.getFD(), 0l, length);
player.prepareAsync();
That's it!
Hope it will help someone with similar problem, peace!
I am playing video from url then .mp4 in playing but .mov format is giving IOException
java.io.IOException: Prepare failed.: status=0x1
My code for playing video is,
private void playVideo() {
if (extras.getString("video_path").equals("VIDEO_URI")) {
showToast("Please, set the video URI in HelloAndroidActivity.java in onClick(View v) method");
} else {
new Thread(new Runnable() {
public void run() {
try {
player.setDataSource(extras.getString("video_path"));
player.setDisplay(holder);
player.prepare();
} catch (IllegalArgumentException e) {
showToast("Error while playing video");
Log.i(TAG, "========== IllegalArgumentException ===========");
e.printStackTrace();
} catch (IllegalStateException e) {
showToast("Error while playing video");
Log.i(TAG, "========== IllegalStateException ===========");
e.printStackTrace();
} catch (IOException e) {
showToast("Error while playing video. Please, check your network connection.");
Log.i(TAG, "========== IOException ===========");
e.printStackTrace();
}
}
}).start();
}
}
Any pointers?
Please visit the website vitamio.org or download VitamioDemo from github github.com/yixia/VitamioDemo
if you want to use Vitamio as your video player in an app, you need to download the Vitamio SDK from their website. Play around with the demo project and you'll understand how it works.
I have implemented the same and it works absolutely fine with me.
.mov files are not supported in android.
Check the Supported Media Formats on developers site.
Why doesn't the MediaPlayer show the video as soon as it is available. What I mean is on the IPhone when a video is played the video shows up right away. Even when returning from pause. But on the Android the screen stays black for a couple of milliseconds to a second depending on the device used and how many processes are running in the background.
I'm asking this because i want to use one of the beginning frames from my video play as a type of screenshot and currently I'm using a handler to wait 1 second before pausing the video.
Can someone tell me a quick way to make the video show up as soon as it is started or even prepared instead of my workaround?
EDIT:
Here is how I prepare my video player so It should be prepared right.
private void initVideo()
{
Log.i("VideoPlayer", "Initialize Video File" + videoFileName);
AssetFileDescriptor afd;
try {
if(videoFileName != null);
{
afd = getAssets().openFd(videoFileName);
vidplayer = new MediaPlayer();
vidplayer.setDataSource(afd.getFileDescriptor(), afd.getStartOffset(), afd.getDeclaredLength());
vidplayer.setDisplay(holder);
vidplayer.prepare();
vidplayer.setOnCompletionListener(this);
vidplayer.setOnPreparedListener(this);
//Log.i("INITVIDEO", Integer.toString(videoPausedAt));
vidplayer.seekTo(videoPausedAt);
//Log.i("VideoPlayer", "video Prepared");
videoDuration = vidplayer.getDuration()/1000;
isVideoReady = true;
}
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (Exception e)
{
//Log.i("InitPlayer", e.getClass().toString());
e.printStackTrace();
}
}
For the background, you can get a thumbnail of the video:
private Bitmap getThumbnail(String path){
try{
return ThumbnailUtils.createVideoThumbnail(path, MediaStore.Images.Thumbnails.MINI_KIND);
}catch(Exception e){
return null;
}
}
When the video starts, you'll need to set the background back to null or you won't be able to see the video.
As for it not playing right away, it should play as soon as start() is called if you prepared it correctly, but it could be delayed if it has to load data let's say from a stream over the internet.
I have found that it is the phones fault.(mostly) Video's will show up automatically unless phone is bogged down with apps and thus loading of the video takes longer (noticed after having a voip service running).
I have an app that will play a tone, it did work perfectly until I installed it on Gingerbread 2.3.4 based android phone. The problem is I don't see any exception thrown, and no sound is generated, exactly same code does play sound in pre gingerbread phone. Here is the code that plays the sound.
MediaPlayer mp = new MediaPlayer();
//mp.release();
try {
String audioFilePath = "content://media/internal/audio/media/20";
mp.setDataSource(audioFilePath);
mp.prepare();
mp.start();
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
I have comments placed before and after the code to play sound, all comments are printed as if sound is played, but there is no sound.
Thanks for any help.
never mind, seems like somehow the volume on alerts was set to 0, I actually went to actual folder, selected a file, played it manually and then turned the volume up while being played and all was well afterwards.
I have online radio (shout cast ) in web. I want to develop android app for listen this stream. so I want to know how to play online stream in android. using URL. In Android Im not going to Stream the audio. I want listen the web stream from android app.
How to do that..?
thanks
Something like
private void init() throws IOException {
try {
mediaPlayer = new MediaPlayer();
String streamPath = "";//enter path
mediaPlayer.setDataSource(streamPath);
mediaPlayer.prepare();
} catch (MalformedURLException ex) {
throw new RuntimeException("Wrong url for mediaplayer! " + ex);
} catch (IllegalStateException ex) {
} catch (IllegalArgumentException ex) {
throw new RuntimeException("Wrong url for mediaplayer! " + ex);
}
}
private void play() {
mediaPlayer.start();
}
Please refer the link it may help you a while, http://developer.android.com/guide/topics/media/index.html