How to use Media player in Android Studio – Mobile App Development, Android App Development, iphone App Development class="post-template-default single single-post postid-4193 single-format-standard wp-custom-logo"

How to use Media player in Android Studio

Create a new project in Android Studio

File – New Android Project

How to use Media player in Android Studio
How to use Media player in Android Studio

MediaPlayer:-Android providing Media Player Class to access android in built mediaplayer service like playing audio, video etc.

In order to use MediaPlayer, we have to call static method create() of this class. This method return and instance of MediaPlayer class
MediaPlayer mediaplayer = MediaPlayer.create(this, R.raw.song);

The second parameter is the name of the song that you want to play. You have to make a new folder under “res” folder which name is raw and place the music file into it.

Once you have to created the mediaplayer object you can call some method to start or stop the music. These methods are:-

mediaPlayer.start();
mediaPlayer.pause();

layout of the main activity

lactivity_main.xml

[code lang=”js”]
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:orientation="vertical"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin">

<TextView
android:id="@+id/songName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="songName" />

<ImageView
android:id="@+id/mp3Icon"
android:layout_width="match_parent"
android:layout_height="200dp"
android:layout_margin="30dp"
android:background="#ffffff"
android:padding="30dp"
android:src="@drawable/music" />

<TextView
android:id="@+id/songDuration"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="songDuration" />

<SeekBar
android:id="@+id/seekBar"
android:layout_width="match_parent"
android:layout_height="wrap_content" />

<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="30dp"
android:gravity="center_horizontal"
android:orientation="horizontal">

<ImageButton
android:id="@+id/btn_back"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="14dp"
android:onClick="backforward"
android:src="@android:drawable/ic_media_rew" />

<ImageButton
android:id="@+id/btn_pause"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="14dp"
android:onClick="pause"
android:src="@android:drawable/ic_media_pause" />

<ImageButton
android:id="@+id/btn_Play"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="14dp"
android:onClick="play"
android:src="@android:drawable/ic_media_play" />

<ImageButton
android:id="@+id/btn_forword"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="14dp"
android:onClick="forward"
android:src="@android:drawable/ic_media_ff" />
</LinearLayout>

</LinearLayout>

[/code]

Createing MainActivity

MainActivity.java

[code lang=”js”]
import android.media.MediaPlayer;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.SeekBar;
import android.widget.TextView;
import android.os.Handler;
import java.util.concurrent.TimeUnit;

public class MainActivity extends ActionBarActivity {
private MediaPlayer mediaPlayer;
private TextView songName, songDuration;
private SeekBar seekBar;
private double timeStart = 0, finalTime = 0;
private int forwardTime = 2000, backwardTime = 2000;
private Handler durationHandler = new Handler();

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
songName = (TextView) findViewById(R.id.songName);
songDuration = (TextView) findViewById(R.id.songDuration);
mediaPlayer = MediaPlayer.create(this, R.raw.sample_song);
seekBar = (SeekBar) findViewById(R.id.seekBar);
songName.setText("Song.mp3");
seekBar.setMax((int) finalTime);
seekBar.setClickable(false);
}
private Runnable updateSeekBarTime = new Runnable() {
public void run() {
timeStart = mediaPlayer.getCurrentPosition();
seekBar.setProgress((int) timeStart);
double timeRemaining = finalTime – timeStart;
songDuration.setText(String.format("%d min, %d sec", TimeUnit.MILLISECONDS.toMinutes((long) timeRemaining), TimeUnit.MILLISECONDS.toSeconds((long) timeRemaining) – TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes((long) timeRemaining))));
durationHandler.postDelayed(this, 100);
}
};
public void play(View view) {
mediaPlayer.start();
timeStart = mediaPlayer.getCurrentPosition();
seekBar.setProgress((int) timeStart);
durationHandler.postDelayed(updateSeekBarTime, 100);
}
public void pause(View view) {
mediaPlayer.pause();
}
public void forward(View view) {
if ((timeStart + forwardTime) <= finalTime) {
timeStart = timeStart – backwardTime;
mediaPlayer.seekTo((int) timeStart);
}
}
public void backforward(View view) {
//check if we can go back at backwardTime seconds after song starts
if ((timeStart – backwardTime) > 0) {
timeStart = timeStart – backwardTime;
mediaPlayer.seekTo((int) timeStart);
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
Android Manifest
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="mobilemerit.com.mediaplayer" >
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>

[/code]

How to use Media player in Android Studio


Warning: _() expects exactly 1 parameter, 2 given in /home2/mobilemeri7/public_html/wp-content/themes/mobilemerit/comments.php on line 28

Write a Reply or Comment

Your email address will not be published.


This site uses Akismet to reduce spam. Learn how your comment data is processed.

Crack Accounts admin panel bypass Tiktok hackleme sitesi Nulled Scripts Nulled Script ioncube decoder iphone sızma valorant hackleme Crack script Brute force programı Brute force yapma internetten para kazanma Evde para kazanmanın yolları Öğrenciler için internetten para kazanma yolları valorant combo Brute force programı Açıklı siteler SQL Açıklı siteler CMS Brute Force https://www.ankihlasnakliyat.com/su kaçağı bulmasu tesisatçısıjigolo siteleri arkadaş bulankara nakliyatankara nakliyatistanbul nakliyatgay sohbetbatak oynatavla oynagabile sohbetokey oynaoyunlar oyna #yaslikurt# sex geschichten sex geschichten brazzers rot media instagram banned service Hack haber Hacklink satışı Hacklink Çift faktör kıran telif scripti instagram hackleme sitesi instagram hackleme sitesi Bahis scripti Hack sitesi Crack forum Crack forum Crack Crack Forum instagram hackleme sitesi instagram hesap kapatma instagram hesap kapatma formları instagram hesap kapatma method instagram faktör geçme instagram faktör kırma instagram telif scripti Instagram Telif Scripti 2022 instagram hackleme sitesi apili telif scripti Facebook Temalı Telif Scripti