Task 10
Task 10
ShowchartFlagment.java:-
package com.example.mytask1;
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;
TextView tvSongName,tvStartTime,tvTotalTime;
ImageView ivSongImage;
SeekBar sbSongProgress;
ImageView ivPrevious,ivBackward,ivPlay,ivForword,ivNext;
MediaPlayer mMediaPlayer;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_show_chart, container,
false);
tvSongName = view.findViewById(R.id.tvEntertainmentSongName);
tvStartTime =view.findViewById(R.id.tvEntertainmentSongStartTime);
tvTotalTime = view.findViewById(R.id.tvEntertainmentSongTotalTime);
ivSongImage = view.findViewById(R.id.ivEntertainmentSongImage);
sbSongProgress = view.findViewById(R.id.sbEntertainmentSongProcess);
ivPrevious = view.findViewById(R.id.ivEntertainmentSongPrevious);
ivBackward = view.findViewById(R.id.ivEntertainmentSongBackward);
ivPlay = view.findViewById(R.id.ivEntertainmentSongPlay);
ivForword = view.findViewById(R.id.ivEntertainmentSongForward);
ivNext = view.findViewById(R.id.ivEntertainmentSongNext);
mMediaPlayer =
MediaPlayer.create(getActivity(),songArrayList.get(currentIndex));
ivPlay.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
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;
TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(tTime))));
TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(sTime))));
handler.postDelayed(UpdateSongProgress,1000);
}
});
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;
}
TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(tTime))));
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;
}
TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(tTime))));
TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(sTime))));
handler.postDelayed(UpdateSongProgress,1000);
mMediaPlayer.start();
songDetails();
ivBackward.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 for 5
Second",Toast.LENGTH_SHORT).show();
}
}
});
ivForword.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if ((sTime +bTime)<tTime)
{
sTime = sTime + fTime;
mMediaPlayer.seekTo(sTime);
}
else
{
Toast.makeText(getActivity(),"Cannot Jump Forword for 5
Second",Toast.LENGTH_SHORT).show();
}
}
});
return view;
sTime = mMediaPlayer.getCurrentPosition();
tvStartTime.setText(String.format("%d Min, %dSec",
TimeUnit.MILLISECONDS.toMinutes(sTime),
TimeUnit.MILLISECONDS.toSeconds(sTime) -
TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(sTime))));
sbSongProgress.setProgress(sTime);
handler.postDelayed(this,1000);
songDetails();
if (currentIndex == 0)
{
tvSongName.setText("Chori Kiya re Jiya ");
ivSongImage.setImageResource(R.drawable.song_image);
} else if (currentIndex == 1) {
tvSongName.setText(" Ghungroo ");
ivSongImage.setImageResource(R.drawable.song_image2);
} else if (currentIndex == 2) {
tvSongName.setText("Saathiya");
ivSongImage.setImageResource(R.drawable.song_image3);
}
};
Flagment_show_chart.xml:-
<?xml version="1.0" encoding="utf-8"?>
<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="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
tools:context=".ShowChartFragment"
android:orientation="vertical"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Now Playing..."
android:textSize="17dp"
android:textColor="@color/black"
android:textStyle="bold"
android:fontFamily="@font/abril_fatface"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/tvEntertainmentSongName"
android:text="Chori Kiya re Jiya"
android:textColor="@color/lightred"
android:textStyle="bold"
android:fontFamily="@font/abril_fatface"
android:layout_marginLeft="8sp"
/>
</LinearLayout>
<ImageView
android:layout_width="match_parent"
android:layout_height="250dp"
android:id="@+id/ivEntertainmentSongImage"
android:src="@drawable/song_image"
android:scaleType="fitXY"
android:layout_margin="20dp"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:weightSum="3"
android:layout_margin="16sp"
android:gravity="center"
>
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.50"
android:id="@+id/tvEntertainmentSongStartTime"
android:text="Start Time"
android:textColor="@color/black"
android:textSize="10sp"
android:textStyle="bold"
android:fontFamily="@font/abril_fatface"/>
<SeekBar
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="2"
android:id="@+id/sbEntertainmentSongProcess"
android:thumbTint="@color/blue"
android:progressTint="@color/blue"/>
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.50"
android:id="@+id/tvEntertainmentSongTotalTime"
android:text="Total Time"
android:textColor="@color/black"
android:textSize="10sp"
android:textStyle="bold"
android:fontFamily="@font/abril_fatface"/>
</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:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:id="@+id/ivEntertainmentSongPrevious"
android:src="@drawable/icon_previous"/>
<ImageView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:id="@+id/ivEntertainmentSongBackward"
android:src="@drawable/icon_backward"/>
<ImageView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:id="@+id/ivEntertainmentSongPlay"
android:src="@drawable/icon_play"/>
<ImageView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:id="@+id/ivEntertainmentSongForward"
android:src="@drawable/icon_forward"/>
<ImageView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:id="@+id/ivEntertainmentSongNext"
android:src="@drawable/icon_next"/>
</LinearLayout>
</LinearLayout>
Output:-