Professional Documents
Culture Documents
16-CS-024 Mad13
16-CS-024 Mad13
LAB REPORT # 13
Submitted To:
Submitted By:
Reg No:
16-CS-024
Experiment # 13
Objective
In this Lab, you’ll take a closer look at some of these hardware APIs..
Software Tool
1. Android Studio
Theory
The hardware APIs available in android include:
• ❑ A telephony package that provides access to calls and phone status.
• ❑ A multimedia playback and recording library.
• ❑ Access to the device camera for taking pictures and previewing video.
• ❑ Extensible support for sensor hardware.
• ❑ Accelerometer and compass APIs to monitor orientation and movement.
• ❑ Communications libraries for managing Bluetooth, network, and Wi-Fi hardware
Android provides many ways to control playback of audio/video files and streams. One of this way
is through a class called MediaPlayer.
Android is providing MediaPlayer class to access built-in mediaplayer services like playing
audio,video e.t.c. In order to use MediaPlayer, we have to call a static Method create() of this class.
This method returns an instance of MediaPlayer class. Its syntax is as follows –
The second parameter is the name of the song that you want to play. You have to make a new folder
under your project with name raw and place the music file into it.
Once you have created the Mediaplayer object you can call some methods to start or stop the music.
These methods are listed below.
mediaPlayer.start(); mediaPlayer.pause();
On call to start() method, the music will start playing from the beginning. If this method is called
again after the pause() method, the music would start playing from where it is left and not from the
beginning.
In order to start music from the beginning, you have to call reset() method. Its syntax is given below.–
mediaPlayer.reset();
Here is an example demonstrating the use of MediaPlayer class. It creates a basic media player that
allows you to forward, backward, play and pause a song. Procedure:
Then delete the code which is there and type the code as given below.
MainActivity.XML
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
tools:context=".MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="100dp"
android:text="Audio Video Player"
android:layout_centerHorizontal="true"
android:textColor="@color/colorAccent"
android:textSize="30dp"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="150dp"
android:layout_centerHorizontal="true"
android:text="Play MP3"
android:textSize="50dp"
android:textColor="@color/colorPrimaryDark"
android:id="@+id/but1"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="250dp"
android:layout_centerHorizontal="true"
android:text="Play MP4"
android:textSize="50dp"
android:textColor="@color/colorPrimaryDark"
android:id="@+id/but2"
/>
</RelativeLayout>
Main2_Activity.XML
<?xml version="1.0" encoding="utf-8"?> <Button
<RelativeLayout android:id="@+id/button4"
xmlns:android="http://schemas.android.com/apk/
android:layout_width="66dp"
res/android"
android:layout_height="wrap_content"
xmlns:tools="http://schemas.android.com/tools" android:layout_alignTop="@+id/button3"
android:layout_width="match_parent"
android:layout_marginStart="14dp"
android:layout_height="match_parent"
android:layout_marginLeft="14dp"
tools:context=".MainActivity">
android:layout_marginTop="0dp"
android:layout_marginEnd="0dp"
<TextView android:text="Music Palyer"
android:layout_width="wrap_content" android:layout_toEndOf="@+id/button3"
android:layout_height="wrap_content" android:layout_toRightOf="@+id/button3"
android:textSize="35dp"
android:layout_alignParentTop="true" <SeekBar
android:layout_centerHorizontal="true" /> android:layout_width="wrap_content"
android:layout_height="wrap_content"
<TextView android:id="@+id/seekBar"
android:layout_width="wrap_content" android:layout_alignLeft="@+id/textview"
android:layout_height="wrap_content" android:layout_alignStart="@+id/textview"
android:text="Enjoy the Music" android:layout_alignRight="@+id/textview"
android:id="@+id/textView" android:layout_alignEnd="@+id/textview"
android:layout_below="@+id/textview" android:layout_above="@+id/button" />
android:layout_centerHorizontal="true"
android:textColor="#ff7aff24" <TextView
android:textSize="35dp" /> android:layout_width="wrap_content"
android:layout_height="wrap_content"
<ImageView
android:textAppearance="?android:attr/textAppeara
android:layout_width="wrap_content"
nceSmall"
android:layout_height="wrap_content"
android:text="Small Text"
android:id="@+id/imageView"
android:id="@+id/textView2"
android:layout_below="@+id/textView"
android:layout_above="@+id/seekBar"
android:layout_centerHorizontal="true"
android:layout_toLeftOf="@+id/textView"
android:src="@drawable/nokia"/>
android:layout_toStartOf="@+id/textView" />
<Button
<TextView
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_width="61dp"
android:layout_height="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true" android:textAppearance="?android:attr/textAppeara
nceSmall"
android:layout_alignParentLeft="true"
android:text="Small Text"
android:layout_alignParentBottom="true" android:id="@+id/textView3"
android:layout_marginStart="37dp" android:layout_above="@+id/seekBar"
android:layout_marginLeft="37dp" android:layout_alignRight="@+id/button4"
android:layout_marginBottom="3dp" android:layout_alignEnd="@+id/button4" />
android:text="@string/forward" />
<TextView
<Button android:layout_width="wrap_content"
android:id="@+id/button2" android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:textAppearance="?android:attr/textAppeara
android:layout_height="wrap_content"
nceMedium"
android:text="Medium Text"
android:layout_alignStart="@+id/imageView"
android:id="@+id/textView4"
android:layout_alignLeft="@+id/imageView"
android:layout_alignBaseline="@+id/textView2"
android:layout_alignParentBottom="true"
android:layout_marginStart="110dp"
android:layout_alignBottom="@+id/textView2"
android:layout_marginLeft="110dp"
android:layout_centerHorizontal="true" />
android:layout_marginBottom="2dp"
android:text="@string/pause" />
</RelativeLayout>
<Button
android:id="@+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="@+id/button2"
android:layout_marginStart="-1dp"
android:layout_marginLeft="-1dp"
android:layout_marginTop="-2dp"
android:layout_toEndOf="@+id/button2"
android:layout_toRightOf="@+id/button2"
android:text="@string/back" />
Main3Activity.XML
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout 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"
tools:context=".MainActivity">
<VideoView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/videoView"
android:layout_gravity="center" />
</FrameLayout>
@Override
import android.content.Intent;
public void onClick(View view) {
import
android.support.v7.app.AppCompatActiv Intent i = new
ity; Intent(MainActivity.this,Main2Activity.cla
ss);
import android.os.Bundle;
startActivity(i);
import android.view.View;
}
import android.widget.Button;
});
b2.setOnClickListener(new
public class MainActivity extends View.OnClickListener() {
AppCompatActivity {
@Override
Button b1,b2;
public void onClick(View view) {
@Override
Intent i = new
protected void onCreate(Bundle Intent(MainActivity.this,Main3Activity.cla
savedInstanceState) { ss);
super.onCreate(savedInstanceState); startActivity(i);
}
setContentView(R.layout.activity_main);
});
b1=(Button)
findViewById(R.id.but1); }
b2=(Button) }
findViewById(R.id.but2);
TimeUnit.MILLISECONDS.toMinutes((long)
startTime),
TimeUnit.MILLISECONDS.toSeconds((long)
startTime) -
TimeUnit.MINUTES.toSeconds(TimeUnit.MILLI
SECONDS.
toMinutes((long)
startTime)))
);
seekbar.setProgress((int)startTime);
myHandler.postDelayed(this, 100);
}
};
}
Main3Activity.java
package com.example.finalapp;
import // When video Screen change
android.support.v7.app.AppCompatActivity size.
;
import android.os.Bundle;
// Re-Set the videoView
import android.app.Activity; that acts as the anchor for the
MediaController
import android.app.ProgressDialog;
import android.content.res.Configuration;
mediaController.setAnchorView(videoV
import android.media.MediaPlayer; iew);
import }
android.media.MediaPlayer.OnPreparedList
});
ener;
}
import android.net.Uri;
});
import android.os.Bundle;
import android.util.Log;
}
import android.widget.MediaController;
import android.widget.VideoView;
// Find ID corresponding to the name of
public class Main3Activity extends
the resource (in the directory raw).
AppCompatActivity {
public int
private VideoView videoView;
getRawResIdByName(String resName) {
private int position = 0;
String pkgName =
private MediaController mediaController; this.getPackageName();
@Override // Return 0 if not found.
mediaController = new
MediaController(Main3Activity.this
); // When you change direction of phone,
this method will be called.
// Set the videoView that acts as the
anchor for the MediaController. // It store the state of video (Current
position)
mediaController.setAnchorView(vid
eoView); @Override
} super.onSaveInstanceState(savedInstanc
eState);
try {
// ID of video file.
// Store current position.
int id =
this.getRawResIdByName("myvide
o"); savedInstanceState.putInt("CurrentPositi
on", videoView.getCurrentPosition());
videoView.setVideoURI(Uri.parse("android
.resource://" + getPackageName() + "/" + videoView.pause();
id));
}
} catch (Exception e) {
Log.e("Error", e.getMessage());
e.printStackTrace();
if (position == 0) {
// Get saved position.
videoView.start();
position =
}
savedInstanceState.getInt("CurrentPositi
on");
videoView.seekTo(position);
Conclusion
In this lab we make application on which a mp3 and mp4 files are being played.