0% found this document useful (0 votes)
12 views11 pages

Ex9 Merged

Uploaded by

Aditya Raj
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)
12 views11 pages

Ex9 Merged

Uploaded by

Aditya Raj
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
You are on page 1/ 11

khushdeep 11212510 A1

Experiment No.-9: Write a program to play songs using services.

Code:

MainActivity.java:
package com.example.ninth;
import androidx.appcompat.app.AppCompatActivity;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.view.View;

public class MainActivity extends AppCompatActivity {


private MediaPlayerService mediaPlayerService;
private boolean isBound = false;

private ServiceConnection serviceConnection = new ServiceConnection() {


@Override
public void onServiceConnected(ComponentName name, IBinder service) {
MediaPlayerService.LocalBinder binder = (MediaPlayerService.LocalBinder) service;
mediaPlayerService = binder.getService();
isBound = true;
}

@Override
public void onServiceDisconnected(ComponentName name) {
isBound = false;
}
};

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

@Override
protected void onStart() {
super.onStart();
Intent intent = new Intent(this, MediaPlayerService.class);
bindService(intent, serviceConnection, Context.BIND_AUTO_CREATE);
}

@Override
protected void onStop() {
super.onStop();
if (isBound) {
unbindService(serviceConnection);
isBound = false;
}
}

public void playMusic(View view) {


if (isBound) {
if (mediaPlayerService.isPlaying()) {
mediaPlayerService.pauseMediaPlayer();
} else {
Intent intent = new Intent(this, MediaPlayerService.class);
startService(intent);
}
}

Subject: Mobile Application Development Lab


khushdeep 11212510 A1
}
public void stopMusic(View view) {
if (isBound) {
mediaPlayerService.stopMediaPlayer();
}
}
}

MediaPlayerService.java
package com.example.ninth;
import android.app.Service;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.Binder;
import android.os.IBinder;
import android.widget.Toast;

public class MediaPlayerService extends Service {


private MediaPlayer mediaPlayer;
private int playbackPosition = 0;
private boolean isPaused = false;

public class LocalBinder extends Binder {


MediaPlayerService getService() {
return MediaPlayerService.this;
}
}

private final IBinder binder = new LocalBinder();

@Override
public IBinder onBind(Intent intent) {
return binder;
}

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
if (mediaPlayer == null) {
mediaPlayer = MediaPlayer.create(getApplicationContext(), R.raw.amrit_bani);
if (mediaPlayer != null) {
mediaPlayer.setLooping(true);
} else {
Toast.makeText(getApplicationContext(), "Failed to create MediaPlayer",
Toast.LENGTH_SHORT).show();
stopSelf(); // Stop the service if MediaPlayer creation fails
return START_NOT_STICKY; // Don't restart the service
}
}
if (isPaused) {
mediaPlayer.seekTo(playbackPosition);
mediaPlayer.start();
Toast.makeText(getApplicationContext(), "Music is Resumed", Toast.LENGTH_SHORT).show();
isPaused = false;
} else {
if (mediaPlayer != null) {
mediaPlayer.start();
Toast.makeText(getApplicationContext(), "Music is Playing", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(getApplicationContext(), "MediaPlayer is not initialized",
Toast.LENGTH_SHORT).show();
}
}

return START_STICKY;
}

Subject: Mobile Application Development Lab


khushdeep 11212510 A1

public boolean isPlaying() {


return mediaPlayer != null && mediaPlayer.isPlaying();
}

@Override
public void onDestroy() {
super.onDestroy();
if (mediaPlayer != null) {
mediaPlayer.stop();
mediaPlayer.release();
mediaPlayer = null;
}
}

public void pauseMediaPlayer() {


if (mediaPlayer != null && mediaPlayer.isPlaying()) {
playbackPosition = mediaPlayer.getCurrentPosition();
mediaPlayer.pause();
isPaused = true;
Toast.makeText(getApplicationContext(), "Music is Paused", Toast.LENGTH_SHORT).show();
}
}

public void stopMediaPlayer() {


if (mediaPlayer != null && mediaPlayer.isPlaying()) {
mediaPlayer.stop();
mediaPlayer.release();
mediaPlayer = null;
isPaused = false;
Toast.makeText(getApplicationContext(), "Music is Stopped", Toast.LENGTH_SHORT).show();
}
}
}

activity_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
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:id="@+id/relativeLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/white"
tools:context=".MainActivity">

<Button
android:id="@+id/btnPlay"
android:layout_width="135dp"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:layout_marginStart="66dp"
android:layout_marginTop="474dp"
android:layout_marginEnd="211dp"
android:layout_marginBottom="209dp"
android:onClick="playMusic"
android:backgroundTint="#A19D9D"
android:fontFamily="@font/cinzel_bold"
android:text="Play/Pause"
android:textColor="@color/black"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
Subject: Mobile Application Development Lab
khushdeep 11212510 A1
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />

<Button
android:id="@+id/btnStop"
android:layout_width="135dp"
android:layout_height="wrap_content"
android:layout_marginTop="474dp"
android:layout_marginEnd="65dp"
android:layout_marginBottom="209dp"
android:backgroundTint="#A19D9D"
android:onClick="stopMusic"
android:fontFamily="@font/cinzel_bold"
android:text="Stop"
android:textColor="@color/black"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent" />

<ImageView
android:id="@+id/imageView2"
android:layout_width="238dp"
android:layout_height="279dp"
android:layout_marginStart="84dp"
android:layout_marginTop="97dp"
android:layout_marginEnd="89dp"
android:layout_marginBottom="355dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="@drawable/oip2" />

</androidx.constraintlayout.widget.ConstraintLayout>

AndroidManifest.xml:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools">
<uses-permission android:name="android.permission.FOREGROUND_SERVICE"/>
<application
android:allowBackup="true"
android:dataExtractionRules="@xml/data_extraction_rules"
android:fullBackupContent="@xml/backup_rules"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.Ninth"
tools:targetApi="31">
<activity
android:name=".MainActivity"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<service
android:name=".MediaPlayerService"
Subject: Mobile Application Development Lab
khushdeep 11212510 A1
android:exported="false" />
<meta-data
android:name="preloaded_fonts"
android:resource="@array/preloaded_fonts" />
</application>

</manifest>

Output:

Subject: Mobile Application Development Lab


khushdeep 11212510 A1

Experiment No.-10: Write a program to draw circles, rectangles and other shapes using canvas.

Code:

MainActivity.java
package com.example.tenth;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;

import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {

private ShapeView shapeView;

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

shapeView = findViewById(R.id.shapeView);

Button buttonCircle = findViewById(R.id.c);


Button buttonRectangle = findViewById(R.id.r);
Button buttonTriangle = findViewById(R.id.t);
Button buttonPolygon = findViewById(R.id.p);

buttonCircle.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
drawCircle();
}
});

buttonRectangle.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
drawRectangle();
}
});
buttonTriangle.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
drawTriangle();
}
});
buttonPolygon.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
drawPolygon();
}
});
}

private void drawCircle() {


shapeView.setShape(ShapeView.Shape.CIRCLE);
}
private void drawTriangle() {
shapeView.setShape(ShapeView.Shape.LINE);
}
private void drawPolygon() {
shapeView.setShape(ShapeView.Shape.POLYGON);
Subject: Mobile Application Development Lab
khushdeep 11212510 A1
}

private void drawRectangle() {


shapeView.setShape(ShapeView.Shape.RECTANGLE);
}
}

activity_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
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:background="@color/white"
tools:context=".MainActivity">

<Button
android:id="@+id/c"
android:layout_width="120dp"
android:layout_height="50dp"
android:layout_marginStart="71dp"
android:layout_marginTop="467dp"
android:layout_marginEnd="220dp"
android:layout_marginBottom="214dp"
android:backgroundTint="#655B50"
android:fontFamily="@font/cinzel_bold"
android:paddingStart="1dp"
android:paddingEnd="1dp"
android:text="Circle"
android:textColor="@color/white"
android:textSize="16sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />

<Button
android:id="@+id/r"
android:layout_width="120dp"
android:layout_height="50dp"
android:layout_marginStart="221dp"
android:layout_marginTop="467dp"
android:layout_marginEnd="71dp"
android:layout_marginBottom="214dp"
android:backgroundTint="#655B50"
android:fontFamily="@font/cinzel_bold"
android:paddingStart="1dp"
android:paddingEnd="1dp"
android:text="Rectangle"
android:textColor="@color/white"
android:textSize="16sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />

<Button
android:id="@+id/t"
android:layout_width="120dp"
android:layout_height="50dp"
android:layout_marginStart="71dp"
android:layout_marginTop="554dp"
android:layout_marginEnd="220dp"
android:layout_marginBottom="127dp"
Subject: Mobile Application Development Lab
khushdeep 11212510 A1
android:backgroundTint="#655B50"
android:fontFamily="@font/cinzel_bold"
android:paddingStart="1dp"
android:paddingEnd="1dp"
android:text="Triangle"
android:textColor="@color/white"
android:textSize="16sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />

<Button
android:id="@+id/p"
android:layout_width="120dp"
android:layout_height="50dp"
android:layout_marginStart="221dp"
android:layout_marginTop="554dp"
android:layout_marginBottom="127dp"
android:backgroundTint="#655B50"
android:fontFamily="@font/cinzel_bold"
android:paddingStart="1dp"
android:paddingEnd="1dp"
android:text="Polygon"
android:textColor="@color/white"
android:textSize="16sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />

<com.example.tenth.ShapeView
android:id="@+id/shapeView"
android:layout_width="389dp"
android:layout_height="350dp"
android:layout_marginStart="11dp"
android:layout_marginTop="14dp"
android:layout_marginEnd="11dp"
android:layout_marginBottom="367dp"
android:background="@color/white"
android:backgroundTint="#FFFFF0"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="1.0" />

</androidx.constraintlayout.widget.ConstraintLayout>

ShapeView.java:
package com.example.tenth;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.widget.Toast;

public class ShapeView extends View {

private Paint paint;


Subject: Mobile Application Development Lab
khushdeep 11212510 A1
private Shape shapeToDraw;

public enum Shape {


CIRCLE, RECTANGLE,LINE,POLYGON
}

public ShapeView(Context context) {


super(context);
init();
}

public ShapeView(Context context, AttributeSet attrs) {


super(context, attrs);
init();
}

private void init() {


paint = new Paint();
paint.setColor(Color.BLUE);
paint.setStyle(Paint.Style.STROKE);
paint.setStrokeWidth(8);
shapeToDraw = Shape.CIRCLE;
}

@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
switch (shapeToDraw) {
case CIRCLE:
drawCircle(canvas);
break;
case RECTANGLE:
drawRectangle(canvas);
break;
case LINE:
drawTriangle(canvas);
break;
case POLYGON:
drawPolygon(canvas);
break;
}
}
private void drawCircle(Canvas canvas){
canvas.drawCircle(510, 459,400, paint);
}

private void drawRectangle(Canvas canvas) {


canvas.drawRect(200, 225, 820, 694, paint);
}
private void drawTriangle(Canvas canvas) {
canvas.drawLine(126, 729, 874, 729, paint);
canvas.drawLine(874, 729, 500, 200, paint);
canvas.drawLine(500, 200, 126, 729, paint);
}
@SuppressLint("Range")
private void drawPolygon(Canvas canvas) {
float centerX = getWidth() / 2f;
float centerY = getHeight() / 2f;
float radius = Math.min(centerX, centerY) - 20;
int numPoints = 6;
float[] points = new float[numPoints * 4];
for (int i = 0; i < numPoints; i++) {
float angle = (float) (i * 2 * Math.PI / numPoints - Math.PI / 2);
float x = centerX + radius * (float) Math.cos(angle);
float y = centerY + radius * (float) Math.sin(angle);
points[i * 4] = x;

Subject: Mobile Application Development Lab


khushdeep 11212510 A1
points[i * 4 + 1] = y;
float nextAngle = (float) ((i + 1) * 2 * Math.PI / numPoints - Math.PI / 2);
float nextX = centerX + radius * (float) Math.cos(nextAngle);
float nextY = centerY + radius * (float) Math.sin(nextAngle);
points[i * 4 + 2] = nextX;
points[i * 4 + 3] = nextY;
}

canvas.drawLines(points, paint);
}
public void setShape(Shape shape) {
shapeToDraw = shape;
invalidate(); // Redraw the view
}
}

AndroidManifest.xml:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools">

<application
android:allowBackup="true"
android:dataExtractionRules="@xml/data_extraction_rules"
android:fullBackupContent="@xml/backup_rules"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.Tenth"
tools:targetApi="31">
<activity
android:name=".MainActivity"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />


</intent-filter>
</activity>
<meta-data
android:name="preloaded_fonts"
android:resource="@array/preloaded_fonts" />
</application>

</manifest>

Subject: Mobile Application Development Lab


khushdeep 11212510 A1
Output:

Subject: Mobile Application Development Lab

You might also like