There are many controls to play youtube videos in eclipse like WebView, YouTubePlayerView, VideoView etc.
My doubt is can we play it using media player? Found that, url having extension like .mp4 can play using media player. But when tried with youtube video url it shows an error Start Called in state 0 .
if(isYTFlag){ //if youtube url
mediaPlayer.setDataSource(videoURL);
}
else{ //local mp4
afd = this.getResources().openRawResourceFd(Integer.parseInt(videoURL));
mediaPlayer.setDataSource(afd.getFileDescriptor(), afd.getStartOffset(), afd.getDeclaredLength());
}
Please help to verify
Here is another working code
MainActivity.java
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_video_view);
VideoView videoView =(VideoView)findViewById(R.id.videoView);
MediaController mediaController= new MediaController(this);
mediaController.setAnchorView(videoView);
Uri uri=Uri.parse("rtsp://r2---sn-a5m7zu76.c.youtube.com/
Ck0LENy73wIaRAnTmlo5oUgpQhMYESARFEgGUg5yZWNvbW1lbmRhdGlvbnIhAWL2kyn64K6aQtkZVJdTxRoO88HsQjpE1a8d1GxQnGDmDA==/0/0/0/video.3gp");
videoView.setMediaController(mediaController);
videoView.setVideoURI(uri);
videoView.requestFocus();
videoView.start();}
activity_video_view.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<VideoView android:id="#+id/videoView"
android:layout_height="fill_parent"
android:layout_width="fill_parent"/>
</LinearLayout>
YouTube Andorid Api
You can play the video using media player easily like below.
Player.java file
public class Player extends Activity implements SurfaceHolder.Callback,
OnPreparedListener {
private MediaPlayer mediaPlayer;
private SurfaceHolder vidHolder;
private SurfaceView vidSurface;
String vidAddress = "https://archive.org/download/ksnn_compilation_master_the_internet/ksnn_compilation_master_the_internet_512kb.mp4";
String videoSrc = "http://bffmedia.com/bigbunny.mp4";
String videoToPlay = "rtsp://v6.cache1.c.youtube.com/CjYLENy73wIaLQkDsLHya4-Z9hMYDSANFEIJbXYtZ29vZ2xlSARSBXdhdGNoYKX4k4uBjbOiUQw=/0/0/0/video.3gp";
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.mediaplayer);
vidSurface = (SurfaceView) findViewById(R.id.surfView);
vidHolder = vidSurface.getHolder();
vidHolder.addCallback(this);
}
#Override
public void onPrepared(MediaPlayer mp) {
// TODO Auto-generated method stub
mediaPlayer.start();
}
#Override
public void surfaceCreated(SurfaceHolder holder) {
// TODO Auto-generated method stub
try {
mediaPlayer = new MediaPlayer();
mediaPlayer.setDisplay(vidHolder);
mediaPlayer.setDataSource(videoToPlay);
mediaPlayer.prepare();
mediaPlayer.setOnPreparedListener(this);
mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
} catch (Exception e) {
e.printStackTrace();
}
}
#Override
public void surfaceChanged(SurfaceHolder holder, int format, int width,
int height) {
// TODO Auto-generated method stub
}
#Override
public void surfaceDestroyed(SurfaceHolder holder) {
// TODO Auto-generated method stub
}
}
//mediaplayer.xml file
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#000000"
android:paddingBottom="10dp" >
<SurfaceView
android:id="#+id/surfView"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
</RelativeLayout>
//finally add internet permission in manifest.xml file
<uses-permission android:name="android.permission.INTERNET" />
Have you tried this?
btnPlay.setOnClickListener(new OnClickListener() {public void onClick(View v) {
startActivity(new Intent(Intent.ACTION_VIEW,Uri.parse("https://www.youtube.com/watch?v=jHHn52KhBkQ")));
}}});
In this solution you can directly pass the youtube video url to the Intent which we want to play into media player.
For playing video try this link Playing(Streaming) video with MediaPlayer
Use your videoUrl instead of their 3gp video url.
Related
The problem is video is not play at all , but when i use http://www.youtubemaza.com/files/data/366/Tom%20And%20Jerry%20055%20Casanova%20Cat%20(1951).mp4 (example) the video player works good.
I'm using json to get url
https://api.vid.me/videos/featured?limit=1
(Current Url https://api.vid.me/video/18175470/stream?format=dash)
public class MediaPlayerActivity extends AppCompatActivity {
private VideoView videoView;
private int position = 0;
private MediaController mediaController;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_video_player);
videoView = (VideoView) findViewById(R.id.videoView);
videoView.setVisibility(View.VISIBLE);
// Set the media controller buttons
if (mediaController == null) {
mediaController = new MediaController(MediaPlayerActivity.this);
// Set the videoView that acts as the anchor for the MediaController.
mediaController.setAnchorView(videoView);
// Set MediaController for VideoView
videoView.setMediaController(mediaController);
}
try {
String videoUrl="https://api.vid.me/video/18175470/stream?format=dash"; // dash
Uri video = Uri.parse(videoUrl);
videoView.setVideoURI(video);
} catch (Exception e) {
Log.e("Error", e.getMessage());
e.printStackTrace();
}
videoView.requestFocus();
// When the video file ready for playback.
videoView.setOnPreparedListener(new OnPreparedListener() {
public void onPrepared(MediaPlayer mediaPlayer) {
videoView.seekTo(position);
if (position == 0) {
videoView.start();
}
// When video Screen change size.
mediaPlayer.setOnVideoSizeChangedListener(new MediaPlayer.OnVideoSizeChangedListener() {
#Override
public void onVideoSizeChanged(MediaPlayer mp, int width, int height) {
// Re-Set the videoView that acts as the anchor for the MediaController
mediaController.setAnchorView(videoView);
}
});
}
});
}
// When you change direction of phone, this method will be called.
// It store the state of video (Current position)
#Override
public void onSaveInstanceState(Bundle savedInstanceState) {
super.onSaveInstanceState(savedInstanceState);
// Store current position.
savedInstanceState.putInt("CurrentPosition", videoView.getCurrentPosition());
videoView.pause();
}
// After rotating the phone. This method is called.
#Override
public void onRestoreInstanceState(Bundle savedInstanceState) {
super.onRestoreInstanceState(savedInstanceState);
// Get saved position.
position = savedInstanceState.getInt("CurrentPosition");
videoView.seekTo(position);
}
}
xml file
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<!--<com.google.android.exoplayer2.ui.SimpleExoPlayerView
android:id="#+id/videoView"
android:layout_width="match_parent"
android:layout_height="match_parent"/>-->
<SurfaceView
android:id="#+id/surface_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:visibility="gone"/>
<VideoView
android:id="#+id/videoView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:visibility="gone"/>
</FrameLayout>
The only way I found was to use a third party library (however this one is done by Google so not sure what you'd call it really...)
http://google.github.io/ExoPlayer/
It has very good documentation and even sample code on how to use it with DASH video sources!
I want to play a video with MediaPlayer in android, code is something like this:
MediaPlayer mMediaPlayer = new MediaPlayer()
mMediaPlayer.setDataSource(this.getContext, mUri, mHeaders)
I have a local file: /storage/emulated/0/Movies/test.mp4, how can I get the mUri of this file?
In fact, android.net.Uri have a method fromFile(File file), it returns a Uri like this: file:///storage/emulated/0/Movies/test.mp4. But this does work in setDataSource() method.
UPDATED:
I don't want to misleading someone in this problem.
In fact, Uri.parse("/storage/emulated/0/Movies/test.mp4") and Uri.parse("file:///storage/emulated/0/Movies/test.mp4") should be ok in my situation. But it is just my mp4 file's problem!
just change your file:/... to file:/// and it will work
Uri.parse(uri.toString().replace("file:/", "file:///"));
or simply if you have a file try
Uri.parse(String.valueOf(Uri.fromFile(myFile)));
Actually the thing is that the media player requires MRL followed by your path to file
EDIT
I have answered a question on how to initialise MediaPlayer using Uri here
Use the above MRL and initialise your player as suggested and enjoy your video :)
UPDATE:
I thought I should share part of my implementation so that it may help you out:
1st
Add below line to your manifest
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
2nd
Make your activity look like
public class MyActivity extends Activity implements SurfaceHolder.Callback, MediaPlayer.OnPreparedListener, MediaPlayer.OnErrorListener {
private SurfaceView mSurfaceView;
private SurfaceHolder mSurfaceHolder;
private MediaPlayer mMediaPlayer;
/**
* Called when the activity is first created.
*/
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
getWindow().setFormat(PixelFormat.UNKNOWN);
mSurfaceView = (SurfaceView) findViewById(R.id.surfaceview);
mSurfaceHolder = mSurfaceView.getHolder();
mSurfaceHolder.addCallback(this);
mSurfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
}
#Override
public void surfaceCreated(SurfaceHolder holder) {
String stringPath = "/storage/emulated/0/Movies/Lesson%201/30%20-%20Create%20Some%20Fake%20Data.mp4";
stringPath = android.net.Uri.parse("file://" + stringPath).getPath();
try {
mMediaPlayer = new MediaPlayer();
mMediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
mMediaPlayer.setOnErrorListener(this);
mMediaPlayer.setOnPreparedListener(this);
mMediaPlayer.setDataSource(stringPath);
mMediaPlayer.setDisplay(holder);
mMediaPlayer.prepareAsync();
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
#Override
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
}
#Override
public void surfaceDestroyed(SurfaceHolder holder) {
}
#Override
public void onPrepared(MediaPlayer mp) {
mp.start();
}
#Override
public boolean onError(MediaPlayer mp, int what, int extra) {
return false;
}
}
and for testing your xml can be like
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<SurfaceView
android:id="#+id/surfaceview"
android:layout_width="300dp"
android:layout_height="169dp"
/>
</LinearLayout>
Try This
String filePath = "file:///storage/emulated/0/Movies/test.mp4"
filePath.replace("file:///", "/"));
MediaPlayer player = MediaPlayer.create(this, Uri.parse(filePath));
player.setOnPreparedListener(new OnPreparedListener() {
#Override
public void onPrepared(MediaPlayer mp) {
mp.start();
}
});
I'm offering another answer to the original question, which may not be applicable to the specific problem as described by #roger later on. I have found that there is no need to further process/parse the media file path. Just use it directly. For example, I have a locally-stored media file on my Samsung Galaxy phone: "/storage/emulated/0/Samsung/Music/Over_the_Horizon.mp3"
. The following simple code works for playing the file:
String url = "/storage/emulated/0/Samsung/Music/Over_the_Horizon.mp3";
MediaPlayer mPlayer = new MediaPlayer();
mPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
mPlayer.setDataSource(url);
mPlayer.prepare();
mPlayer.start();
(I have omitted try-catch blocks and other non-basic code). If SecurityException or IOException is issued while running the code, most likely a user permission needs to be added at the right place inside AndroidManifest.xml file:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
<application
</application>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
</manifest>
I didn't use a real device, I was working on emulator. (Problem Solved)
I am developing a live stream video application. I have so many live stream URLs which have *.m3u8 postfix.
When I start the app, I can hear only sound but no video displaying. Video display area is always dark.
I am waiting for your helpful suggestions to solve my problem.
Regards.
Some of my code:
public class MainActivity extends Activity {
private static ProgressDialog progressDialog;
String videourl = "URL/playlist.m3u8";
VideoView videoView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
videoView = (VideoView) findViewById(R.id.video_View);
progressDialog = ProgressDialog.show(MainActivity.this, "", "Buffering video...", true);
progressDialog.setCancelable(false);
PlayVideo();
}
private void PlayVideo() {
try {
getWindow().setFormat(PixelFormat.TRANSLUCENT);
MediaController mediaController = new MediaController(MainActivity.this);
mediaController.setAnchorView(videoView);
Uri video = Uri.parse(videourl);
videoView.setMediaController(mediaController);
videoView.setVideoURI(video);
videoView.requestFocus();
videoView.setVisibility(View.VISIBLE);
videoView.setOnPreparedListener(new OnPreparedListener() {
public void onPrepared(MediaPlayer mp) {
progressDialog.dismiss();
videoView.start();
}
});
} catch (Exception e) {
progressDialog.dismiss();
System.out.println("Video Play Error :" + e.toString());
finish();
}
}
}
layout:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<VideoView
android:id="#+id/video_View"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
</VideoView>
</LinearLayout>
I have a MediaPlayerActivity with the following code:
This code basically tries to get a video stream from a http url and load it but for some reason it keeps crashing.
public class MediaPlayerActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.setContentView(R.layout.video_player);
SurfaceView v = (SurfaceView) findViewById(R.id.surface_video);
SurfaceHolder holder = v.getHolder();
holder.setFixedSize(400,300);
MediaPlayer mp = MediaPlayer.create(this, Uri.parse("http://stream-url.com/playlist.m3u8"));
mp.setDisplay(holder);
//mp.setAudioStreamType(2);
try {
//mp.prepare();
mp.start();
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
video_player.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<SurfaceView android:id="#+id/surface_video"
android:layout_width="250px"
android:layout_height="250px">
</SurfaceView>
<LinearLayout
android:orientation="horizontal"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:padding="10dip"
>
</LinearLayout>
</LinearLayout>
When I go to this activity using the following code it crashes:
Intent myIntent = new Intent(HomeActivity.this, MediaPlayerActivity.class);
HomeActivity.this.startActivity(myIntent);
What am I doing wrong?
Without logs, two suggestions:
try implementing SurfaceHolder.Callback.surfaceCreated().
try using MediaPlayer.create() that accepts SurfaceHolder
Details of (1)
Maybe your surface is not yet created when you call start(). You should use MediaPlayer.setDisplay() and MediaPlayer.start() only after surface is created. To do this, you should add overrideSurfaceHolder.Callback.surfaceCreated()`. For example, your code could look like this.
public class MediaPlayerActivity extends Activity implements SurfaceHolder.Callback {
MediaPlayer mp;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.setContentView(R.layout.video_player);
SurfaceView v = (SurfaceView) findViewById(R.id.surface_video);
SurfaceHolder holder = v.getHolder();
holder.setFixedSize(400,300);
holder.addCallback(this).
mp = MediaPlayer.create(this, Uri.parse("http://stream-url.com/playlist.m3u8"));
#Override
public void surfaceCreated(SurfaceHolder holder) {
mp.setDisplay(holder);
try {
mp.start();
} catch (IllegalStateException e) {
e.printStackTrace();
}
}
}
Details of (2)
There seems to be other MediaPlayer.create() that accepts SurfaceHolder as one of the arguments - you could try it: http://developer.android.com/reference/android/media/MediaPlayer.html#create(android.content.Context, android.net.Uri, android.view.SurfaceHolder)
I have a problem with playing video in mediaplayer. I have a sound but no video. I tried everything and can't find solve. Or maybe you know how to play sdp file in videoview? Here's the code:
public class TestStream1 extends Activity implements Callback{
MediaPlayer mMediaPlayer;
String SrcPath = "rtsp://184.72.239.149/vod/mp4:BigBuckBunny_175k.mov";
private SurfaceView mPreview;
private SurfaceHolder holder;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mPreview = (SurfaceView) findViewById(R.id.surface);
holder = mPreview.getHolder();
holder.addCallback(TestStream1.this);
holder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
holder.setFixedSize(400,300);
mMediaPlayer = new MediaPlayer();
try{
mMediaPlayer.setDataSource(SrcPath);
mMediaPlayer.setDisplay(holder);
mMediaPlayer.prepare();
mMediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
mMediaPlayer.start();
}catch(Exception e){
Toast .makeText( TestStream1.this, "Fail", Toast.LENGTH_LONG).show();
}
}
#Override
public void surfaceChanged(SurfaceHolder holder, int format, int width,
int height) {
// TODO Auto-generated method stub
}
#Override
public void surfaceCreated(SurfaceHolder holder) {
// TODO Auto-generated method stub
}
#Override
public void surfaceDestroyed(SurfaceHolder holder) {
// TODO Auto-generated method stub
}
}
EDIT: layout file
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/hello" />
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<SurfaceView
android:id="#+id/surface"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
</LinearLayout>
</LinearLayout>
Some videos cannot be played in mediaplayer, if it doesn't have the codec for its a/v or it is too complex or badly interleaved (Look at Logcat for signs about some). Could be one or more reasons. To make sure the mediaplayer can play the type of video you are asking it to play, save a video file of the same a/v codec and extension to your sd card and then try to play it using the default media player.
Meanwile, try using a videoView to check the video can be played.Your code seems all right.
Try this code :
package a.b;
public class TestStream1 extends Activity implements Callback{
MediaPlayer mMediaPlayer;
String SrcPath = "rtsp://184.72.239.149/vod/mp4:BigBuckBunny_175k.mov";
private SurfaceView mPreview;
private SurfaceHolder holder;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mPreview = (SurfaceView) findViewById(R.id.surface);
holder = mPreview.getHolder();
holder.addCallback(TestStream1.this);
holder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
holder.setFixedSize(400,300);
mMediaPlayer = new MediaPlayer();
}
#Override
public void surfaceChanged(SurfaceHolder holder, int format, int width,
int height) {
// TODO Auto-generated method stub
}
#Override
public void surfaceCreated(SurfaceHolder holder) {
// TODO Auto-generated method stub
try{
mMediaPlayer.setDisplay(holder);
mMediaPlayer.setDataSource(SrcPath);
mMediaPlayer.prepare();
mMediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
mMediaPlayer.start();
}catch(Exception e){
Toast .makeText( TestStream1.this, "Fail", Toast.LENGTH_LONG).show();
}
}
#Override
public void surfaceDestroyed(SurfaceHolder holder) {
// TODO Auto-generated method stub
mMediaPlayer.release();
}
}
SurfaceHolders are created ansynchronously, so we have to wait until the surface has been created and then assign the returned surface holder object .
And I am using the following layout:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/hello" />
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<SurfaceView
android:id="#+id/surface"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
</LinearLayout>
The above solution should work.
Note sure, but try setting the datasource like:
mMediaPlayer.setDataSource(this, SrcPath);