Select Page

Android Media Player Code

package tts.indeed.com.mediaplayer;

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

public class MainActivity extends Activity {

MediaController mediaController;
VideoView videoView;
Button openButton;
Uri current;

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

videoView =(VideoView)findViewById(R.id.videoView1);
openButton = (Button) findViewById(R.id.openButton);

//Creating MediaController
mediaController= new MediaController(this);
mediaController.setAnchorView(videoView);

openButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent();

intent.setType(“video/*”);
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent, “Video File to Play”), 0);
}
});
}

public void startPlayback()
{
//specify the location of media file
//Setting MediaController and URI, then starting the videoView
videoView.setMediaController(mediaController);
videoView.setVideoURI(current);
videoView.requestFocus();
videoView.start();
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
if (requestCode == 0)
{
if (resultCode == RESULT_OK)
{
Uri sourceUri = data.getData();
current = sourceUri;
startPlayback();
}
}
}

}

<?xml version=”1.0″ encoding=”utf-8″?>
<android.widget.RelativeLayout
xmlns:android=”http://schemas.android.com/apk/res/android”
xmlns:tools=”http://schemas.android.com/tools”
xmlns:app=”http://schemas.android.com/apk/res-auto”
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
tools:context=”tts.indeed.com.mediaplayer.MainActivity”>

<VideoView
android:id=”@+id/videoView1″
android:layout_width=”match_parent”
android:layout_height=”300dp”
android:layout_below=”@+id/openButton”
android:layout_alignParentLeft=”true”
android:layout_alignParentStart=”true”
android:layout_marginTop=”50dp” />

<Button
android:id=”@+id/openButton”
android:layout_width=”match_parent”
android:layout_height=”wrap_content”
android:layout_alignParentLeft=”true”
android:layout_alignParentStart=”true”
android:layout_alignParentTop=”true”
android:layout_marginTop=”45dp”
android:text=”Open” />

</android.widget.RelativeLayout>

Click here for the files: https://drive.google.com/open?id=1g6p5YcCQptoypHMtIUxMbAFityHQX8iD

Splash Activity Code

Splash Activity will bring an image at the start of the application and after few seconds, it will jump to another activity.

Create a New Project

Go to File -> New -> New Project

Provide a Project name as Splash.

Select Empty Activity.

 

Add Splash Image

Go to Project Structure -> app -> res -> drawable

Right click on drawable -> New -> Batch Drawable Import

Select an Image from the pop-up window and click next/ok.

This will add the image to the drawable folder.

 

package com.example.splash;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;

public class MainActivity extends AppCompatActivity {
/** Duration of wait **/
private final int SPLASH_DISPLAY_LENGTH = 1000;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.activity_main);

/* New Handler to start the Menu-Activity
* and close this Splash-Screen after some seconds.*/
new Handler().postDelayed(new Runnable(){
@Override
public void run() {
/* Create an Intent that will start the Menu-Activity. */
Intent mainIntent = new Intent(MainActivity.this,Splash.class);
MainActivity.this.startActivity(mainIntent);
MainActivity.this.finish();
}
}, SPLASH_DISPLAY_LENGTH);
}

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

<ImageView android:id="@+id/splashscreen" android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:src="@drawable/splash"
android:layout_gravity="center"/>

<TextView android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Hello World, splash"/>

</LinearLayout>