1

I have a list view that is populated from an array of items names. It is set up with List (class) and list_item (layout). Now I set up click listener for listview rows to play video in VideoView which is set up with ClipsActivity (class) activity_clips(layout).

PROBLEM: On clicking the row in ListView next activity(ClipsActivity) loads with the alert "Sorry,this video can not be played". I have the video files in asset folder for sure. I believe - the problem may lie in the way the string is passed/parsed from the ClickListener to the videoview. Hope someone helps me on this.

Code using for List class

package com.abcdef.list;



//import android.app.Activity;
import android.app.ListActivity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
//import android.widget.AdapterView;
//import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
 import android.widget.ListView;
    //import android.widget.TextView;

   public class List extends ListActivity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

// storing string resources into Array
String[] clips_files = getResources().getStringArray(R.array.clips_files);

// Binding resources Array to ListAdapter
this.setListAdapter(new ArrayAdapter<String>(this, R.layout.list_item, R.id.label, clips_files));
  }   
 @Override
 protected void onListItemClick(ListView l, View v, int position, long id) {
super.onListItemClick(l, v, position, id);
change(position);
  }
void change(int position){
Intent intent = new Intent(getApplicationContext(), ClipsActivity.class);
    switch(position){  

    case 0 :
       intent.setData(Uri.parse("file:///android_asset/A1.mp4"));
        break;
    case 1 :
       intent.setData(Uri.parse("file:///android_asset/A2.mp4"));
        break;
    case 2 :
       intent.setData(Uri.parse("file:///android_asset/A3.mp4"));
        break;

    case 3 :
      intent.setData(Uri.parse("file:///android_asset/A4.mp4"));
        break;
    }
        startActivity(intent);
    }  
}

This is the code using for ClipsActivity class with VideoView

package com.abcde.list;

import android.app.Activity;
import android.content.Context;
import android.content.pm.ActivityInfo;
import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Bundle;
import android.widget.MediaController;
import android.widget.VideoView;
import java.net.URI; 


@SuppressWarnings("unused")

public class ClipsActivity extends Activity  {

VideoView mVideoView;
MediaController mc;
// String uriPath;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_clips);
    final VideoView mVideoView = (VideoView)findViewById(R.id.videoview);
     MediaController mc = new MediaController(this);
     mc.setAnchorView(mVideoView);
     mVideoView.setMediaController(mc);


    mVideoView.setVideoPath((getIntent().getDataString()); 
    mVideoView.start(); 
    mVideoView.setOnCompletionListener(
            new MediaPlayer.OnCompletionListener() {
            public void onCompletion(MediaPlayer mp) {
                mVideoView.start();



             }
            });

}



}
Sivon
  • 91
  • 9

1 Answers1

0

Instead of accessing from assets,You must copy the video into your project's res/raw folder. Create raw folder under res folder. It must be in a supported format (3gp, wmv, mp4 ) and named with lower case, numerics, underscores and dots in its filename likewise:video_file.mp4.

VideoView view = (VideoView)findViewById(R.id.videoView);
String path = "android.resource://" + getPackageName() + "/" + R.raw.video_file;
view.setVideoURI(Uri.parse(path));
view.start();
conrad
  • 85
  • 9
  • How should I code in the clicklistener of the list view to set the correct video file for the switch position and how should I set that for vido view?This is interesting, – Sivon Jan 13 '13 at 20:52
  • You should put an extra to your intent to identify which video to play. For example put an integer value to pass it to ClipsActivity intent.putExtra("videoID", videoID); Then get that integer by int id = getIntent().getExtras.getInt("videoID"); – conrad Jan 17 '13 at 21:32