如何解决此错误 - androidx.appcompat.widget.AppCompatSeekBar 无法转换为 android.view.ViewGroup?
What to do for this error- androidx.appcompat.widget.AppCompatSeekBar cannot be cast to android.view.ViewGroup?
希望你们在这次疫情中一切都好。我正在尝试在 Android 中创建一个录音机应用程序,它将在另一个 TAB 中录制和显示录制的文件。可以从列表中播放录制的文件列表。当我播放列表中的音频文件时(截图),出现以下错误:
2020-11-12 10:54:21.816 4605-4605/com.icddrb.recordaudio E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.icddrb.recordaudio, PID: 4605
android.view.InflateException: Binary XML file line #41: androidx.appcompat.widget.AppCompatSeekBar cannot be cast to android.view.ViewGroup
Caused by: java.lang.ClassCastException: androidx.appcompat.widget.AppCompatSeekBar cannot be cast to android.view.ViewGroup
at android.view.LayoutInflater.rInflate(LayoutInflater.java:884)
at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:846)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:886)
at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:846)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:886)
at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:846)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:886)
at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:846)
at android.view.LayoutInflater.inflate(LayoutInflater.java:522)
at android.view.LayoutInflater.inflate(LayoutInflater.java:430)
at android.view.LayoutInflater.inflate(LayoutInflater.java:377)
at com.icddrb.recordaudio.fragments.PlayBackFragment.onCreateDialog(PlayBackFragment.java:61)
at androidx.fragment.app.DialogFragment.onGetLayoutInflater(DialogFragment.java:380)
at androidx.fragment.app.Fragment.performGetLayoutInflater(Fragment.java:1412)
at androidx.fragment.app.FragmentManagerImpl.moveToState(FragmentManagerImpl.java:881)
at androidx.fragment.app.FragmentManagerImpl.moveFragmentToExpectedState(FragmentManagerImpl.java:1238)
at androidx.fragment.app.FragmentManagerImpl.moveToState(FragmentManagerImpl.java:1303)
at androidx.fragment.app.BackStackRecord.executeOps(BackStackRecord.java:439)
at androidx.fragment.app.FragmentManagerImpl.executeOps(FragmentManagerImpl.java:2079)
at androidx.fragment.app.FragmentManagerImpl.executeOpsTogether(FragmentManagerImpl.java:1869)
at androidx.fragment.app.FragmentManagerImpl.removeRedundantOperationsAndExecute(FragmentManagerImpl.java:1824)
at androidx.fragment.app.FragmentManagerImpl.execPendingActions(FragmentManagerImpl.java:1727)
at androidx.fragment.app.FragmentManagerImpl.run(FragmentManagerImpl.java:150)
at android.os.Handler.handleCallback(Handler.java:754)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:165)
at android.app.ActivityThread.main(ActivityThread.java:6375)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:912)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:802)
这是我的 fragment_playback.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:gravity="center_vertical">
<androidx.cardview.widget.CardView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/media_player_view"
android:elevation="4dp"
android:transitionName="open_mediaplayer"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="7dp"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="10dp"
android:layout_marginLeft="10dp"
android:layout_marginTop="7dp"
android:layout_marginBottom="10dp"
android:text="File name.mp3"
android:id="@+id/file_name_textview"/>
<SeekBar
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/seekbar">
<RelativeLayout
android:layout_width ="match_parent"
android:layout_height = "wrap_content">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="10dp"
android:layout_marginLeft="10dp"
android:layout_alignParentTop="true"
android:layout_alignParentBottom="true"
android:text="00:00"
android:id="@+id/current_progress_tv"/>
<com.melnykov.fab.FloatingActionButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/fab_play"
android:src="@drawable/ic_media_play"
android:layout_centerHorizontal="true"
android:layout_marginBottom="10dp"
app:fab_colorNormal="@color/colorPrimary"
app:fab_colorPressed="@color/colorPrimary"
app:fab_shadow="false"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/file_length_text_view"
android:text="00:00"
android:layout_alignParentTop="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"/>
</RelativeLayout>
</SeekBar>
</LinearLayout>
</androidx.cardview.widget.CardView>
</RelativeLayout>
这是我的 PlayBackFragment.java:
package com.icddrb.recordaudio.fragments;
import android.app.AlertDialog;
import android.app.Dialog;
import android.graphics.ColorFilter;
import android.graphics.LightingColorFilter;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.SeekBar;
import android.widget.TextView;
import androidx.fragment.app.DialogFragment;
import com.icddrb.recordaudio.R;
import com.icddrb.recordaudio.model.RecodingItem;
import com.melnykov.fab.FloatingActionButton;
import java.io.IOException;
import java.util.concurrent.TimeUnit;
import butterknife.BindView;
import butterknife.ButterKnife;
public class PlayBackFragment extends DialogFragment {
@BindView(R.id.file_name_textview) TextView fileNameTV;
@BindView(R.id.file_length_text_view) TextView fileLenghtTV;
@BindView(R.id.seekbar) SeekBar seekBar;
@BindView(R.id.current_progress_tv) TextView currentProgress;
@BindView(R.id.fab_play) FloatingActionButton floatingActionButton;
//SeekBar seekBar;
private RecodingItem item;
private Handler handler = new Handler();
private MediaPlayer mediaPlayer;
private boolean isPlaying = false;
long minutes =0;
long seconds = 0;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
item = (RecodingItem) getArguments().getSerializable("item");
minutes = TimeUnit.MILLISECONDS.toMinutes(item.getLength());
seconds = TimeUnit.MILLISECONDS.toSeconds(item.getLength())- TimeUnit.MINUTES.toSeconds(minutes);
}
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
Dialog dialog = super.onCreateDialog(savedInstanceState);
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
View view = getActivity().getLayoutInflater().inflate(R.layout.fragment_playback, null);
ButterKnife.bind(this, view);
//seekBar = getView().findViewById(R.id.seekbar);
seekbarValues();
floatingActionButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
onPlay(isPlaying);
isPlaying = !isPlaying;
}
});
fileNameTV.setText(item.getName());
fileLenghtTV.setText(String.format("%02d:%02d", minutes, seconds));
builder.setView(view);
dialog.getWindow().requestFeature(Window.FEATURE_NO_TITLE);
return builder.create();
}
private void onPlay(boolean isPlaying) {
if(!isPlaying){
if(mediaPlayer != null){
try {
startPlaying();
} catch (IOException e) {
e.printStackTrace();
}
}
} else{
pausePlaying();
}
}
private void pausePlaying() {
floatingActionButton.setImageResource(R.drawable.ic_media_play);
handler.removeCallbacks(mRunnable);
mediaPlayer.pause();
}
private void startPlaying() throws IOException {
floatingActionButton.setImageResource(R.drawable.ic_media_pause);
mediaPlayer = new MediaPlayer();
mediaPlayer.setDataSource(item.getPath());
mediaPlayer.prepare();
seekBar.setMax(mediaPlayer.getDuration());
mediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
@Override
public void onPrepared(MediaPlayer mp) {
mediaPlayer.start();
}
});
mediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer mp) {
stopPlaying();
}
});
updateSeekbar();
getActivity().getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
}
private void seekbarValues() {
ColorFilter colorFilter = new LightingColorFilter(getResources().getColor(R.color.colorPrimary),
getResources().getColor(R.color.colorPrimaryDark));
seekBar.getProgressDrawable().setColorFilter(colorFilter);
seekBar.getThumb().setColorFilter(colorFilter);
seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
if(mediaPlayer != null && fromUser){
mediaPlayer.seekTo(progress);
handler.removeCallbacks(mRunnable);
long minutes= TimeUnit.MILLISECONDS.toMinutes(mediaPlayer.getCurrentPosition());
long seconds= TimeUnit.MILLISECONDS.toSeconds(mediaPlayer.getCurrentPosition()) -
TimeUnit.MINUTES.toSeconds(minutes);
currentProgress.setText(String.format("%02d:%02d", minutes, seconds));
updateSeekbar();
} else if(mediaPlayer == null && fromUser){
try {
prepareMediaPlayerFromPoint(progress);
} catch (IOException e) {
e.printStackTrace();
}
updateSeekbar();
}
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
});
}
private void prepareMediaPlayerFromPoint(int progress) throws IOException {
mediaPlayer = new MediaPlayer();
mediaPlayer.setDataSource(item.getPath());
mediaPlayer.prepare();
seekBar.setMax(mediaPlayer.getDuration());
mediaPlayer.seekTo(progress);
mediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer mp) {
stopPlaying();
}
});
}
private void stopPlaying() {
floatingActionButton.setImageResource(R.drawable.ic_media_play);
handler.removeCallbacks(mRunnable);
mediaPlayer.stop();
mediaPlayer.reset();
mediaPlayer.release();
mediaPlayer = null;
seekBar.setProgress(seekBar.getMax());
isPlaying = !isPlaying;
currentProgress.setText(fileLenghtTV.getText());
seekBar.setProgress(seekBar.getMax());
}
private Runnable mRunnable = new Runnable() {
@Override
public void run() {
if(mediaPlayer != null){
int mCurrentPosition = mediaPlayer.getCurrentPosition();
seekBar.setProgress(mCurrentPosition);
long minutes= TimeUnit.MILLISECONDS.toMinutes(mCurrentPosition);
long seconds= TimeUnit.MILLISECONDS.toSeconds(mCurrentPosition) -
TimeUnit.MINUTES.toSeconds(minutes);
currentProgress.setText(String.format("%02d:%02d", minutes, seconds));
updateSeekbar();
}
}
};
private void updateSeekbar() {
handler.postDelayed(mRunnable, 1000);
}
}
我也在分享我的 build.gradle 文件:
plugins {
id 'com.android.application'
}
android {
compileSdkVersion 29
buildToolsVersion "29.0.3"
defaultConfig {
applicationId "com.icddrb.recordaudio"
minSdkVersion 16
targetSdkVersion 29
versionCode 1
versionName "1.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
}
dependencies {
implementation 'androidx.appcompat:appcompat:1.2.0'
implementation 'com.google.android.material:material:1.2.1'
implementation 'androidx.constraintlayout:constraintlayout:2.0.4'
testImplementation 'junit:junit:4.+'
androidTestImplementation 'androidx.test.ext:junit:1.1.2'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0'
implementation "androidx.recyclerview:recyclerview:1.1.0"
implementation "androidx.cardview:cardview:1.0.0"
implementation 'com.melnykov:floatingactionbutton:1.1.0'
implementation 'com.jpardogo.materialtabstrip:library:1.0.6'
implementation 'com.jakewharton:butterknife:10.0.0'
annotationProcessor 'com.jakewharton:butterknife-compiler:10.0.0'
implementation "com.android.support:support-core-utils:29.0.0"
}
在此先感谢您在这方面的帮助。
我认为你的 SeekBar 有 RelativeLayout。
所以你必须设置
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:gravity="center_vertical">
<androidx.cardview.widget.CardView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/media_player_view"
android:elevation="4dp"
android:transitionName="open_mediaplayer"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="7dp"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="10dp"
android:layout_marginLeft="10dp"
android:layout_marginTop="7dp"
android:layout_marginBottom="10dp"
android:text="File name.mp3"
android:id="@+id/file_name_textview"/>
<SeekBar
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/seekbar"/>
<RelativeLayout
android:layout_width ="match_parent"
android:layout_height = "wrap_content">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="10dp"
android:layout_marginLeft="10dp"
android:layout_alignParentTop="true"
android:layout_alignParentBottom="true"
android:text="00:00"
android:id="@+id/current_progress_tv"/>
<com.melnykov.fab.FloatingActionButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/fab_play"
android:src="@drawable/ic_media_play"
android:layout_centerHorizontal="true"
android:layout_marginBottom="10dp"
app:fab_colorNormal="@color/colorPrimary"
app:fab_colorPressed="@color/colorPrimary"
app:fab_shadow="false"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/file_length_text_view"
android:text="00:00"
android:layout_alignParentTop="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"/>
</RelativeLayout>
</LinearLayout>
</androidx.cardview.widget.CardView>
</RelativeLayout>
像这样。
希望你们在这次疫情中一切都好。我正在尝试在 Android 中创建一个录音机应用程序,它将在另一个 TAB 中录制和显示录制的文件。可以从列表中播放录制的文件列表。当我播放列表中的音频文件时(截图),出现以下错误:
2020-11-12 10:54:21.816 4605-4605/com.icddrb.recordaudio E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.icddrb.recordaudio, PID: 4605
android.view.InflateException: Binary XML file line #41: androidx.appcompat.widget.AppCompatSeekBar cannot be cast to android.view.ViewGroup
Caused by: java.lang.ClassCastException: androidx.appcompat.widget.AppCompatSeekBar cannot be cast to android.view.ViewGroup
at android.view.LayoutInflater.rInflate(LayoutInflater.java:884)
at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:846)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:886)
at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:846)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:886)
at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:846)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:886)
at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:846)
at android.view.LayoutInflater.inflate(LayoutInflater.java:522)
at android.view.LayoutInflater.inflate(LayoutInflater.java:430)
at android.view.LayoutInflater.inflate(LayoutInflater.java:377)
at com.icddrb.recordaudio.fragments.PlayBackFragment.onCreateDialog(PlayBackFragment.java:61)
at androidx.fragment.app.DialogFragment.onGetLayoutInflater(DialogFragment.java:380)
at androidx.fragment.app.Fragment.performGetLayoutInflater(Fragment.java:1412)
at androidx.fragment.app.FragmentManagerImpl.moveToState(FragmentManagerImpl.java:881)
at androidx.fragment.app.FragmentManagerImpl.moveFragmentToExpectedState(FragmentManagerImpl.java:1238)
at androidx.fragment.app.FragmentManagerImpl.moveToState(FragmentManagerImpl.java:1303)
at androidx.fragment.app.BackStackRecord.executeOps(BackStackRecord.java:439)
at androidx.fragment.app.FragmentManagerImpl.executeOps(FragmentManagerImpl.java:2079)
at androidx.fragment.app.FragmentManagerImpl.executeOpsTogether(FragmentManagerImpl.java:1869)
at androidx.fragment.app.FragmentManagerImpl.removeRedundantOperationsAndExecute(FragmentManagerImpl.java:1824)
at androidx.fragment.app.FragmentManagerImpl.execPendingActions(FragmentManagerImpl.java:1727)
at androidx.fragment.app.FragmentManagerImpl.run(FragmentManagerImpl.java:150)
at android.os.Handler.handleCallback(Handler.java:754)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:165)
at android.app.ActivityThread.main(ActivityThread.java:6375)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:912)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:802)
这是我的 fragment_playback.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:gravity="center_vertical">
<androidx.cardview.widget.CardView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/media_player_view"
android:elevation="4dp"
android:transitionName="open_mediaplayer"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="7dp"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="10dp"
android:layout_marginLeft="10dp"
android:layout_marginTop="7dp"
android:layout_marginBottom="10dp"
android:text="File name.mp3"
android:id="@+id/file_name_textview"/>
<SeekBar
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/seekbar">
<RelativeLayout
android:layout_width ="match_parent"
android:layout_height = "wrap_content">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="10dp"
android:layout_marginLeft="10dp"
android:layout_alignParentTop="true"
android:layout_alignParentBottom="true"
android:text="00:00"
android:id="@+id/current_progress_tv"/>
<com.melnykov.fab.FloatingActionButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/fab_play"
android:src="@drawable/ic_media_play"
android:layout_centerHorizontal="true"
android:layout_marginBottom="10dp"
app:fab_colorNormal="@color/colorPrimary"
app:fab_colorPressed="@color/colorPrimary"
app:fab_shadow="false"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/file_length_text_view"
android:text="00:00"
android:layout_alignParentTop="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"/>
</RelativeLayout>
</SeekBar>
</LinearLayout>
</androidx.cardview.widget.CardView>
</RelativeLayout>
这是我的 PlayBackFragment.java:
package com.icddrb.recordaudio.fragments;
import android.app.AlertDialog;
import android.app.Dialog;
import android.graphics.ColorFilter;
import android.graphics.LightingColorFilter;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.SeekBar;
import android.widget.TextView;
import androidx.fragment.app.DialogFragment;
import com.icddrb.recordaudio.R;
import com.icddrb.recordaudio.model.RecodingItem;
import com.melnykov.fab.FloatingActionButton;
import java.io.IOException;
import java.util.concurrent.TimeUnit;
import butterknife.BindView;
import butterknife.ButterKnife;
public class PlayBackFragment extends DialogFragment {
@BindView(R.id.file_name_textview) TextView fileNameTV;
@BindView(R.id.file_length_text_view) TextView fileLenghtTV;
@BindView(R.id.seekbar) SeekBar seekBar;
@BindView(R.id.current_progress_tv) TextView currentProgress;
@BindView(R.id.fab_play) FloatingActionButton floatingActionButton;
//SeekBar seekBar;
private RecodingItem item;
private Handler handler = new Handler();
private MediaPlayer mediaPlayer;
private boolean isPlaying = false;
long minutes =0;
long seconds = 0;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
item = (RecodingItem) getArguments().getSerializable("item");
minutes = TimeUnit.MILLISECONDS.toMinutes(item.getLength());
seconds = TimeUnit.MILLISECONDS.toSeconds(item.getLength())- TimeUnit.MINUTES.toSeconds(minutes);
}
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
Dialog dialog = super.onCreateDialog(savedInstanceState);
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
View view = getActivity().getLayoutInflater().inflate(R.layout.fragment_playback, null);
ButterKnife.bind(this, view);
//seekBar = getView().findViewById(R.id.seekbar);
seekbarValues();
floatingActionButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
onPlay(isPlaying);
isPlaying = !isPlaying;
}
});
fileNameTV.setText(item.getName());
fileLenghtTV.setText(String.format("%02d:%02d", minutes, seconds));
builder.setView(view);
dialog.getWindow().requestFeature(Window.FEATURE_NO_TITLE);
return builder.create();
}
private void onPlay(boolean isPlaying) {
if(!isPlaying){
if(mediaPlayer != null){
try {
startPlaying();
} catch (IOException e) {
e.printStackTrace();
}
}
} else{
pausePlaying();
}
}
private void pausePlaying() {
floatingActionButton.setImageResource(R.drawable.ic_media_play);
handler.removeCallbacks(mRunnable);
mediaPlayer.pause();
}
private void startPlaying() throws IOException {
floatingActionButton.setImageResource(R.drawable.ic_media_pause);
mediaPlayer = new MediaPlayer();
mediaPlayer.setDataSource(item.getPath());
mediaPlayer.prepare();
seekBar.setMax(mediaPlayer.getDuration());
mediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
@Override
public void onPrepared(MediaPlayer mp) {
mediaPlayer.start();
}
});
mediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer mp) {
stopPlaying();
}
});
updateSeekbar();
getActivity().getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
}
private void seekbarValues() {
ColorFilter colorFilter = new LightingColorFilter(getResources().getColor(R.color.colorPrimary),
getResources().getColor(R.color.colorPrimaryDark));
seekBar.getProgressDrawable().setColorFilter(colorFilter);
seekBar.getThumb().setColorFilter(colorFilter);
seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
if(mediaPlayer != null && fromUser){
mediaPlayer.seekTo(progress);
handler.removeCallbacks(mRunnable);
long minutes= TimeUnit.MILLISECONDS.toMinutes(mediaPlayer.getCurrentPosition());
long seconds= TimeUnit.MILLISECONDS.toSeconds(mediaPlayer.getCurrentPosition()) -
TimeUnit.MINUTES.toSeconds(minutes);
currentProgress.setText(String.format("%02d:%02d", minutes, seconds));
updateSeekbar();
} else if(mediaPlayer == null && fromUser){
try {
prepareMediaPlayerFromPoint(progress);
} catch (IOException e) {
e.printStackTrace();
}
updateSeekbar();
}
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
});
}
private void prepareMediaPlayerFromPoint(int progress) throws IOException {
mediaPlayer = new MediaPlayer();
mediaPlayer.setDataSource(item.getPath());
mediaPlayer.prepare();
seekBar.setMax(mediaPlayer.getDuration());
mediaPlayer.seekTo(progress);
mediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer mp) {
stopPlaying();
}
});
}
private void stopPlaying() {
floatingActionButton.setImageResource(R.drawable.ic_media_play);
handler.removeCallbacks(mRunnable);
mediaPlayer.stop();
mediaPlayer.reset();
mediaPlayer.release();
mediaPlayer = null;
seekBar.setProgress(seekBar.getMax());
isPlaying = !isPlaying;
currentProgress.setText(fileLenghtTV.getText());
seekBar.setProgress(seekBar.getMax());
}
private Runnable mRunnable = new Runnable() {
@Override
public void run() {
if(mediaPlayer != null){
int mCurrentPosition = mediaPlayer.getCurrentPosition();
seekBar.setProgress(mCurrentPosition);
long minutes= TimeUnit.MILLISECONDS.toMinutes(mCurrentPosition);
long seconds= TimeUnit.MILLISECONDS.toSeconds(mCurrentPosition) -
TimeUnit.MINUTES.toSeconds(minutes);
currentProgress.setText(String.format("%02d:%02d", minutes, seconds));
updateSeekbar();
}
}
};
private void updateSeekbar() {
handler.postDelayed(mRunnable, 1000);
}
}
我也在分享我的 build.gradle 文件:
plugins {
id 'com.android.application'
}
android {
compileSdkVersion 29
buildToolsVersion "29.0.3"
defaultConfig {
applicationId "com.icddrb.recordaudio"
minSdkVersion 16
targetSdkVersion 29
versionCode 1
versionName "1.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
}
dependencies {
implementation 'androidx.appcompat:appcompat:1.2.0'
implementation 'com.google.android.material:material:1.2.1'
implementation 'androidx.constraintlayout:constraintlayout:2.0.4'
testImplementation 'junit:junit:4.+'
androidTestImplementation 'androidx.test.ext:junit:1.1.2'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0'
implementation "androidx.recyclerview:recyclerview:1.1.0"
implementation "androidx.cardview:cardview:1.0.0"
implementation 'com.melnykov:floatingactionbutton:1.1.0'
implementation 'com.jpardogo.materialtabstrip:library:1.0.6'
implementation 'com.jakewharton:butterknife:10.0.0'
annotationProcessor 'com.jakewharton:butterknife-compiler:10.0.0'
implementation "com.android.support:support-core-utils:29.0.0"
}
在此先感谢您在这方面的帮助。
我认为你的 SeekBar 有 RelativeLayout。
所以你必须设置
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:gravity="center_vertical">
<androidx.cardview.widget.CardView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/media_player_view"
android:elevation="4dp"
android:transitionName="open_mediaplayer"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="7dp"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="10dp"
android:layout_marginLeft="10dp"
android:layout_marginTop="7dp"
android:layout_marginBottom="10dp"
android:text="File name.mp3"
android:id="@+id/file_name_textview"/>
<SeekBar
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/seekbar"/>
<RelativeLayout
android:layout_width ="match_parent"
android:layout_height = "wrap_content">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="10dp"
android:layout_marginLeft="10dp"
android:layout_alignParentTop="true"
android:layout_alignParentBottom="true"
android:text="00:00"
android:id="@+id/current_progress_tv"/>
<com.melnykov.fab.FloatingActionButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/fab_play"
android:src="@drawable/ic_media_play"
android:layout_centerHorizontal="true"
android:layout_marginBottom="10dp"
app:fab_colorNormal="@color/colorPrimary"
app:fab_colorPressed="@color/colorPrimary"
app:fab_shadow="false"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/file_length_text_view"
android:text="00:00"
android:layout_alignParentTop="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"/>
</RelativeLayout>
</LinearLayout>
</androidx.cardview.widget.CardView>
</RelativeLayout>
像这样。