I am trying to play a video through my application.
public class VideoScreen extends Activity {
public MediaPlayer videoMediaPlayer = new MediaPlayer();
public void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.videoscreen);
SurfaceView demoVideo = (SurfaceView) findViewById(R.id.demoVideo);
SurfaceHolder videoHolder = demoVideo.getHolder();
videoMediaPlayer = MediaPlayer.create(this, R.raw.help);
videoMediaPlayer.setDisplay(videoHolder);
videoMediaPlayer.setLooping(false);
videoMediaPlayer.setScreenOnWhilePlaying(true);
videoMediaPlayer.start();
}
}
XML File:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" android:layout_height="fill_parent">
<SurfaceView android:id="#+id/demoVideo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"></SurfaceView>
</FrameLayout>
I can hear only sound, but no video.
But, when I try to play this video separately through file manager. It is playing perfectly. Where I am wrong?
Please help.
I managed to play the video on my phone as well as simulator using VideoView
The changes done were:
Java File:
public class VideoScreen extends Activity {
public void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.videoscreen);
Uri raw_uri=Uri.parse("android.resource://com.binary/"+R.raw.myvideo);
VideoView videoView=(VideoView)findViewById(R.id.demoVideo);
videoView.setVideoURI(raw_uri);
videoView.setMediaController(new MediaController(this));
videoView.setOnCompletionListener(videoOverListener);
videoView.start();
videoView.requestFocus();
}
}
XML File:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" android:layout_height="fill_parent">
<VideoView android:id="#+id/demoVideo"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</VideoView>
</FrameLayout>
Hope this helps.
Related
Hi I'm new to Android studio, I'm having an issue where I'm trying to play a vlc live steam on my phone but I get a message after a few seconds saying "video cannot be played". The strange thing is that video live stream will play on the emulator in android studio. I will add my code below. I have added internet permissions to the manifest also. If anyone could help it would be great, thanks in advance
//XML Code
<LinearLayout xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.pkinsellaweb.BabyMate.VideoScreen"
>
<VideoView
android:id="#+id/videoView"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1" />
</LinearLayout>
//Java Code
public class VideoScreen extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_video_screen);
VideoView video = (VideoView) findViewById(R.id.videoView);
String vidAdd = "http://192.168.43.66:8160";
Uri videoPath = Uri.parse(vidAdd);
video.setVideoURI(videoPath);
MediaController mediaController = new
MediaController(this);
mediaController.setAnchorView(video);
video.setMediaController(mediaController);
video.start();
}
}
I am trying to play video in Videoview from urL but it shows ERROR "Can't play this video"...Here is my code and xml file.I have tried various url listed below.
public class MainActivity extends Activity {
private static final String MOVIE_URL="https://www.youtube.com/watch?v=fv_pIbhKdzY";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
VideoView vid=(VideoView)findViewById(R.id.videoView1);
Uri video=Uri.parse(MOVIE_URL);
vid.setMediaController(new MediaController(this));
vid.setVideoURI(video);
vid.start();
vid.requestFocus();
}
}
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:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context="com.example.vid.MainActivity" >
<VideoView
android:id="#+id/videoView1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true" />
</RelativeLayout>
I have tried these Uri
http://s1133.photobucket.com/albums/m590/Anniebabycupcakez/?action=view& current=1376992942447_242.mp4
https://www.youtube.com/watch?v=fv_pIbhKdzY
https://www.youtube.com/watch?v=OZJalBmtGnQ
Try this
VideoView mVideoView = (VideoView) findViewById(R.id.videoView1);
mVideoView.setMediaController(new MediaController(this));
String viewSource ="https://www.youtube.com/watch?v=fv_pIbhKdzY";
mVideoView.setVideoURI(Uri.parse(viewSource));
Check this video URL3
link2
Other format which you tried might not be supported to play with android application that is the reason you are getting that error.
Since 24 hours I'm trying to set the Anchor view of my MediaControler without success.
My VideoView is embedded in a bigger application but even with a very simple testing app I don't understand the problem.
Here is the code of the onCreate of my testing activity :
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final VideoView vv = (VideoView) findViewById(R.id.videoView1);
vv.setVideoURI(Uri.parse("http://192.168.30.188/test.mp4"));
final MediaController mediaController = new MediaController(ll.getContext());
vv.setMediaController(mediaController);
vv.setOnPreparedListener(new OnPreparedListener() {
#Override
public void onPrepared(MediaPlayer mp) {
mediaController.setAnchorView(vv);
}
});
vv.requestFocus();
}
And here is my simple layout :
<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"
tools:context=".MainActivity" >
<FrameLayout
android:id="#+id/layout1"
android:layout_width="218dp"
android:layout_height="162dp"
android:background="#android:color/holo_purple">
<VideoView
android:id="#+id/videoView1"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</FrameLayout>
</RelativeLayout>
It seems that the media controller appears aligned with the bottom of my video view, but ever horitzontally centered !!!
I had a look at a lot of stack overflow "solutions" (more or less good) or other site samples and I can't understand ...
Can anybody help me please ?
I am developing a school application and I want to play video from my PHP server
I have tried this code but it couldn't play
Uri video = Uri.fromFile(myurl);
Intent intent = new Intent(Intent.ACTION_VIEW); intent.setFlags(Intent.FLAG_ACTIVITY_LAUNCHED_FROM_HISTORY);
intent.setDataAndType(video, "video/*");
try this one, play the video on vedio view through the parse the uri
xml part
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<VideoView
android:id="#+id/surface_view"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_gravity="center"
/>
</FrameLayout>
Now java part
public class HelloInterruptVideoStream extends Activity
{
private String path = "http://dl.dropbox.com/u/145894/t/rabbits.3gp";
private VideoView videoview;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
videoview = (VideoView)findViewById(R.id.surface_view);
getWindow().clearFlags(WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);
videoview.setVideoURI(Uri.parse(path));
videoview.setMediaController(new MediaController(this));
videoview.requestFocus();
videoview.start();
}
}
Use internet permission in menifest file
<uses-permission android:name="android.permission.INTERNET"/>
Try to set your MediaController in this way:
MediaController mediaController= new MediaController(this);
mediaController.setAnchorView(video);
videoview.setMediaController(mediaController);
I'm currently developing android app using eclipse. But, the app crash when after I put videoView. I put onCompletionListener so if the video end it automaticaly jump to main activity. Here it is the my videoview code:
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.view_video);
playVideo();
}
public void playVideo(){
VideoView myVideoView = (VideoView)findViewById(R.id.myvideoview);
setContentView(myVideoView);
Uri video = Uri.parse("android.resource://" + getPackageName() + "/" + R.raw.endingvid);
myVideoView.setVideoURI(video);
myVideoView.setOnCompletionListener(new OnCompletionListener() {
public void onCompletion(MediaPlayer mp) {
jumpMain();
}
});
myVideoView.start();
}
And here it is the 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"
android:layout_gravity="center_vertical|center"
android:background="#drawable/black"
>
<VideoView
android:id="#+id/myvideoview"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
</LinearLayout>
Anyone know what is wrong with my code?
VideoView myVideoView = (VideoView)findViewById(R.id.myvideoview);
setContentView(myVideoView);
You can not reference R.id.myvideoview before you set your Layout with setContentView
setContentView(R.layour.layout_with_videoview);
VideoView myVideoView = (VideoView)findViewById(R.id.myvideoview);