无法在对话框中使用 android MediaPlayer 播放声音?
Posted
技术标签:
【中文标题】无法在对话框中使用 android MediaPlayer 播放声音?【英文标题】:unable to play sound using android MediaPlayer in dialog? 【发布时间】:2012-01-30 08:35:32 【问题描述】:我正在尝试在对话框中播放声音。但不知何故,它不起作用。我不确定是不是因为它在对话框中。
File file = new File(Environment.getExternalStorageDirectory(),"/record");
final String soundUri = file.getAbsolutePath() + "/" +childName;
RelativeLayout layout = new RelativeLayout(MLT_File.this);
final MediaPlayer mp = MediaPlayer.create(MLT_File.this, Uri.parse(soundUri));
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT);
params.setMargins(0, 20, 0, 0);
LinearLayout layout2 = new LinearLayout(MLT_File.this);
layout2.setLayoutParams(params);
layout2.setGravity(Gravity.CENTER);
final ImageButton btn = new ImageButton(MLT_File.this);
btn.setImageDrawable(this.getResources().getDrawable(android.R.drawable.ic_media_play));
layout2.addView(btn);
layout.addView(layout2);
btn.setOnClickListener(new OnClickListener()
@Override
public void onClick(View v)
// TODO Auto-generated method stub
if(mp.isPlaying())
mp.pause();
btn.setImageDrawable(MLT_File.this.getResources().getDrawable(android.R.drawable.ic_media_play));
//video is playing
else
mp.start();
btn.setImageDrawable(MLT_File.this.getResources().getDrawable(android.R.drawable.ic_media_pause));
);//onClickListener
AlertDialog.Builder builder = new AlertDialog.Builder(MLT_File.this);
builder.setView(layout);
builder.setCancelable(false);
builder.setPositiveButton("Delete", new DialogInterface.OnClickListener()
@Override
public void onClick(DialogInterface dialog, int which)
// TODO Auto-generated method stub
mp.stop();
itemClick (1,soundUri);
);//delete
builder.setNeutralButton("Share", new DialogInterface.OnClickListener()
@Override
public void onClick(DialogInterface dialog, int which)
// TODO Auto-generated method stub
mp.stop();
itemClick (3,soundUri);
);//share
builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener()
@Override
public void onClick(DialogInterface dialog, int which)
// TODO Auto-generated method stub
mp.stop();
dialog.dismiss();
);//cancel
builder.create().show();
只有 1 个错误:
01-30 16:27:29.251: E/MediaPlayer-JNI(32416): setDataSource: outside path in JNI is �x@
我从Android: Playing an audio clip onClick得到了使用媒体播放器的想法
【问题讨论】:
【参考方案1】:随便用,
private MediaPlayer mp = new MediaPlayer();
mp.reset();
mp.setDataSource(soundUri);
mp.prepare();
mp.start();
让我知道发生了什么..
也看这个例子MusicDroid - Audio Player Part I
【讨论】:
它不工作。我是否需要在清单中添加任何内容才能访问它? 它有效.. 但因为我没有设置音量,所以我认为它没有。谢谢! 欢迎好友..!快乐编码..!以上是关于无法在对话框中使用 android MediaPlayer 播放声音?的主要内容,如果未能解决你的问题,请参考以下文章
Android错误:使用对话框时无法从可绘制资源中找到ColorStateList
Android:“BadTokenException:无法添加窗口;您的活动正在运行吗?”在 PreferenceActivity 中显示对话框