Audio and Video Player

How to add Media Player

MediaPlayer class:  MediaPlayer class can be used to control playback of audio/video files and streams. 

To implement audio sound in the activity write the following piece of code :

MainActivity.java : 

public class MainActivity extends Activity implements OnClickListener{

       MediaPlayer mp;
       Button play;
       Button stop;
       Button pause;
@Override
       protected void onCreate(Bundle savedInstanceState) {
              super.onCreate(savedInstanceState);
              setContentView(R.layout.activity_main);
play = (Button) findViewById(R.id.buttonPlay);
              stop = (Button) findViewById(R.id.buttonStop);
              pause = (Button) findViewById(R.id.buttonPause);
play.setOnClickListener(this);
              stop.setOnClickListener(this);
              pause.setOnClickListener(this);
             
mp = MediaPlayer.create(this, R.raw.abc);  // place a audio mp3 file in raw(create folder in res directory) folder.
}
@Override
       public boolean onCreateOptionsMenu(Menu menu) {
              // Inflate the menu; this adds items to the action bar if it is present.
              getMenuInflater().inflate(R.menu.main, menu);
              return true;
       }

       @Override
       public void onClick(View v) {
              int x = v.getId();
              switch (x) {
              case R.id.buttonPlay:
                     mp.start();
                     break;
              case R.id.buttonStop:
                     mp.stop();
                     break;
              case R.id.buttonPause:
                     mp.pause();
                     break;
              default:
                     break;
              }
             
       }

}


In activity_main.xml :

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

    <Button
        android:id="@+id/buttonPlay"
        style="?android:attr/buttonStyleSmall"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_alignTop="@+id/button2"
        android:layout_marginRight="16dp"
        android:text="Play" />

    <Button
        android:id="@+id/buttonStop"
        style="?android:attr/buttonStyleSmall"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/button1"
        android:layout_alignBottom="@+id/button1"
        android:layout_centerHorizontal="true"
        android:text="stop" />

    <Button
        android:id="@+id/buttonPause"
        style="?android:attr/buttonStyleSmall"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_marginRight="30dp"
        android:layout_toLeftOf="@+id/buttonStop"
        android:text="pause" />

</RelativeLayout>


Right click the project and run as Android application.




How to add Video :

VideoView class:  Displays a video file. The VideoView class can load images from various sources (such as resources or content providers), takes care of computing its measurement from the video so that it can be used in any layout manager, and provides various display options such as scaling and tinting.


To implement video in the activity write the following piece of code :


MainActivity.java : 

public class MainActivity extends Activity implements OnClickListener{

       Button play;
       Button stop;
       Button pause;
      
       VideoView videoView;

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

play = (Button) findViewById(R.id.buttonPlay);
              stop = (Button) findViewById(R.id.buttonStop);
              pause = (Button) findViewById(R.id.buttonPause);
play.setOnClickListener(this);
              stop.setOnClickListener(this);
              pause.setOnClickListener(this);
             
videoView = (VideoView) findViewById(R.id.videoView1);
videoView.setVideoURI(Uri.parse("android.resource://"+getPackageName()+"/"+R.raw.javascript));
       videoView.setMediaController(new MediaController(this));
}
@Override
       public boolean onCreateOptionsMenu(Menu menu) {
              // Inflate the menu; this adds items to the action bar if it is present.
              getMenuInflater().inflate(R.menu.main, menu);
              return true;
       }

       @Override
       public void onClick(View v) {
              int x = v.getId();
              switch (x) {
              case R.id.buttonPlay:
                     mp.start();
                     break;
              case R.id.buttonStop:
                     mp.stop();
                     break;
              case R.id.buttonPause:
                     mp.pause();
                     break;
              default:
                     break;
              }
             
       }

}



In videoviewlay.xml :

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <VideoView
        android:id="@+id/videoView1"
        android:layout_width="wrap_content"
        android:layout_height="186dp" />

</LinearLayout>


Right click the project and run as Android application.







Reach us At: - 0120-4029000 / 24 / 25 / 27 / 29 Mbl: 9953584548
Write us at: - smruti@apextgi.com and pratap@apextgi.com

No comments:

Post a Comment