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.

Cracked Accounts Free Netflix Accounts VPN Proxy Shares Hack Programları Hack dökümanları Hack methodsları Wordlist Combolist Warez Scriptler Download Warez Files WhatsApp Numara çekme +90 Abazalardan Para kazanma Telegram Mavi tik alma 2022 Numara sorgulama Azuere Aws Sanal Kart Method WhatsApp vurma Güncel Method 2022 Bedava .Com .Org .Net VDS Premium Host YouTube Sahte Telif Methodu Tıklama hilesi ile para kazanmak Azure Hack method Pubg Mobile Uc Almak Hedef Kişinin herşeyini öğrenmek instagram Telif taslağı 2022 Ölüm Belgesi ile instagram hesap kaptma instagram form ile ebedi hesap kapatma instagram taklitsiz hesap kapatma instagram Çift faktör geçme instagram tiktok hesap çalma method instagram 2 Faktörlü doğrulama geçme instagram hesap kapatma 2022 instagram rever kırmak instagram Revert Kırma Abaza Dayı Methodu Aylık Dolar kazamak 1500 dolar instagram Çalmak istiyorum instagram düşmeyen takipçi instagram hackleme sitesi Whatsapp Kapatma %100 Abaza tokatlayarak para kazanmak Spotify ile para kazanma Günde 100 tl kazanma sınırsız kazanç Yatakta yatarak para kazanma YouTube izlenme arttırme yöntemi Oturduğun yerde para kazanma yöntemi Discord Hack tool Discord hack programları Cerebrus Rat v2 kodları PP Takipçi Çeken Telif Scripti Facebook profil kapatma methodu 2022 instagram yapay ile banned methodu instagram hesap satşı Sosyal medya hesap satışı Sosyal medya hack Leak Forum Crack Forum Cracking Forum Warez Forum Hack Forum illegal Forum Hacking Forum instagram Forum instagram Forumu Hacklink Hack forum php Shell indir Hack haber
https://www.ankihlasnakliyat.com/jigolosahibinden.comankara nakliyatankara nakliyatistanbul nakliyatankara nakliyatJigolodiyarijigolo sitesigerçek jigolo sitesijigolo sitelerimjigolo şirketlerijigolo sitelerijigolo başvurujigolo arayan kadınlara ulaşKocuceKo cuce