Java 安卓音乐应用程序“;“已经停止”;无缘无故
我正在尝试为android创建基本音乐播放器。对我来说一切都很好,但当我尝试在手机上运行应用程序时。它说它停了。我不能解决那个问题。谢谢你的帮助。 我试着在应用程序停止时查看“Logcat”,但这对我来说似乎很正常 MainActivity.javaJava 安卓音乐应用程序“;“已经停止”;无缘无故,java,android,Java,Android,我正在尝试为android创建基本音乐播放器。对我来说一切都很好,但当我尝试在手机上运行应用程序时。它说它停了。我不能解决那个问题。谢谢你的帮助。 我试着在应用程序停止时查看“Logcat”,但这对我来说似乎很正常 MainActivity.java public class MainActivity extends ListActivity { private static final int UPDATE_FREQUENCY = 500; privat
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;
}
};
}
更新
现在这个弹出窗口
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"
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\u SM-N950F\u 8.0.0\u 0002 audit
04-06 00:49:32.767 11925-12132/? E/msgr.BootIdReader:从procfs读取引导id时出错
java.io.FileNotFoundException:/proc/sys/kernel/random/boot\u id(权限被拒绝)
位于java.io.FileInputStream.open0(本机方法)
在java.io.FileInputStream.open(FileInputStream.java:200)
位于java.io.FileInputStream。(FileInputStream.java:150)
位于java.io.FileInputStream。(FileInputStream.java:103)
位于java.io.FileReader。(FileReader.java:58)
在X.17G.a时(:240349)
在X.1ex.a(未知来源:680)
在X.0I4.A(未知来源:26)
在X.0IZ.a(:51612)
在X.0IZ.b(:51631)
在X.2FF.d(:450319)
在X.2FF.b处(:450310)
在X.2FF.a(:450272)
在X.2FD.init处(:450217)
在X.0RK运行时(:69037)
在X.0Mi运行时(:60705)
位于java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:457)
在java.util.concurrent.FutureTask.run(FutureTask.java:266)处
位于java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:457)
在X.0Nz下运行(:62781)
位于java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:457)
在java.util.concurrent.FutureTask.run(FutureTask.java:266)处
在X.0LN.运行时(:57215)
位于java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1162)
位于java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:636)
在X.0O8运行时(:63173)
运行(Thread.java:764)
04-06 00:49:32.769 3130-3130/? E/audit:type=1300 audit(1522968572.746:16192):arch=40000028 syscall=322per=8 success=no exit=-13 a0=ffffffff 9c a1=dbbf9200 a2=20000 a3=0 items=0 ppid=3787 pid=12132 auid=4294967295 uid=10271 guid=10271 fsuid=10271 egid=10271 sgid=10271 fsgid=10271 sgid=10271 fsgid=10271 tty=(无)ses=4294967295 comm=“Bg\u Shared3”exe=“/system/process32”sub=u:r:untrusted_app_25:s0:c512,c768 key=(空)
type=1327审计(1522968572.746:16192):proctitle=“com.facebook.orca”
另一个更新:问题似乎出在我称之为“listitem”的XML文件中,正如它所说,它列出了手机中的MP3文件。WIHT输出它“工作”的文件(及其实现,如持续时间、显示名称)(打开)
“列表项”
如果要从存储器中读取,则需要此权限
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
写作
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
如果你想读和写,把它们都加上logcat怎么说?过滤logcat的错误并将错误粘贴到问题上我看到的唯一“红色文本”是:04-06 00:44:39.587 10679-10679/?E/合子:isWhitelistProcess-过程被列入白名单04-06 00:44:39.588 10679-10679/?E/libpersona:ScankonExpersonas无法打开文件-/data/system/users/0/personalist.xml-没有此类文件或目录,但personalist.xml不存在事件找不到您尝试播放的文件。。检查您在列表项的标签中存储了什么。@Mohammad premissions没有问题吗?因为我想从手机存储器中加载/获取*.mp3文件并将其加载到列表项谢谢!我只需要阅读,所以我添加了外部阅读。仍然无法加载或查找任何类型的*.mp3并列出它们。非常感谢。大更新:我明白了!现在,我只需编写代码,在用户安装此应用程序时向其请求权限。非常感谢。如果这对你有帮助,请核对答案,我很乐意,但我必须得15分,已经得14分了,所以当我达到15分时,我会核对!我现在就试着写那个权限代码,谢谢!我能得到一点帮助吗?因此,我已经设法获得权限对话框窗口,询问用户是否希望授予我读取存储的权限,并且它可以工作,buuuuuuuu当我安装该应用程序时,它会打开“应用程序已停止”对话框,然后显示具有权限的对话框。如果你愿意,我可以给你更新的代码。非常感谢。04-06 02:20:42.397 11452-11714/? E/SingleFileAttributeStore.cpp:无法从磁盘读取数据,我必须加快一步,才能弹出数据。所以我必须设法安装apk,打开应用程序,然后在它试图找到没有它们的歌曲并崩溃之前请求权限。另外,我放入:ActivityCompat.requestPermissions(MainActivity.this,新字符串[]{android.Manifest.permission.READ_EXTERNAL_STORAGE},1);就在ONCREATE下面
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />