Search Tutorials

Sunday 20 October 2013

Play Video or Audio File from sdcard in android

In this Android Tutorial, we will learn how to play Video or Audio file from sdcard. First, Open your main XML file and drag VideoView and a button or paste below code:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools" >

    <VideoView
        android:id="@+id/videoView1"
        android:layout_width="300dp"
        android:layout_height="200dp"
      />

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/videoView1"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="15dp"
        android:text="Play"
        android:onClick="play" />

</RelativeLayout>


Now open your Java file and paste below code:

package coders.hub.com; //your package name

import android.os.Bundle;
import android.view.View;
import android.widget.VideoView;
import android.app.Activity;

public class MainActivity extends Activity {
      VideoView v;

      @Override
      protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);

            v=(VideoView)findViewById(R.id.videoView1);

//give Video or audio path of sdcard here
            v.setVideoPath("/sdcard/Music/BigB.mp4");
      }
      public void play(View view)
      {
            v.start();
      }
 }


Now run your project...only video will display on VideoView if we are playing audio file than nothing will display but we can listen audio.

12 comments:

  1. hi sir i play a video in my laptop so there is no sdcard how i can play a video .
    so ple send the code sir

    ReplyDelete
    Replies
    1. Code is given above. Just go to your mobile storage and create(if not available) Music folder and put any mp4 file with name "BigB.mp4" in this folder. you have done!!!

      Delete
  2. Hii sir, can i get the source code of drawing application??

    ReplyDelete
  3. how to play all video in sd card

    ReplyDelete
    Replies
    1. Show all videos in listview and play by using path of clicked video from list item.

      Delete
  4. it is force to stop ist useless

    ReplyDelete
    Replies
    1. Make sure you have "Music" folder in sdcard and "BigB.mp4" in this folder.

      Delete
  5. how to get all mp3 song file in sd card

    ReplyDelete
    Replies
    1. Brother put all songs to your music folder, and get a link in java using listView, so simply while you click on audio or video it will be played, and check the audio or video format must, (.mp4, .3gp, .avi, etc.)

      Delete
  6. This comment has been removed by the author.

    ReplyDelete
  7. May i get the source code for browsing the SdCard. And then select any video file to play on my videoview ?

    ReplyDelete

Back to Top