0% found this document useful (0 votes)
3 views9 pages

Text-to-Speech and Multimedia

The document provides code examples for implementing Text-to-Speech, audio playback, video playback, and Google Maps integration in Android applications. It includes detailed Java code for each functionality, demonstrating how to set up and manage user interactions and media resources. Additionally, it outlines the necessary permissions and steps for using Google Maps, including location updates and marker placement.

Uploaded by

officialhitelo03
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)
3 views9 pages

Text-to-Speech and Multimedia

The document provides code examples for implementing Text-to-Speech, audio playback, video playback, and Google Maps integration in Android applications. It includes detailed Java code for each functionality, demonstrating how to set up and manage user interactions and media resources. Additionally, it outlines the necessary permissions and steps for using Google Maps, including location updates and marker placement.

Uploaded by

officialhitelo03
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/ 9

V2V EdTech LLP

Text-to-Speech, Multimedia and Google Maps

Text-to-Speech
package com.example.app;

import android.app.Activity;
import android.os.Bundle;
import android.speech.tts.TextToSpeech;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import java.util.Locale;
import android.widget.Toast;

public class MainActivity extends Activity {


TextToSpeech t1;
EditText ed1;
Button b1;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ed1=(EditText)findViewById(R.id.editText);
b1=(Button)findViewById(R.id.button);

t1=new TextToSpeech(getApplicationContext(), new TextToSpeech.OnInitListener() {


@Override
public void onInit(int status) {
if(status != TextToSpeech.ERROR) {
t1.setLanguage(Locale.UK);
}
}
});

b1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String toSpeak = ed1.getText().toString();
Toast.makeText(getApplicationContext(), toSpeak,Toast.LENGTH_SHORT).show();
V2V EdTech LLP

t1.speak(toSpeak, TextToSpeech.QUEUE_FLUSH, null);


}
});
}

public void onPause(){


if(t1 !=null){
t1.stop();
t1.shutdown();
}
super.onPause();
}
}

Multimedia - Audio
package com.example.audioexample;

import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.app.Activity;

public class MainActivity extends Activity {

MediaPlayer mediaPlayer;
Button playButton, pauseButton;

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

playButton = findViewById(R.id.play_button);
pauseButton = findViewById(R.id.pause_button);

mediaPlayer = MediaPlayer.create(this, R.raw.mysound); // Load audio from raw folder

playButton.setOnClickListener(new View.OnClickListener() {
@Override
V2V EdTech LLP

public void onClick(View v) {


if (!mediaPlayer.isPlaying()) {
mediaPlayer.start(); // Start playing
}
}
});

pauseButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (mediaPlayer.isPlaying()) {
mediaPlayer.pause(); // Pause playing
}
}
});
}

@Override
protected void onDestroy() {
if (mediaPlayer != null) {
mediaPlayer.release(); // Free media resources
mediaPlayer = null;
}
super.onDestroy();
}
}

MediaPlayer - Video
package com.example.videoplayer;

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

public class MainActivity extends Activity {

VideoView videoView;
V2V EdTech LLP

Button playButton, pauseButton;

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

videoView = findViewById(R.id.video_view);
playButton = findViewById(R.id.play_button);
pauseButton = findViewById(R.id.pause_button);

// Load video from res/raw folder


String videoPath = "android.resource://" + getPackageName() + "/" + R.raw.samplevideo;
Uri uri = Uri.parse(videoPath);
videoView.setVideoURI(uri);

// Play video
playButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
videoView.start();
}
});

// Pause video
pauseButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (videoView.isPlaying()) {
videoView.pause();
}
}
});
}
}
V2V EdTech LLP

Google Maps
Steps
1. Get map fragment and set up callback ➝ onMapReady()
2. Check permission ➝ if granted, build Google API client
3. On connected ➝ request location updates
4. On location changed ➝ update marker

// 1. Initialize Map Fragment


// 2. Set up OnMapReadyCallback
// 3. Handle permissions
// 4. Connect Google API
// 5. Request location updates
// 6. Add marker and move camera

activity_main.xml
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:map="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/map"
android:name="com.google.android.gms.maps.SupportMapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="example.com.mapexample.MapsActivity" />

AndroidManifest.xml
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.INTERNET" />

MainActivity.java
import android.os.Build;
import android.support.v4.app.FragmentActivity;
import android.os.Bundle;
import android.Manifest;
import android.content.pm.PackageManager;
import android.location.Location;
import android.support.v4.content.ContextCompat;
V2V EdTech LLP

import com.google.android.gms.common.api.GoogleApiClient;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.OnMapReadyCallback;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.BitmapDescriptorFactory;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.Marker;
import com.google.android.gms.maps.model.MarkerOptions;
import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.location.LocationServices;
import com.google.android.gms.location.LocationListener;
import com.google.android.gms.location.LocationRequest;

public class MapsActivity extends FragmentActivity implements


OnMapReadyCallback,
LocationListener,
GoogleApiClient.ConnectionCallbacks,
GoogleApiClient.OnConnectionFailedListener {

private GoogleMap mMap;


Location mLastLocation;
Marker mCurrLocationMarker;
GoogleApiClient mGoogleApiClient;
LocationRequest mLocationRequest;

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

SupportMapFragment mapFragment = (SupportMapFragment)


getSupportFragmentManager().findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
}

@Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
V2V EdTech LLP

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {


if (ContextCompat.checkSelfPermission(this,
Manifest.permission.ACCESS_FINE_LOCATION) ==
PackageManager.PERMISSION_GRANTED) {
buildGoogleApiClient();
mMap.setMyLocationEnabled(true);
}
} else {
buildGoogleApiClient();
mMap.setMyLocationEnabled(true);
}
}

protected synchronized void buildGoogleApiClient() {


mGoogleApiClient = new GoogleApiClient.Builder(this)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.addApi(LocationServices.API)
.build();

mGoogleApiClient.connect();
}

@Override
public void onConnected(Bundle bundle) {
mLocationRequest = new LocationRequest();
mLocationRequest.setInterval(1000);
mLocationRequest.setFastestInterval(1000);

mLocationRequest.setPriority(LocationRequest.PRIORITY_BALANCED_POWER_ACCURA
CY);

if (ContextCompat.checkSelfPermission(this,
Manifest.permission.ACCESS_FINE_LOCATION)
== PackageManager.PERMISSION_GRANTED) {
LocationServices.FusedLocationApi.requestLocationUpdates(
mGoogleApiClient,
mLocationRequest,
this
);
V2V EdTech LLP

}
}

@Override
public void onConnectionSuspended(int i) {
// Can be used to reconnect or show a message
}

@Override
public void onLocationChanged(Location location) {
mLastLocation = location;

if (mCurrLocationMarker != null) {
mCurrLocationMarker.remove();
}

// Place current location marker


LatLng latLng = new LatLng(location.getLatitude(), location.getLongitude());
MarkerOptions markerOptions = new MarkerOptions()
.position(latLng)
.title("Current Position")

.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_GREEN));
mCurrLocationMarker = mMap.addMarker(markerOptions);

// move map camera


mMap.moveCamera(CameraUpdateFactory.newLatLng(latLng));
mMap.animateCamera(CameraUpdateFactory.zoomTo(11));

// stop location updates


if (mGoogleApiClient != null) {
LocationServices.FusedLocationApi.removeLocationUpdates(mGoogleApiClient, this);
}
}

@Override
public void onConnectionFailed(ConnectionResult connectionResult) {
// Handle failed connection
}
}
V2V EdTech LLP

All the Best!


With Regards
Darshan Sir
Team V2V

You might also like