0% found this document useful (0 votes)
17 views8 pages

Task 11

Uploaded by

Gauri Nilakhe
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)
17 views8 pages

Task 11

Uploaded by

Gauri Nilakhe
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/ 8

Task 11

<LinearLayout

xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
tools:context=".MusicFragment"
android:orientation="vertical">

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_margin="16sp"
android:gravity="center">

<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/tvSongName"
android:text="Song Name"
android:textColor="@color/black"
android:textSize="20sp"
android:textStyle="bold"
android:fontFamily="serif-monospace"
android:layout_marginLeft="8dp"/>

</LinearLayout>

<ImageView
android:id="@+id/ivSongImage"
android:layout_width="match_parent"
android:layout_height="304dp"
android:layout_margin="20sp"
android:scaleType="fitXY"
android:src="@drawable/music_icon"/>

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:weightSum="3"
android:layout_margin="16sp"
android:layout_gravity="center">

<TextView
android:id="@+id/tvSongStartTime"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.50"
android:text="Start Time"
android:textColor="@color/black"
android:textSize="10sp"
android:textStyle="bold"
android:fontFamily="serif-monospace"/>
<SeekBar
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="2"
android:id="@+id/sbSongProgress"
android:thumbTint="@color/black"
android:progressTint="@color/black"
android:progressBackgroundTint="@color/black"/>

<TextView
android:id="@+id/tvSongTotalTime"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.50"
android:text="Total Time"
android:textColor="@color/black"
android:textSize="10sp"
android:textStyle="bold"
android:fontFamily="serif-monospace"/>

</LinearLayout>

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_margin="16sp"
android:gravity="center"
android:weightSum="5">

<ImageView
android:id="@+id/ivSongPrevious"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:src="@drawable/icon_previous" />

<ImageView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:id="@+id/ivSongBackword"
android:src="@drawable/icon_backword"/>

<ImageView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:id="@+id/ivSongPlay"
android:src="@drawable/icon_play"/>

<ImageView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:id="@+id/ivSongForward"
android:src="@drawable/icon_forward"/>

<ImageView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:id="@+id/ivSongNext"
android:src="@drawable/icon_next"/>

</LinearLayout>

</LinearLayout>

package com.example.task;

import android.media.MediaPlayer;
import android.os.Bundle;

import androidx.fragment.app.Fragment;

import android.os.Handler;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.SeekBar;
import android.widget.TextView;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.concurrent.TimeUnit;

public class MusicFragment extends Fragment {

TextView tvSongName, tvStartTime, tvTotalTime;


ImageView ivSongImage;
SeekBar sbSongProgress;
ImageView ivPrevious, ivBackword, ivPlay, ivForward, ivNext;

private int currentIndex = 0;


MediaPlayer mMediaPlayer;
private static int sTime = 0, tTime = 0, oTime = 0, bTime = 5000, fTime
= 5000;
Handler handler = new Handler();

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_music, container,
false);

tvSongName = view.findViewById(R.id.tvSongName);
tvStartTime = view.findViewById(R.id.tvSongStartTime);
tvTotalTime = view.findViewById(R.id.tvSongTotalTime);
ivSongImage = view.findViewById(R.id.ivSongImage);
sbSongProgress = view.findViewById(R.id.sbSongProgress);
ivPrevious = view.findViewById(R.id.ivSongPrevious);
ivBackword = view.findViewById(R.id.ivSongBackword);
ivPlay = view.findViewById(R.id.ivSongPlay);
ivForward = view.findViewById(R.id.ivSongForward);
ivNext = view.findViewById(R.id.ivSongNext);

ArrayList<Integer> songArrayList = new ArrayList<>();


songArrayList.add(0,R.raw.chaleya);
songArrayList.add(1,R.raw.rasiya);

mMediaPlayer =
MediaPlayer.create(getActivity(),songArrayList.get(currentIndex));

ivPlay.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (mMediaPlayer != null && mMediaPlayer.isPlaying()){
mMediaPlayer.pause();
ivPlay.setImageResource(R.drawable.icon_play);
} else {
mMediaPlayer.start();
ivPlay.setImageResource(R.drawable.icon_pause);
}

tTime = mMediaPlayer.getDuration();
sTime = mMediaPlayer.getCurrentPosition();

if (oTime == 0) {

sbSongProgress.setMax(tTime);
oTime = 1;
}

tvTotalTime.setText(String.format("%d:%d",
TimeUnit.MILLISECONDS.toMinutes(tTime),
TimeUnit.MILLISECONDS.toSeconds(tTime),

TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(tTime))));

tvStartTime.setText(String.format("%d:%d",
TimeUnit.MILLISECONDS.toMinutes(sTime),
TimeUnit.MILLISECONDS.toSeconds(sTime),

TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(sTime))));

handler.postDelayed(UpdateSongProgress, 1000);

songDetails();

}
});

sbSongProgress.setOnSeekBarChangeListener(new
SeekBar.OnSeekBarChangeListener() {
@Override
public void onProgressChanged(SeekBar seekBar, int progress,
boolean fromUser) {
if (fromUser) {
mMediaPlayer.seekTo(progress);
sbSongProgress.setProgress(progress);
}
}

@Override
public void onStartTrackingTouch(SeekBar seekBar) {

@Override
public void onStopTrackingTouch(SeekBar seekBar) {

}
});

ivPrevious.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (currentIndex > 0) {
currentIndex--;
} else {
currentIndex = songArrayList.size() - 1;
}

if (mMediaPlayer.isPlaying()) {
mMediaPlayer.stop();
}

if (mMediaPlayer != null){
ivPlay.setImageResource(R.drawable.icon_pause);
}

mMediaPlayer = MediaPlayer.create(getActivity(),
songArrayList.get(currentIndex));

tTime = mMediaPlayer.getDuration();
sTime = mMediaPlayer.getCurrentPosition();

oTime = 0;
if (oTime == 0) {

sbSongProgress.setMax(tTime);
oTime = 1;
}

tvTotalTime.setText(String.format("%d:%d",
TimeUnit.MILLISECONDS.toMinutes(tTime),
TimeUnit.MILLISECONDS.toSeconds(tTime),

TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(tTime))));

tvStartTime.setText(String.format("%d:%d",
TimeUnit.MILLISECONDS.toMinutes(sTime),
TimeUnit.MILLISECONDS.toSeconds(sTime),
TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(sTime))));

handler.postDelayed(UpdateSongProgress, 1000);
mMediaPlayer.start();
songDetails();
}
});

ivNext.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (currentIndex < songArrayList.size() - 1) {
currentIndex++;
} else {
currentIndex = 0;
}

if (mMediaPlayer.isPlaying()) {
mMediaPlayer.stop();
}

if (mMediaPlayer != null){
ivPlay.setImageResource(R.drawable.icon_pause);
}

mMediaPlayer = MediaPlayer.create(getActivity(),
songArrayList.get(currentIndex));

tTime = mMediaPlayer.getDuration();
sTime = mMediaPlayer.getCurrentPosition();

oTime = 0;
if (oTime == 0) {

sbSongProgress.setMax(tTime);
oTime = 1;
}

tvTotalTime.setText(String.format("%d:%d",
TimeUnit.MILLISECONDS.toMinutes(tTime),
TimeUnit.MILLISECONDS.toSeconds(tTime),

TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(tTime))));

tvStartTime.setText(String.format("%d:%d",
TimeUnit.MILLISECONDS.toMinutes(sTime),
TimeUnit.MILLISECONDS.toSeconds(sTime),

TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(sTime))));

handler.postDelayed(UpdateSongProgress, 1000);
mMediaPlayer.start();
songDetails();

}
});
ivBackword.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if ((sTime - bTime) > 0){
sTime = sTime - bTime;
mMediaPlayer.seekTo(sTime);
} else {
Toast.makeText(getActivity(), "Cannot jump Backword",
Toast.LENGTH_SHORT).show();
}
}
});

ivForward.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if ((sTime + bTime) < tTime){
sTime = sTime + bTime;
mMediaPlayer.seekTo(sTime);
} else {
Toast.makeText(getActivity(), "Cannot jump Forward",
Toast.LENGTH_SHORT).show();
}
}
});

return view;
}

private void songDetails() {

if (currentIndex == 0) {
tvSongName.setText("Chaleya");
ivSongImage.setImageResource(R.drawable.cover_chaleya
);
} else if (currentIndex == 1) {
tvSongName.setText("Rasiya");
ivSongImage.setImageResource(R.drawable.cover_rasiya);
}
}

private Runnable UpdateSongProgress = new Runnable() {


@Override
public void run() {
sTime = mMediaPlayer.getCurrentPosition();

tvStartTime.setText(String.format("%d:%d",
TimeUnit.MILLISECONDS.toMinutes(sTime),
TimeUnit.MILLISECONDS.toSeconds(sTime),

TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(sTime))));

sbSongProgress.setProgress(sTime);

handler.postDelayed(this,1000);
}
};
}

You might also like