MainActivity.java
==================
package com.sam.web.mediaplayer;
import android.media.MediaPlayer;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageButton;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity implements View.OnClickListener{
private ImageButton playButton, pauseButton;
private MediaPlayer mediaPlayer;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
playButton=(ImageButton) findViewById(R.id.playButtonId);
pauseButton=(ImageButton) findViewById(R.id.pauseButtonId);
mediaPlayer=MediaPlayer.create(this,R.raw.music_playe);
playButton.setOnClickListener(this);
pauseButton.setOnClickListener(this);
}
@Override
public void onClick(View v) {
if(v.getId()==R.id.playButtonId){
if(mediaPlayer!=null){
mediaPlayer.start();
int duration=mediaPlayer.getDuration()/1000;
Toast.makeText(MainActivity.this,"song play"+duration,Toast.LENGTH_SHORT).show();
}
}
if(v.getId()==R.id.pauseButtonId){
if(mediaPlayer!=null){
mediaPlayer.pause();
Toast.makeText(MainActivity.this,"song paused",Toast.LENGTH_SHORT).show();
}
}
}
@Override
protected void onDestroy() {
if(mediaPlayer!=null && mediaPlayer.isPlaying()){
mediaPlayer.stop();
mediaPlayer.release();
mediaPlayer=null;
}
super.onDestroy();
}
}
==============================================================================
for song duration
mainActivity.java
================
public void showDeails(){
titleTextView.setText(songName[count]);
finalTime=mediaPlayer.getDuration();
durationTextView.setText(String.format("%d:%d,
TimeUnit.MILLISECONDS.toMInutes((long) finalTime),
TimeUnit.MILLISECONDS.toSeconds((long) finalTime),
TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(long)finalTime)
));
}
================================================================================
activity_main.xml
===================
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:orientation="vertical"
tools:context="com.sam.web.mediaplayer.MainActivity">
<LinearLayout
android:layout_weight="1"
android:layout_width="match_parent"
android:layout_height="0dp">
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:src="@drawable/music"
android:layout_weight="1"
android:scaleType="fitXY"/>
</LinearLayout>
<LinearLayout
android:background="#8eecd6"
android:gravity="center"
android:layout_weight="1"
android:layout_width="match_parent"
android:layout_height="0dp">
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/playButtonId"
android:src="@android:drawable/ic_media_play"
/>
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/pauseButtonId"
android:src="@android:drawable/ic_media_pause"
/>
</LinearLayout>
</LinearLayout>
thistimebd Bangladesh Live online newsportal, education, Lifestyle, Health, Photography, gif image etc.
Make your own name or company name website | contact: thistimebd24@gmail.com
Copyright © 2020-2024 News Portal in Bangladesh - THISTIMEBD.COM. ALL Rights Reserved.