- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
我正在尝试为 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/
我正在安装这个程序:THERMUS ,据我所知应该安装正常。我/我通过 ubuntu 控制台安装这个程序。但是当我运行 make all 时,我收到了这条消息: make: ***No rule to
我正在渲染一个简单的 sass 文件并收到以下警告: This selector doesn't have any properties and won't be rendered. ╷ 14
我不明白为什么文本不会与 div 的中间对齐,我认为这是导致页面底部出现空白的原因,我希望文本位于中间(高度)页脚的(两个 div 我都有两个强制文本的每个部分到页面的边缘) HTML:
我正在尝试为 android 创建基本的音乐播放器。对我来说一切似乎都很好,但是当我试图在我的手机上运行应用程序时。它说它停止了。我无法解决那个问题。感谢您的任何帮助。我试图在应用程序停止时查看“Lo
在我的 LoginProvider 中,我使用了一个函数来执行登录并将创建的 session 作为 promise 返回。 @Injectable() export class LoginProvid
我在 Google Cloud Platform 上运行 Dataflow-Jobs,我收到的一个新错误是“Workflow failed”,没有任何解释。我得到的日志如下: 2017-08-25
我已经阅读了无数关于这个错误的主题,但是没有一个和我有同样的问题。 我得到了 E/MediaPlayer: 错误 (-19, 0) E/MediaPlayer: 错误 (-19,0) 错误,然而,音乐
这个错误或我缺乏知识或其他东西真的开始困扰我。我正在开发一个 Grails 应用程序,并且在我的工作过程中随机出现 Grails 提示一些导入,说无法解析类名。它在一个保存前工作!我没有对项目的基础设
我为此失去了头发!我不断收到“发送后无法设置 header ”错误,我确定我没有像在其他问题中看到的那样调用 Next()。我的代码一直在工作,直到我尝试进行一些重构,我没有改变这个类的任何东西,所以
我是一名优秀的程序员,十分优秀!