0% found this document useful (0 votes)
25 views14 pages

MC 8 8A Shubham

mobile computing

Uploaded by

shubham chougule
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
25 views14 pages

MC 8 8A Shubham

mobile computing

Uploaded by

shubham chougule
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Name of Student :Shubham Chougule

Roll No : 8 LAB Assignment Number : 8

Title of LAB Assignment :


A) Record an audio and play
B) Play a video in Videoview.

DOP : 11/10/2024 DOS: 25/10/2024

CO MAPPED : PO MAPPED: FACULTY MARKS:

SIGNATURE:
Practical No: 9
AIM:To Write a program to record and play audio and video.
A) Record an audio and play
B) Play a video in Videoview.

Theory:

1. Permissions

Audio and Video Permissions: To record audio and video, the application needs access to
the device’s microphone, camera, and storage. In [Link], we specify
RECORD_AUDIO, CAMERA, WRITE_EXTERNAL_STORAGE, and
READ_EXTERNAL_STORAGE permissions.

Runtime Permission Handling: Starting from Android 6.0 (API level 23), permissions must
be requested at runtime. Using [Link]() prompts the user for
these permissions when needed, ensuring the app has the necessary access.

2. MediaRecorder for Audio Recording

MediaRecorder Class: MediaRecorder enables us to record audio and video within an


application. It allows setting various parameters such as the audio source (MIC for
microphone), the output format, and the encoder type.

File Path Management: We set the output file path using


getExternalFilesDir(Environment.DIRECTORY_MUSIC) to store audio in the app’s
external files directory under “Music.” This storage method complies with Android’s scoped
storage, providing isolated storage areas per app.

Recording Lifecycle:

start() initiates the recording.

stop() finalizes and saves the audio file.

release() frees up resources held by the MediaRecorder.

3. MediaPlayer for Audio Playback

MediaPlayer Class: This class handles audio and video playback. In our app, MediaPlayer
accesses the recorded audio file and uses setDataSource() with the file path to prepare it for
playback.

Lifecycle:
prepare() loads the audio file, readying it for playback.

start() initiates playback.

Error Handling: Using try-catch blocks for IOException allows us to handle potential issues
with audio file access and playback errors.

4. MediaStore for Video Recording and Storage

MediaStore and Scoped Storage: Since Android 10, Android introduced scoped storage,
limiting access to external storage for improved security and privacy. MediaStore is a
content provider that abstracts file storage and provides a secure, shared location for media
files.

Setting File Paths by Android Version:

For Android versions below 10, we use


getExternalFilesDir(Environment.DIRECTORY_MOVIES), which stores the video within
the app’s designated external files.

For Android 10+ (API level 29), we use ContentValues with MediaStore to store the video
in the Movies directory. This approach uses a ContentUri, an indirect reference to the file,
ensuring compliance with scoped storage.

5. Handling Activity Results for Video Recording

ActivityResultLauncher: Instead of using the deprecated onActivityResult(), we register an


ActivityResultLauncher with [Link]() to handle
video capture. This captures the result of the MediaStore.ACTION_VIDEO_CAPTURE
intent, which opens the camera app for video recording.

Play Video Using Intent: The video file is accessed using its ContentUri or file path URI
based on the Android version. An intent with action Intent.ACTION_VIEW and MIME
type "video/*" opens the video in an available video player.

Key Concepts

Scoped Storage: Starting with Android 10, scoped storage requires apps to handle external
files through MediaStore APIs or app-specific directories to limit unstructured file access.

Content Providers (MediaStore): Content providers offer a consistent interface to access


media files, allowing secure access to shared media files across different apps, while
managing permissions and storage limitations.
Media APIs: MediaRecorder and MediaPlayer are foundational classes for audio and video
in Android:

MediaRecorder handles capturing audio and video, with controls for setting audio/video
sources, output formats, and encoders.

MediaPlayer supports audio and video playback, with options for preparing media sources
and handling playback lifecycle methods.

Permissions and File Access: Accessing sensitive resources (microphone, camera, and
storage) requires permissions, which are essential to ensure app functionality and security
on Android platforms.

Permission on [Link]

<uses-permission android:name="[Link].RECORD_AUDIO"/>

<uses-permission android:name="[Link]"/>

<uses-permission android:name="[Link].WRITE_EXTERNAL_STORAGE"/>

<uses-permission android:name="[Link].READ_EXTERNAL_STORAGE"/>

Activity_main.xml

<LinearLayout

xmlns:android="[Link]

android:layout_width="match_parent"

android:layout_height="match_parent"

android:orientation="vertical"

android:padding="16dp">

<Button

android:id="@+id/buttonStartAudioRecording"

android:layout_width="wrap_content"

android:layout_height="wrap_content"
android:text="Start Audio Recording" />

<Button

android:id="@+id/buttonStopAudioRecording"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="Stop Audio Recording"

android:layout_marginTop="8dp"/>

<Button

android:id="@+id/buttonPlayAudio"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="Play Audio"

android:layout_marginTop="8dp"/>

<Button

android:id="@+id/buttonStartVideoRecording"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="Start Video Recording"

android:layout_marginTop="16dp"/>

<Button
android:id="@+id/buttonPlayVideo"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="Play Video"

android:layout_marginTop="8dp"/>

</LinearLayout>

[Link]

package [Link];

import [Link];

import [Link];

import [Link];

import [Link];

import [Link];

import [Link];

import [Link];

import [Link];

import [Link];

import [Link];

import [Link];

import [Link];

import [Link];
import [Link];

import [Link];

import [Link];

import [Link];

import [Link];

import [Link];

public class MainActivity extends AppCompatActivity {

private MediaRecorder audioRecorder;

private MediaPlayer mediaPlayer;

private Uri videoUri;

private File audioFile;

private Button buttonStartAudioRecording, buttonStopAudioRecording,


buttonPlayAudio;

private Button buttonStartVideoRecording, buttonPlayVideo;

@Override

protected void onCreate(Bundle savedInstanceState) {

[Link](savedInstanceState);

setContentView([Link].activity_main);

// Request permissions

[Link](this, new String[]{


[Link].RECORD_AUDIO,

[Link],

[Link].WRITE_EXTERNAL_STORAGE,

[Link].READ_EXTERNAL_STORAGE

}, 1000);

// File for audio recording

audioFile = new File(getExternalFilesDir(Environment.DIRECTORY_MUSIC),


"audio_record.3gp");

// Initialize UI elements

buttonStartAudioRecording = findViewById([Link]);

buttonStopAudioRecording = findViewById([Link]);

buttonPlayAudio = findViewById([Link]);

buttonStartVideoRecording = findViewById([Link]);

buttonPlayVideo = findViewById([Link]);

// Set click listeners

[Link](view -> startAudioRecording());

[Link](view -> stopAudioRecording());

[Link](view -> playAudio());

[Link](view -> startVideoRecording());

[Link](view -> playVideo());

}
// Audio Recording

private void startAudioRecording() {

try {

audioRecorder = new MediaRecorder();

[Link]([Link]);

[Link]([Link].THREE_GPP);

[Link]([Link].AMR_NB);

[Link]([Link]());

[Link]();

[Link]();

[Link](this, "Audio recording started", Toast.LENGTH_SHORT).show();

} catch (IOException e) {

[Link]();

[Link](this, "Failed to start audio recording",


Toast.LENGTH_SHORT).show();

private void stopAudioRecording() {

if (audioRecorder != null) {

[Link]();

[Link]();

audioRecorder = null;

[Link](this, "Audio recording stopped", Toast.LENGTH_SHORT).show();


}

private void playAudio() {

mediaPlayer = new MediaPlayer();

try {

[Link]([Link]());

[Link]();

[Link]();

[Link](this, "Playing audio", Toast.LENGTH_SHORT).show();

} catch (IOException e) {

[Link]();

[Link](this, "Failed to play audio", Toast.LENGTH_SHORT).show();

// Video Recording

private void startVideoRecording() {

Intent videoIntent = new Intent(MediaStore.ACTION_VIDEO_CAPTURE);

if ([Link].SDK_INT >= Build.VERSION_CODES.Q) {

ContentValues values = new ContentValues();

[Link]([Link].RELATIVE_PATH, "Movies/MyApp");

[Link]([Link], "video_record");
[Link]([Link].MIME_TYPE, "video/mp4");

videoUri =
getContentResolver().insert([Link].EXTERNAL_CONTENT_URI,
values);

[Link](MediaStore.EXTRA_OUTPUT, videoUri);

} else {

File videoFile = new


File(getExternalFilesDir(Environment.DIRECTORY_MOVIES), "video_record.mp4");

videoUri = [Link](videoFile);

[Link](MediaStore.EXTRA_OUTPUT, videoUri);

[Link](videoIntent);

private final ActivityResultLauncher<Intent> videoCaptureLauncher =

registerForActivityResult(new [Link](),

result -> {

if ([Link]() == RESULT_OK) {

[Link](this, "Video recording saved",


Toast.LENGTH_SHORT).show();

} else {

[Link](this, "Video recording failed",


Toast.LENGTH_SHORT).show();

});
private void playVideo() {

if (videoUri != null) {

Intent playVideoIntent = new Intent(Intent.ACTION_VIEW);

[Link](videoUri, "video/*");

startActivity(playVideoIntent);

} else {

[Link](this, "No video to play", Toast.LENGTH_SHORT).show();

Output:
Conclusion:

Creating an audio and video recording app in Android demonstrates the practical integration
of several Android components, including MediaRecorder for capturing media, MediaPlayer
for playback, and MediaStore for managing media storage, especially on devices with
scoped storage requirements.

You might also like