Ashwyn
Ashwyn

Reputation: 669

Android: how to run video from resource folder

I have already seen the other questions similar to mine, but the problem persists. Thanks in advance.

Here's the code

package com.akk.mysecondvideo;

import android.app.Activity;
import android.content.Context;
import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Bundle;
import android.widget.VideoView;

public class MySecondVideo extends Activity {

Context context;
MediaPlayer mp;
/** 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);
    //videoView.setMediaController(mediaController);

    Uri uri = Uri.parse("android.resource://com.akk.mysecondvideo/"
            + R.raw.bommarillu);

    videoView.setVideoURI(uri);

    //mp = new MediaPlayer();
    //mp = MediaPlayer.create(context, R.raw.bommarillu);


    videoView.start(); 
}
}

It shows a force close when i try to run it, and the logcat shows error NULLPOINTEREXCEPTION in line 28 which is videoView.setVideoURI(uri);

VideoView is part of main.xml. The main.xml 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:layout_height="fill_parent"
     android:layout_width="fill_parent" 
     android:id="@+id/VideoView" />
</LinearLayout>

Upvotes: 2

Views: 1735

Answers (3)

Thomas Hecker
Thomas Hecker

Reputation: 1

Try this sample code:

Uri uri = Uri.parse("android.resource://com.akk.mysecondvideo/raw/bommarillu");

Upvotes: 0

slayton
slayton

Reputation: 20319

Did you check to see if uri is null?

Try adding this line before videoView.setVidoeURI(uri) :

if (null == uri)
   Toast.makeText(this, "URI IS NULL", Toast.LENGTH_LONG).show();

If the toast notification shows up then the URI didn't parse correctly

Upvotes: 1

ValayPatel
ValayPatel

Reputation: 1094

I dont see any problem as such. But couple of suggestions I would like to tell you

  • Name the widget in XML different than the widget name e.g myVideoView instead of VideoView
  • videoView.setMediaController(new MediaController(this));
  • videoView.requestFocus();

Check if this works. I have the same code as you I will create a project and will give you my inputs.

Till that time try this

Upvotes: 0

Related Questions