I am using a VideoView to play Mp4 video. There are buttons in the screen and on the click event I need to change the video of the VideoView and play. I want this transition to be as smooth as possible. This is the code I was using in the button onclick handler.
Uri uri = Uri.parse("android.resource://"+getPackageName() + "/"+R.raw.myvideo);
mainVideoView.setVideoURI(uri);
mainVideoView.requestFocus();
mainVideoView.start();
I was testing this on my phone with Andorid 4, everything worked fine. But when I tested it in a phone with Android 2.2, I was shocked to see on click of the button the videoview part of the screen becomes black for a second and the new video starts. I cant afford that delay in my application.
Try this code
This is a java code
package com.android.video;
import android.app.Activity;
import android.net.Uri;
import android.os.Bundle;
import android.widget.MediaController;
import android.widget.VideoView;
public class VideoActivity extends Activity {
/** Called when the activity is first created. */
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.setContentView(R.layout.main);
VideoView videoView = (VideoView)this.findViewById(R.id.videoView);
MediaController mc = new MediaController(this);
videoView.setMediaController(mc);
//videoView.setVideoURI(Uri.parse("http://www.mediafire.com/?trc33d4yma3rkv3.mp4"));
videoView.setVideoPath("/sdcard/movie.mp4");
videoView.requestFocus();
videoView.start();
}
}
This is main.xml
<LinearLayout android:layout_width="fill_parent" android:layout_height="fill_parent" xmlns:android="http://schemas.android.com/apk/res/android">
<VideoView android:id="#+id/videoView" android:layout_width="300dp" android:layout_height="300dp" />
</LinearLayout>
This is manifest file
<uses-permission android:name="android.permission.INTERNET" />
Related
Anyone help me. I am preparing application to integrate video into it. Kept video in res/raw folder. Video format .mp4. Working api-level-21. It is working on android devices for android version 2.2 but not on 4+.
Here is my code:
java:
import android.net.Uri;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.widget.MediaController;
import android.widget.VideoView;
public class Gift extends ActionBarActivity {
MediaController mediaController;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.gift);
VideoView myVideoView = (VideoView) findViewById(R.id.videoView);
Uri uri= Uri.parse("android.resource://" + getPackageName() + "/" + R.raw.sidbday);
mediaController = new MediaController(this);
myVideoView.setMediaController(mediaController);
myVideoView.setVideoURI(uri);
myVideoView.start();
}
}
xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<VideoView
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:id="#+id/videoView"/>
</RelativeLayout>
This message is shown when framework is not ready to play video yet. You can start your video after onPrepared method is called:
instead of myVideoView.start(); use
video.setOnPreparedListener(new OnPreparedListener() {
#Override
public void onPrepared(MediaPlayer mp) {
myVideoView.start();
}
});
EDIT
I didn't notice that it's an old topic but maybe it hepls someone.
I am creating video playing app from Youtube. I have extracted Video Data using gdata API and got 3gp url for format 1,6. This i got extracted from Media:Group --> Media:content element.
My Device Info Android 4.0.3 and Model Micromax P350.
These video is working in VLC Player but not in VideoView enter code here in my app. Here is my code:
<LinearLayout
android:id="#+id/LinearLayout01"
android:layout_height="fill_parent"
android:paddingLeft="2px"
android:paddingRight="2px"
xmlns:android="http://schemas.android.com/apk/res/android"
android:paddi`enter code here`ngTop="2px"
android:paddingBottom="2px"
android:layout_width="fill_parent"
android:orientation="vertical">
<VideoView
android:layout_height="fill_parent"
android:layout_width="fill_parent"
android:id="#+id/VideoView" />
</LinearLayout>
MainActivity.java
package com.example.firstapp;
import android.app.Activity;
import android.os.Bundle;
import android.widget.MediaController;
import android.widget.VideoView;
public class MainActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
VideoView videoView = (VideoView)findViewById(R.id.VideoView);
//MediaController mediaController = new MediaController(this);
// mediaController.setAnchorView(videoView);
//videoView.setMediaController(mediaController);
videoView.setVideoPath("rtsp://v1.cache8.c.youtube.com/CiILENy73wIaGQneb1Sj_PGnoRMYDSANFEgGUgZ2aWRlb3MM/0/0/0/video.3gp");
videoView.start();
}
}
Must be your video is not in one of the supported formats:
http://developer.android.com/guide/appendix/media-formats.html
//play rtsp stream
private void PlayRtspStream(String rtspUrl){
videoView.setVideoURI(Uri.parse(rtspUrl));
videoView.requestFocus();
videoView.start();
}
I am trying to play youtube video within video view but getting error(can't play this video) all the time(on device as well as on emulator too). Please help me with this problem.
Thanks in advance.
import android.app.Activity;
import android.net.Uri;
import android.os.Bundle;
import android.widget.VideoView;
public class AndroidVideoView extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
VideoView myVideoView = (VideoView) findViewById(R.id.videoview);
String viewSource = "rtsp://v2.cache8.c.youtube.com/CjYLENy73wIaLQndUnGVjs340xMYDSANFEIJbXYtZ29vZ2xlSARSBXdhdGNoYJP8mrqSz625UAw=/0/0/0/video.3gp";
myVideoView.setVideoURI(Uri.parse(viewSource));
myVideoView.requestFocus();
myVideoView.start();
}
}
1) Videos will not buffer on emulator because it is virtual device. In-order stream video you should use real device
2) you must provide this permission because with out internet connection we will not able to connect with YouTube server
<uses-permission android:name="android.permission.INTERNET" />
3)Please validate your RTSP link. Given below is my rtsp link for my YouTube video
"rtsp://v1.cache6.c.youtube.com/CiILENy73wIaGQnCikhfRzTDsBMYDSANFEgGUgZ2aWRlb3MM/0/0/0/video.3gp";
To generate rtsp link Use this
http://gdata.youtube.com/feeds/mobile/videos/"+Video Id+"
In my case Video Id is sMM0R19IisI
My Code
public class MainActivity extends Activity {
String SrcPath = "rtsp://v1.cache6.c.youtube.com/CiILENy73wIaGQnCikhfRzTDsBMYDSANFEgGUgZ2aWRlb3MM/0/0/0/video.3gp";//"rtsp://v5.cache1.c.youtube.com/CjYLENy73wIaLQnhycnrJQ8qmRMYESARFEIJbXYtZ29vZ2xlSARSBXdhdGNoYPj_hYjnq6uUTQw=/0/0/0/video.3gp";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
VideoView myVideoView = (VideoView)findViewById(R.id.myvideoview);
myVideoView.setVideoURI(Uri.parse(SrcPath));
myVideoView.setMediaController(new MediaController(this));
myVideoView.requestFocus();
myVideoView.start();
}
}
LAYOUT 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/myvideoview"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
</LinearLayout>
I have code for simple audio player on android. But it is not working for videos. Please guide me to write a simple video player.
The code of audio player is
package com.example.helloplayer;
public class HelloPlayer extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
MediaPlayer mp = MediaPlayer.create(this, R.raw.file);
mp.start();
}
}
For making a video player you will have to make use of video view. a sample is shown below
Layout file
<?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>
Here i am playing a video stored in "resource/raw" folder , "one" is the name of video file,you can replace it with name of your video file .also make sure that you are going to play an android supported video format
VideoplayerActivitvity
import android.app.Activity;
import android.net.Uri;
import android.os.Bundle;
import android.widget.MediaController;
import android.widget.Toast;
import android.widget.VideoView;
public class VideoplayerActivity extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
VideoView videoView =(VideoView)findViewById(R.id.videoView);
MediaController mediaController= new MediaController(this);
mediaController.setAnchorView(videoView);
Uri uri=Uri.parse("android.resource://"+getPackageName()+"/"+R.raw.one);
videoView.setMediaController(mediaController);
videoView.setVideoURI(uri);
videoView.requestFocus();
videoView.start();
}
}
In the following code what ami doing wrong the video dosnt seem to play here.Is that the permission issue if so what should be included in the manifest file
this
main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="horizontal" android:baselineAligned="true">
<LinearLayout android:layout_height="match_parent" android:layout_width="wrap_content" android:id="#+id/linearLayout2"></LinearLayout>
<MediaController android:id="#+id/mediacnt" android:layout_width="wrap_content" android:layout_height="wrap_content"></MediaController>
<LinearLayout android:layout_height="match_parent" android:layout_width="wrap_content" android:id="#+id/linearLayout1" android:orientation="vertical"></LinearLayout>
<Gallery android:layout_height="wrap_content" android:id="#+id/gallery" android:layout_width="wrap_content" android:layout_weight="1"></Gallery>
<VideoView android:layout_height="match_parent" android:id="#+id/vv" android:layout_width="wrap_content"></VideoView>
</LinearLayout>
this is java class
package com.gallery;
import java.net.URL;
import android.app.Activity;
import android.app.Activity;
import android.net.Uri;
import android.os.Bundle;
import android.widget.MediaController;
import android.widget.Toast;
import android.widget.VideoView;
public class GalleryActivity extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Toast.makeText(GalleryActivity.this, "Hello world", Toast.LENGTH_LONG).show();
VideoView videoView = (VideoView) findViewById(R.id.vv);
MediaController mediaController = new MediaController(this);
mediaController.setAnchorView(videoView);
// Set video link (mp4 format )
Uri video = Uri.parse("http://www.youtube.com/watch?v=lEbxLDuecHU&playnext=1&list=PL040F3034C69B1674");
videoView.setMediaController(mediaController);
videoView.setVideoURI(video);
videoView.start();
}
}
add the Internet permission to applications' manifest file
<uses-permission android:name="android.permission.INTERNET" />
this link you should see AndroidVideoComponent this also MediaPlayerDemo_Video
If you want to play a video from a URL in the wifi state, you need to add the wifi and network state permissions in the manifest file like this:
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>