Skip to content

Instantly share code, notes, and snippets.

@SurajBahadur
Created August 11, 2019 17:36
Show Gist options
  • Save SurajBahadur/59afd20db9a9548d009f12d9ebd4fd92 to your computer and use it in GitHub Desktop.
Save SurajBahadur/59afd20db9a9548d009f12d9ebd4fd92 to your computer and use it in GitHub Desktop.
Audio viewing in android
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/white">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:layout_gravity="center_vertical"
android:orientation="vertical"
android:layout_margin="25dp">
<TextView
android:id="@+id/closeView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Close"
android:layout_marginRight="15dp"
android:textStyle="bold"
android:textSize="15sp"
android:layout_gravity="right"/>
<TextView
android:id="@+id/titleView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="15dp"
android:textStyle="bold"
android:textSize="15sp"/>
<LinearLayout
android:id="@+id/seekbarLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:gravity="center"
android:layout_gravity="center_vertical">
<ImageView
android:id="@+id/playIcon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@mipmap/ic_play_blue"/>
<ImageView
android:id="@+id/pauseIcon"
android:visibility="gone"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@mipmap/ic_pause_blue"/>
<SeekBar
android:id="@+id/seekBar"
android:layout_width="180dp"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/time_view"
android:text="00:00"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
</LinearLayout>
</LinearLayout>
package com.test.dialogfragment;
import android.app.Dialog;
import android.app.DialogFragment;
import android.content.DialogInterface;
import android.graphics.Color;
import android.graphics.PorterDuff;
import android.graphics.drawable.ColorDrawable;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.os.Handler;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.SeekBar;
import android.widget.TextView;
import com.narayana.nconnect.R;
import java.io.IOException;
public class AudioDialog extends DialogFragment {
private static final int SECOND = 1000;
private static final int MINUTE = 60 * SECOND;
private static final int HOUR = 60 * MINUTE;
private static final int DAY = 24 * HOUR;
View view;
String url;
String file_name;
SeekBar seekBar;
ImageView playIcon;
ImageView pauseIcon;
int total_duration;
TextView time_view;
TextView closeView;
TextView titleView;
MediaPlayer mediaplayer;
private Handler handler;
public static AudioDialog newInstance(String url, String file_name) {
AudioDialog audioDialog = new AudioDialog();
Bundle bundle = new Bundle();
bundle.putString("url", url);
bundle.putString("file_name", file_name);
audioDialog.setArguments(bundle);
return audioDialog;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Bundle bundle = getArguments();
url = bundle.getString("url");
file_name = bundle.getString("file_name");
}
@Override
public void onDismiss(final DialogInterface dialog) {
super.onDismiss(dialog);
try {
mediaplayer.reset();
} catch (Exception e) {
}
}
/**
* The system calls this only when creating the layout in a dialog.
*/
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
// The only reason you might override this method when using onCreateView() is
// to modify any dialog characteristics. For example, the dialog includes a
// title by default, but your custom layout might not need it. So here you can
// remove the dialog title, but you must call the superclass to get the Dialog.
Dialog dialog = super.onCreateDialog(savedInstanceState);
dialog.setCanceledOnTouchOutside(false);
return dialog;
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
view = inflater.inflate(R.layout.audio_view, container, false);
//backButton = view.findViewById(R.id.backButton);
//titleView = view.findViewById(R.id.titleView);
seekBar = view.findViewById(R.id.seekBar);
playIcon = view.findViewById(R.id.playIcon);
pauseIcon = view.findViewById(R.id.pauseIcon);
time_view = view.findViewById(R.id.time_view);
closeView = view.findViewById(R.id.closeView);
titleView = view.findViewById(R.id.titleView);
seekBar.getProgressDrawable().setColorFilter(Color.DKGRAY, PorterDuff.Mode.MULTIPLY);
try {
titleView.setText(file_name);
} catch (Exception e) {
}
playIcon.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
try {
mediaplayer.start();
playIcon.setVisibility(View.GONE);
pauseIcon.setVisibility(View.VISIBLE);
} catch (Exception e) {
}
}
});
pauseIcon.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
try {
mediaplayer.pause();
pauseIcon.setVisibility(View.GONE);
playIcon.setVisibility(View.VISIBLE);
} catch (Exception e) {
}
}
});
closeView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
try {
mediaplayer.reset();
pauseIcon.setVisibility(View.GONE);
playIcon.setVisibility(View.VISIBLE);
dismiss();
} catch (Exception e) {
}
}
});
handler = new Handler();
mediaplayer = new MediaPlayer();
mediaplayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
try {
mediaplayer.setDataSource(url);
mediaplayer.prepare();
} catch (IOException e) {
e.printStackTrace();
}
mediaplayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
@Override
public void onPrepared(MediaPlayer mp) {
total_duration = mediaplayer.getDuration();
seekBar.setMax(total_duration);
// time_view.setText(String.valueOf(mediaplayer.getDuration() / 1000));
time_view.setText(ConvertMsToHour(total_duration));
changeSeekbar();
}
});
seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
if (fromUser) {
mediaplayer.seekTo(progress);
}
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
total_duration = mediaplayer.getCurrentPosition();
}
});
return view;
}
@Override
public void onStart() {
super.onStart();
Dialog d = getDialog();
WindowManager.LayoutParams a = d.getWindow().getAttributes();
a.dimAmount = 0;
d.getWindow().setAttributes(a);
if (d != null) {
int width = ViewGroup.LayoutParams.MATCH_PARENT;
int height = ViewGroup.LayoutParams.WRAP_CONTENT;
d.getWindow().setLayout(width, height);
d.getWindow().setBackgroundDrawable(new ColorDrawable(Color.WHITE));
}
}
private void changeSeekbar() {
seekBar.setProgress(mediaplayer.getCurrentPosition());
if (mediaplayer.isPlaying()) {
Runnable runnable = new Runnable() {
@Override
public void run() {
if (total_duration > 0) {
total_duration = total_duration - 1000;
String time = ConvertMsToHour(total_duration);
time_view.setText(time);
changeSeekbar();
}
// else {
// total_duration = mediaplayer.getDuration();
// time_view.setText("0");
// }
}
};
handler.postDelayed(runnable, 1000);
}
}
private String ConvertMsToHour(long ms) {
// long ms = 10304004543l;
StringBuffer text = new StringBuffer("");
// if (ms > DAY) {
// text.append(ms / DAY).append(":");
// ms %= DAY;
// }
if (ms > HOUR) {
long min = ms / HOUR;
if (min < 10) {
text.append("0");
}
text.append(ms / HOUR).append(":");
ms %= HOUR;
}
if (ms > MINUTE) {
long min = ms / MINUTE;
if (min < 10) {
text.append("0");
}
text.append(ms / MINUTE).append(":");
ms %= MINUTE;
}
if (ms < SECOND) {
text.append("00");
}
if (ms > SECOND) {
long min = ms / SECOND;
if (min < 10) {
text.append("0");
}
text.append(ms / SECOND);
ms %= SECOND;
}
// text.append(ms);
return text.toString();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment