gpt4 book ai didi

java - Android 音乐应用程序 "has stopped"无缘无故

转载 作者:搜寻专家 更新时间:2023-11-01 09:27:27 28 4
gpt4 key购买 nike

我正在尝试为 android 创建基本的音乐播放器。对我来说一切似乎都很好,但是当我试图在我的手机上运行应用程序时。它说它停止了。我无法解决那个问题。感谢您的任何帮助。我试图在应用程序停止时查看“Logcat”,但这对我来说似乎很正常。

MainActivity.java

    public class MainActivity extends ListActivity {
private static final int UPDATE_FREQUENCY = 500;
private static final int STEP_VALUE = 4000;

private MediaCursorAdapter mediaAdapter = null;
private TextView selectedFile= null;
private SeekBar seekbar = null;
private MediaPlayer player = null;
private ImageButton playButton = null;
private ImageButton previousButton = null;
private ImageButton nextButton = null;

private boolean isStarted = true;
private String currentFile = "";
private boolean isMovingseekBar = false;

private final Handler handler = new Handler();

private final Runnable updatePositionRunnable = new Runnable() {
public void run() {
updatePosition();

}
};

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

selectedFile = (TextView) findViewById(R.id.selectedfile);
seekbar = (SeekBar) findViewById(R.id.seekbar);
playButton = (ImageButton) findViewById(R.id.play);
previousButton = (ImageButton) findViewById(R.id.previous);
nextButton = (ImageButton) findViewById(R.id.next);

player = new MediaPlayer();

player.setOnCompletionListener(onCompletion);
player.setOnErrorListener(onError);
seekbar.setOnSeekBarChangeListener(seekBarChanged);
Cursor cursor = getContentResolver().query(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, null, null, null, null);

if (null != cursor) {
cursor.moveToFirst();

mediaAdapter = new MediaCursorAdapter(this, R.layout.listitem, cursor);
setListAdapter(mediaAdapter);
playButton.setOnClickListener(onButtonClick);
nextButton.setOnClickListener(onButtonClick);
previousButton.setOnClickListener(onButtonClick);
}
}
@Override
protected void onListItemClick(ListView list, View view, int position, long id) {
super.onListItemClick(list, view, position, id);
currentFile = (String) view.getTag();
startPlay(currentFile);
}
@Override
protected void onDestroy(){
super.onDestroy();

handler.removeCallbacks(updatePositionRunnable);
player.stop();
player.reset();
player.release();

player = null;
}
private void startPlay(String file) {
// Log.i("Selected: ",file);

selectedFile.setText(file);
seekbar.setProgress(0);

player.stop();
player.reset();

try {
player.setDataSource(file);
player.prepare();
player.start();
} catch (IllegalArgumentException e){
e.printStackTrace();
} catch (IllegalStateException e){
e.printStackTrace();
} catch (IOException e){
e.printStackTrace();
}

seekbar.setMax(player.getDuration());
playButton.setImageResource(android.R.drawable.ic_media_pause);

updatePosition();

isStarted = true;

}

private void stopPlay() {
player.stop();
player.reset();
playButton.setImageResource(android.R.drawable.ic_media_play);
handler.removeCallbacks(updatePositionRunnable);
seekbar.setProgress(0);

isStarted = false;
}
private void updatePosition() {
handler.removeCallbacks(updatePositionRunnable);

// seekbar.setSecondaryProgressTintMode(player.getCurrentPosition());
handler.postDelayed(updatePositionRunnable, UPDATE_FREQUENCY);
}


private class MediaCursorAdapter extends SimpleCursorAdapter {

public MediaCursorAdapter(Context context, int layout, Cursor c){
super(context, layout,c,
new String[] {MediaStore.MediaColumns.DISPLAY_NAME, MediaStore.MediaColumns.TITLE, MediaStore.Audio.AudioColumns.DURATION},
new int[] {R.id.displayname, R.id.title,R.id.duration});
}

@Override
public void bindView(View view,Context context, Cursor cursor) {
TextView title = (TextView) view.findViewById(R.id.title);
TextView name = (TextView) view.findViewById(R.id.displayname);
TextView duration = (TextView) view.findViewById(R.id.duration);

name.setText(cursor.getString(
cursor.getColumnIndex(MediaStore.MediaColumns.DISPLAY_NAME)));

title.setText(cursor.getString(
cursor.getColumnIndex(MediaStore.MediaColumns.TITLE)));

// long durationInMs = Long.parseLong(cursor.getString(
// cursor.getColumnIndex(MediaStore.Audio.AudioColumns.DURATION)));
//
// double durationInMin = ((double) durationInMs / 1000.0) / 60.0;

// durationInMin = new BigDecimal(Double.toString(durationInMin)).setScale(2, BigDecimal.ROUND_UP).doubleValue();
//
// duration.setText("" + durationInMin);
//
view.setTag(cursor.getString(cursor.getColumnIndex(MediaStore.MediaColumns.DATA)));
}
@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
LayoutInflater inflater = LayoutInflater.from(context);
View v = inflater.inflate(R.layout.listitem, parent, false);

bindView(v, context, cursor);

return v;
}
}



private View.OnClickListener onButtonClick = new View.OnClickListener() {

@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.play: {
if (player.isPlaying()) {
handler.removeCallbacks(updatePositionRunnable);
player.pause();
playButton.setImageResource(android.R.drawable.ic_media_play);
} else {
if (isStarted) {
player.start();
playButton.setImageResource(android.R.drawable.ic_media_pause);
updatePosition();
} else {
startPlay(currentFile);
}
}
break;
}
case R.id.next: {
int seekto = player.getCurrentPosition() + STEP_VALUE;

if (seekto > player.getDuration())
seekto = player.getDuration();
player.start();

break;
}
case R.id.previous: {
int seekto = player.getCurrentPosition() - STEP_VALUE;

if (seekto < 0)
seekto = 0;

player.pause();
player.seekTo(seekto);
player.start();

break;

}
}
}
};
private MediaPlayer.OnCompletionListener onCompletion = new MediaPlayer.OnCompletionListener() {

@Override
public void onCompletion(MediaPlayer mp) {
stopPlay();
}
};

private MediaPlayer.OnErrorListener onError = new MediaPlayer.OnErrorListener() {

@Override
public boolean onError(MediaPlayer mp, int what, int extra) {

return false;
}
};


private SeekBar.OnSeekBarChangeListener seekBarChanged = new SeekBar.OnSeekBarChangeListener() {
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
isMovingseekBar = false;
}

@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fomUser) {
if (isMovingseekBar) {
player.seekTo(progress);
Log.i("OnSeekBarChangeListener", "onProgressChange");
}
}

@Override
public void onStartTrackingTouch(SeekBar seekBar) {
isMovingseekBar = true;
}
};

}

Logcat“警告”

04-06 00:44:39.587 10679-10679/? E/Zygote: isWhitelistProcess - Process is Whitelisted 04-06 00:44:39.588 10679-10679/? E/libpersona: scanKnoxPersonas Couldn't open the File - /data/system/users/0/personalist.xml - No such file or directory

已更新现在这个弹出窗口

04-06 00:49:32.764 3130-3130/? E/audit: type=1400 audit(1522968572.746:16192): avc:  denied  { read } for  pid=12132 comm="Bg_Shared3" name="boot_id" dev="proc" ino=12061920 scontext=u:r:untrusted_app_25:s0:c512,c768 tcontext=u:object_r:proc:s0 tclass=file permissive=0 SEPF_SM-N950F_8.0.0_0002 audit_filtered
04-06 00:49:32.767 11925-12132/? E/msgr.BootIdReader: Error reading boot_id from procfs
java.io.FileNotFoundException: /proc/sys/kernel/random/boot_id (Permission denied)
at java.io.FileInputStream.open0(Native Method)
at java.io.FileInputStream.open(FileInputStream.java:200)
at java.io.FileInputStream.<init>(FileInputStream.java:150)
at java.io.FileInputStream.<init>(FileInputStream.java:103)
at java.io.FileReader.<init>(FileReader.java:58)
at X.17G.a(:240349)
at X.1ex.a(Unknown Source:680)
at X.0I4.A(Unknown Source:26)
at X.0IZ.a(:51612)
at X.0IZ.b(:51631)
at X.2FF.d(:450319)
at X.2FF.b(:450310)
at X.2FF.a(:450272)
at X.2FD.init(:450217)
at X.0RK.run(:69037)
at X.0Mi.run(:60705)
at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:457)
at java.util.concurrent.FutureTask.run(FutureTask.java:266)
at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:457)
at X.0Nz.run(:62781)
at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:457)
at java.util.concurrent.FutureTask.run(FutureTask.java:266)
at X.0LN.run(:57215)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1162)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:636)
at X.0O8.run(:63173)
at java.lang.Thread.run(Thread.java:764)
04-06 00:49:32.769 3130-3130/? E/audit: type=1300 audit(1522968572.746:16192): arch=40000028 syscall=322 per=8 success=no exit=-13 a0=ffffff9c a1=dbbf9200 a2=20000 a3=0 items=0 ppid=3787 pid=12132 auid=4294967295 uid=10271 gid=10271 euid=10271 suid=10271 fsuid=10271 egid=10271 sgid=10271 fsgid=10271 tty=(none) ses=4294967295 comm="Bg_Shared3" exe="/system/bin/app_process32" subj=u:r:untrusted_app_25:s0:c512,c768 key=(null)
type=1327 audit(1522968572.746:16192): proctitle="com.facebook.orca"

另一个更新:问题似乎出在我称为“listitem”的 XML 文件中,正如它所说,它列出了手机中的 MP3 文件。 Wihtout 那个文件(及其实现,如持续时间、显示名称)它确实“工作”(打开)

“列表项”

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">

<TextView android:id="@+id/displayname"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:ellipsize="end"
android:textSize="18dip"
android:textStyle="bold" />

<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">

<TextView
android:id="@+id/title"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1.0"
android:ellipsize="end"
android:singleLine="true"
android:textSize="15dip" />
<TextView
android:id="@+id/duration"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ellipsize="end"
android:gravity="right"
android:singleLine="true"
android:textSize="15dip" />


</LinearLayout>

</LinearLayout>

最佳答案

如果你想从存储中读取你需要这个权限

<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />

写作

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

如果你想读和写,把它们都加上

关于java - Android 音乐应用程序 "has stopped"无缘无故,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49682566/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com