• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps Video file select

cr5315

Android Enthusiast
I'm making an app that plays a video right from the start and I'm trying to figure out how to get the app the choose the video from the res/raw folder. The example in my android book had streaming a .3gp video from a website and that worked, but I can't figure out how to have it choose from the video in the res/raw folder.

Activity.java
Code:
import android.app.Activity;
import android.net.Uri;
import android.os.Bundle;
import android.widget.MediaController;
import android.widget.VideoView;

public class fireActivity 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);
// Set video link (mp4 format )
		Uri video = Uri.parse("www.example.com/video.3gp");
		videoView.setMediaController(mediaController);
		videoView.setVideoURI(video);
		videoView.start();

	}
}

main.xml
Code:
<?xml version="1.0" encoding="utf-8"?>

<LinearLayout android:id="@+id/LinearLayout01"
	android:layout_height="fill_parent" xmlns:android="http://schemas.android.com/apk/res/android"
	android:paddingLeft="2px" android:paddingRight="2px"
	android:paddingTop="2px" android:paddingBottom="2px"
	android:layout_width="fill_parent" android:orientation="horizontal">

	<VideoView android:layout_height="fill_parent"
		android:layout_width="fill_parent" android:id="@+id/VideoView"></VideoView>

</LinearLayout>

I've seen it done before, I just can't remember how.
 
You can load it from the app's resources like this:

Code:
Abstract:
URI videoPath = Uri.parse("android.resource://[packageName]/raw/filename");
//file type not needed!

Example:
Uri videoPath = Uri.parse("android.resource://jamtheman.dk.app.skridt/raw/vejskilte");
 
Thanks that worked!
And then a new problem arises. The video isn't full screen and if I make the video the right size for my phone, it won't work with all phones. Is there any way I could make it full screen?
 
Glad to help (and a click on "Thanks" always warms the heart :D )

When you want to make it full screen, you could use the DisplayMetrics class, from which you can read the screen size of the device your app is running on - setting the new width and height of the video accordingly...

You also should be able to use the Intent.ACTION_VIEW to start it in a separate acvity, but I have never tried that, and you should do some google'ing on it - "Intent.ACTION_VIEW video example" or something should get you on the right way...
 
This solution also worked for me. I spent a number of hours searching the web. None of the other examples I tried worked. Some postings said you could not display video from the /raw folder because it was compressed. Wrong! You just have to get your pathname EXACTLY right. Leave off the extension and include the full class name as indicated in the comments by the original replier, and you should be good to go.
THANK YOU!
 
Back
Top Bottom