5,606,201 Members 33,014 Now Online
XDA Developers Android and Mobile Development Forum

Playing videos using mediastore with custom player

Tip us?
 
AmatuerAppDeveloper
Old
#1  
Member - OP
Thanks Meter 7
Posts: 33
Join Date: Jan 2014
Location: Hampshire
Default Playing videos using mediastore with custom player

Not that anyone will actually reply but I'm about to go insane to be honest, ALL i want to do is play a video from a list view using media store and a custom video player (Not stock player!) but so far all my attempts have failed.

So i need some professional help!


My MediaStore code:

Code:
public class VideoManager {
	public VideoManager() {

	}
	private ArrayList<HashMap<String, String>> videoList = new ArrayList<HashMap<String, String>>();

		
	public ArrayList<HashMap<String, String>> getPlayList(Context c) {
	
		

	    /*use content provider to get beginning of database query that queries for all audio by display name, path
	    and mimtype which i dont use but got it incase you want to scan for mp3 files only you can compare with RFC  mimetype for mp3's
	    */
	    final Cursor mCursor = c.getContentResolver().query(
	    		MediaStore.Video.Media.EXTERNAL_CONTENT_URI,
	            new String[] { MediaStore.Video.Media.DISPLAY_NAME, MediaStore.Video.Media.DATA}, null, null,
	            "UPPER(" +  MediaStore.Video.Media.TITLE + ") ASC");

	    String videoTitle = "";
	    String videoPath = "";
	    
	    

	    /* run through all the columns we got back and save the data we need into the arraylist for our listview*/
	    if (mCursor.moveToFirst()) {
	        do {

	        


	            
	            videoTitle = mCursor.getString(mCursor.getColumnIndexOrThrow(MediaStore.Video.Media.DISPLAY_NAME));
	            videoPath = mCursor.getString(mCursor.getColumnIndexOrThrow(MediaStore.Video.Media.DATA));
	            HashMap<String, String> video = new HashMap<String, String>();

	            video.put("videoTitle", videoTitle);
	            video.put("videoPath", videoPath);

	            videoList.add(video);

	        } while (mCursor.moveToNext());
	    }   

	    mCursor.close(); //cursor has been consumed so close it
	return videoList;
	}


	public ArrayList<HashMap<String, String>> getPlayList() {
		// TODO Auto-generated method stub
		return null;
	}
	
	
}

The list code along with an OnItemClick method and Intent:

Code:
public class VideoActivity extends ListActivity {

	 // Songs list
    public ArrayList<HashMap<String, String>> videoList = new ArrayList<HashMap<String, String>>();
    ListView videolist;
     Cursor mCursor;
     int videoTitle;
     int count;
     int videoPath;
 
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.videos);
 
        
        ArrayList<HashMap<String, String>> videoListData = new ArrayList<HashMap<String, String>>();
 
        VideoManager plm = new VideoManager();
        // get all songs from sdcard
        this.videoList = plm.getPlayList(this);
 
        // looping through playlist
        for (int i = 0; i < videoList.size(); i++) {
            // creating new HashMap
            HashMap<String, String> video = videoList.get(i);
 
            // adding HashList to ArrayList
            videoListData.add(video);
        }
 
        // Adding menuItems to ListView
        ListAdapter adapter = new SimpleAdapter(this, videoListData,
                R.layout.video_item, new String[] { "videoTitle" }, new int[] {
                        R.id.videoTitle });
 
        setListAdapter(adapter);
 
        // selecting single ListView item
        ListView lv = getListView();
        // listening to single listitem click
        lv.setOnItemClickListener(new OnItemClickListener() {
 
            @Override
            public void onItemClick(AdapterView<?> parent, View view,
                    int position, long id) {
            	
                // getting listitem index
            	int videoIndex = position;
                // Starting new intent
                Intent in = new Intent(getApplicationContext(),
                       VideoPlayerActivity.class);
                Log.d("TAG","onItemClick");
                // Sending songIndex to PlayerActivity
                in.putExtra("videoPath", videoIndex);
                startActivity(in);
                // Closing PlayListView
                finish();
               
            }
        });
    }
}

And lastly, the players code:

Code:
public class VideoPlayerActivity extends Activity implements SurfaceHolder.Callback, MediaPlayer.OnPreparedListener, VideoControllerView.MediaPlayerControl {

    SurfaceView videoSurface;
    MediaPlayer player;
    VideoControllerView controller;
    private VideoManager videoManager;
    private int currentvideoIndex = 0; 
    private ArrayList<HashMap<String, String>> videoList = new ArrayList<HashMap<String, String>>();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_video_player);
        
        videoSurface = (SurfaceView) findViewById(R.id.videoSurface);
        SurfaceHolder videoHolder = videoSurface.getHolder();
        videoHolder.addCallback(this);

        player = new MediaPlayer();
        videoManager = new VideoManager();
        
        controller = new VideoControllerView(this);
        videoList = videoManager.getPlayList(this);
    }
    
        @Override
        protected void onActivityResult(int requestCode,
                                         int resultCode, Intent data) {
            super.onActivityResult(requestCode, resultCode, data);
            if(resultCode == 0){
             	 currentvideoIndex = data.getExtras().getInt("videoPath");
             	 // play selected song
                 playVideo(currentvideoIndex);
            }
     
        }
        
        public void  playVideo(int videoIndex){
        try {
            player.setDataSource(videoList.get(videoIndex).get("videoPath"));
            player.setOnPreparedListener(this);
        } catch (IllegalArgumentException e) {
            e.printStackTrace();
        } catch (SecurityException e) {
            e.printStackTrace();
        } catch (IllegalStateException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        controller.show();
        return false;
    }

    // Implement SurfaceHolder.Callback
    @Override
    public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
        
    }

    @Override
    public void surfaceCreated(SurfaceHolder holder) {
    	player.setDisplay(holder);
        player.prepareAsync();
    }

    @Override
    public void surfaceDestroyed(SurfaceHolder holder) {
        
    }
    // End SurfaceHolder.Callback

    // Implement MediaPlayer.OnPreparedListener
    @Override
    public void onPrepared(MediaPlayer mp) {
        controller.setMediaPlayer(this);
        controller.setAnchorView((FrameLayout) findViewById(R.id.videoSurfaceContainer));
        try {
			player.prepare();
			player.start();
		} catch (IllegalStateException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
    }
    // End MediaPlayer.OnPreparedListener

    // Implement VideoMediaController.MediaPlayerControl
    @Override
    public boolean canPause() {
        return true;
    }

    @Override
    public boolean canSeekBackward() {
        return true;
    }

    @Override
    public boolean canSeekForward() {
        return true;
    }

    @Override
    public int getBufferPercentage() {
        return 0;
    }

    @Override
    public int getCurrentPosition() {
        return player.getCurrentPosition();
    }

    @Override
    public int getDuration() {
        return player.getDuration();
    }

    @Override
    public boolean isPlaying() {
        return player.isPlaying();
    }

    @Override
    public void pause() {
        player.pause();
    }

    @Override
    public void seekTo(int i) {
        player.seekTo(i);
    }

    @Override
    public void start() {
        player.start();
    }

    @Override
    public boolean isFullScreen() {
        return false;
    }

    @Override
    public void toggleFullScreen() {
        
    }
   

}


Right now PLEASE HELP ME!!
 
EmptinessFiller
Old
#2  
Member
Thanks Meter 21
Posts: 88
Join Date: Sep 2013
Why do you look up the videoPath in onActivityResult? Why do you need that Method? You'd add playVideo(getIntent().getBundle().getIntExtra("vid eopath")); to the end of your onCreate.

Regards
 
AmatuerAppDeveloper
Old
#3  
Member - OP
Thanks Meter 7
Posts: 33
Join Date: Jan 2014
Location: Hampshire
Quote:
Originally Posted by EmptinessFiller View Post
Why do you look up the videoPath in onActivityResult? Why do you need that Method? You'd add playVideo(getIntent().getBundle().getIntExtra("vid eopath")); to the end of your onCreate.

Regards
Hi, thanks for the reply but unfortunately it didn't work.

I assume the code i entered is correct..

Code:
@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_video_player);
        playVideo(getIntent().getExtras().getInt("videoPath"));
	
        
        videoSurface = (SurfaceView) findViewById(R.id.videoSurface);
        SurfaceHolder videoHolder = videoSurface.getHolder();
        videoHolder.addCallback(this);

        player = new MediaPlayer();
        videoManager = new VideoManager();
        
        controller = new VideoControllerView(this);
        videoList = videoManager.getPlayList();
}
    
       
        
        public void  playVideo(int videoIndex){
        try {
            player.setDataSource(videoList.get(videoIndex).get("videoPath"));
            player.setOnPreparedListener(this);
        } catch (IllegalArgumentException e) {
            e.printStackTrace();
        } catch (SecurityException e) {
            e.printStackTrace();
        } catch (IllegalStateException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes