I'm back with another problem!
I'm trying to create an app that would list a selected Livestreams, from Own3d.TV, Justin.Tv etc...
If my research isn't totally failed, I can use the MediaPlayer object to Stream video, the only question is how do I use it?
So far my code looks like this, but it's giving me an Exception when trying to prepare the MediaPlayer.
public class Media extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
SurfaceView sw = new SurfaceView(this);
SurfaceHolder sh = sw.getHolder();
setContentView(sw);
Uri ur = Uri.parse("http://www.twitch.tv/widgets/live_embed_player.swf?channel=hashe");
MediaPlayer mp = new MediaPlayer();
mp.setDisplay(sh);
try {
mp.setDataSource("http://www.twitch.tv/widgets/live_embed_player.swf?channel=hashe");
mp.prepare();
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
mp.start();
//mp.setDisplay(sw);
}
}
Is it even possible to Stream the video from these sites using the MediaPlayer?
If not, how shoud I approach this problem?
Thanks!
when implementing mediaPlayer, try something like this, it worked fine for me: http://android-er.blogspot.com/2010/11/play-3gp-video-file-using-mediaplayer.html
but I think, u cant use source like u have written in your code. You have to use some URL from where is video streamed directly with progresive streaming, not URL of html site with player embeded. You can recognize: when u write useful URL with streamed video in your internet browser, browser starts to download this streamed video in your computer. And I think, MediaPlayer supports in core .3gp or .mp4 format only..
I hope u understand my bad english
Use ExoPlayer instead of MediaPlayer. See Android official documentation:
ExoPlayer supports features like Dynamic adaptive streaming over HTTP (DASH), SmoothStreaming and Common Encryption, which are not supported by MediaPlayer. It's designed to be easy to customize and extend.
ExoPlayer - Android Developers
Related
I have saved the sample mp4 video in android internal storage in a file, But when try to read from file and playback in android videoview by parsing file path as a uri , am getting can't play this video error. I had checked many links but no use, How do i implement this.?
final String mFileName = "sample.mp4";
try{
FileOutputStream mFileOutputStream = openFileOutput(mFileName, Context.MODE_PRIVATE );
mFileOutputStream.write(getResources().getAssets().open("sample_video.mp4").read());
mFileOutputStream.close();
Log.v(TAG, "Success");
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
MediaController mediaController = new MediaController(this);
mediaController.setAnchorView(mVideoView);
mVideoView.setMediaController(mediaController);
mVideoView.setVideoPath(getFilesDir().getAbsolutePath()+"/sample.mp4");
mVideoView.start();
Simply you can't. VideoView internally uses a MediaPlayer, which explicitly requires a file to be world-readable (app's dirs aren't). The only way to bypass this limitation, as doc says, is to pass a File descriptor rather than the file path.
Unfortunately VideoView does not have a method like setDataSource(FileDescriptor), so you have two ways:
Access the VideoView's MediaPlayer through reflection and call setDataSource(FileDescriptor) on it. Not sure if this is a feasible solution, because the internal media player is instantiated when you call VideoView.setVideoPath, but you should not call it... in short, evaluate it, but it may be a dead end.
Use a MediaPlayer, set a surface holder on which play the video and pass a FileDescriptor directly to the MediaPlayer (imo this second solution is cleaner).
I created an app in which I am using Android's mediaplayer to stream multiple video files from a HTTP source. I am using the Prepare() method instead of prepareAsync() since I can't continue unless something is being shown.
Here I have a simple method that return each of the mediaplayer instances:
MediaPlayer mediaPreparation(String filename) {
String url = "myURL"; // your URL here
// create mediaplayer instance
MediaPlayer mediaPlayer = new MediaPlayer();
try {
mediaPlayer.setDataSource(url);
mediaPlayer.prepare();
} catch (IOException e) {
}
mediaPlayer.setLooping(true);
// mediaPlayer.start();
return mediaPlayer;
}
And then I start them one by one using start(). Now I have two questions:
1- How does Prepare() really work? It takes a while for my app to start. Isn't it supposed to first receive a portion of the video stream (not download the whole) and then start playing?!
2- Can I manually buffer the video so I have more control over the buffer, and feed it into a mediaplayer to play? In specific, given a condition I need to start the video from a specific time. So probably I need to buffer the video stream from a specific time of the video.
I am developing an android application for android OS > 4.0 (including and post OS). I have a sample m3u8 file as follows :
#EXTM3U
#EXT-X-TARGETDURATION:56
#EXT-X-MEDIA-SEQUENCE:0
#EXTINF:28, no desc
ulr/audio/file.mp3
#EXTINF:28, no desc
ulr/audio/file.mp3
#EXT-X-ENDLIST
and I am trying to play that file, using the following code
mMediaPlayer = new MediaPlayer();
mMediaPlayer.setOnErrorListener(this);
mMediaPlayer.setOnPreparedListener(this);
mMediaPlayer.setOnCompletionListener(this);
mMediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
try
{
mMediaPlayer.setDataSource(uri);
} catch (IllegalArgumentException e)
{
e.printStackTrace();
} catch (SecurityException e)
{
e.printStackTrace();
} catch (IllegalStateException e)
{
e.printStackTrace();
} catch (IOException e)
{
e.printStackTrace();
}
mMediaPlayer.prepareAsync();
and my onPrepared() method is as follows :
public void onPrepared(MediaPlayer player)
{
player.start();
}
But the code first comes to onPrepared() and then immediately goes to onError(), with the what=1 and the extra=-1010.
I know this question has been asked various times (here, here and here for instance) and I also know about Vitamio, but i want to find out what is wrong with my implementation. Is there something wrong with the m3u8 file that I created? I went through its documentation and everything seems correct.
Would be really glad if someone could throw some light in this matter.
Error code -1010 matches up with MEDIA_ERROR_UNSUPPORTED which would imply that the device does not have the hardware or software codecs it needs to decode the MP3 files in your playlist.
Vitamio would work in this situation because it adds software decoding for the media. This is slower than hardware decoding and uses more battery. It can also increase your app size significantly.
This seems odd, though, since MP3 has been a supported media format for decoding in Android for a very long time.
I want to play video content from HttpResponse object. The content is downloading from a streaming sever.
My requirement is to create a HTTP POST request to the server. Request contains the URL of video, username and password of the user for authentication purpose.
I wish to know how can we create a HTTP POST request and play/download the video.
Kindly provide some hints, steps/code to proceed.
Thanks,
I am not 100% sure, but I think that you can't stream most of the video due to the way the format stores the meta data of the video. That's why you tube has to convert your video files instead of just showing them in any format.
There are protocols that encapsulate this metadata and allows you to stream any video (that's what youtube mobile does). You should take a look at RTSP: http://en.wikipedia.org/wiki/Real_Time_Streaming_Protocol
If you use an rtsp link in a videoView it should stream the video flawlessly. The point is that your problem is server and not client related.
As an exercise you could grab an rtsp link from m.youtube.com and just pass to a videoView using setVideoPath and it should work.
If can't change the server implementation then you probably them I guess your solutions are:
1) Download and decode the video yourself, you would have to handle all the metadata and ensure that the video does work though. You could, theoretically, compile ffmpeg for android to help you with that, but I couldn't compile it to android with the network option on. That is a lot of work.
2) Write your own implementation of RTSP or other streaming protocol. Download the video in a thread and them create a local server in the device to stream that video file to a videoView instance. I actually have this working on an app. Android doesn't support the specific protocol that the client's servers use and I had to get it working. It took me a full month to do this. I can't post any example code because it's all the client's, but I could give you some further info on this if you are interested.
Either way, if you can't change the server/format of the video then you should prepare for a whole lot of work.
check the below code for showing a url http or rtsp in Video View.
Now before invoking your videoview .. just get
public class VideoViewDemo extends Activity {
/**
* TODO: Set the path variable to a streaming video URL or a local media
* file path.
*/
private String path = "";
private VideoView mVideoView;
#Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.videoview);
mVideoView = (VideoView) findViewById(R.id.surface_view);
if (path == "") {
// Tell the user to provide a media file URL/path.
Toast.makeText(
VideoViewDemo.this,
"Please edit VideoViewDemo Activity, and set path"
+ " variable to your media file URL/path",
Toast.LENGTH_LONG).show();
} else {
/*
* Alternatively,for streaming media you can use
* mVideoView.setVideoURI(Uri.parse(URLstring));
*/
mVideoView.setVideoPath(path);
mVideoView.setMediaController(new MediaController(this));
mVideoView.requestFocus();
}
}
}
Now since your requirements are different first make http post request to your server ... get the desired input stream.. write that stream as mp4 to your local file system. then you can use below code to play content
// this can be path on file system
String path="/adf.mp4" ;
public void playVideo() {
MediaController mediaController = new MediaController(this);
mediaController.setMediaPlayer(videoView);
videoView.setVideoPath(path);
videoView.setMediaController(mediaController);
videoView.requestFocus();
videoView.start();
mediaController.show();
I tried this and it worked for me :D no rtsp no nothing, just a url with a mp4 video and it played!
public class VideoActivity extends Activity {
public static final String KEY = "video_id";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fotogaleria_video);
Intent i = getIntent();
if(i.hasExtra(KEY)){
String url = "https://sites.google.com/site/tempmp4download/bnm.mp4";
VideoView vv = (VideoView)findViewById(R.id.fotogaleria_video);
// vv.setVideoPath(url);
vv.setVideoURI(Uri.parse(url));
vv.setMediaController(new MediaController(this));
vv.requestFocus();
vv.start();
}
}
}
Look into following links
http://www.softwarepassion.com/android-series-get-post-and-multipart-post-requests/
http://w3mentor.com/learn/java/android-development/android-http-services/performa-a-http-post-request-with-the-httpclient-in-android/
For streaming, I think you have to download the full file, and then show it in the video player.
I am working on android application in which i have play online radio streaming.
i have gone through the media player classes but i don't think is there any method to online streaming of radio. If any know about this please help me.
Thank You.
Vikram
Vikram,
You should be able to achieve this using the MediaPlayer; however, depending on your format it may be difficult. For example, if you're trying to play an online radio stream that uses .pls, or .m3u, you would have to parse that file, and pull out the true URLs to use.
Beyond that, you should be able to use MediaPlayer's create method with a URL to start streaming playback. Keep in mind that if the streams URL redirects (which it likely does) you may have to resolve the URL. A simple way to do this is use HttpURLConnection to open a connection, then connect(), then getURL(). You'll likely need a string url, so call toExternalForm() on the result from getURL().
Additionally, If things aren't working for you with MediaPlayer via URL, you might have to come up with your own buffering mechanism to get the data from the server. That being the case, you can try this tutorial: http://blog.pocketjourney.com/2008/04/04/tutorial-custom-media-streaming-for-androids-mediaplayer/
From what I've read, you should just be able to do:
MediaPlayer mediaPlayer = new MediaPlayer();
mediaPlayer.setDataSource(streamingURL);
mediaPlayer.prepare();
mediaPlayer.start();
to get basic functionality I believe, but I haven't tested it myself.
the easiest way to play a radio channel in android is to use the built in MediaPlayer, however when the datasource is from web the prepare() method takes a long time to execute and you should use prepareAsync() instead to avoid blocking the ui:
player = new MediaPlayer();
player.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
#Override
public void onPrepared(MediaPlayer mediaPlayer) {
player.start();
}
}
});
try {
player.setDataSource(currentChannelUrl);
player.setAudioStreamType(AudioManager.STREAM_MUSIC);
} catch (IOException e) {
Toast.makeText(this, e.getMessage(), Toast.LENGTH_SHORT).show();
e.printStackTrace();
return;
}
player.prepareAsync();