我一直在尝试在默认媒体播放器中播放mp3音频文件。从here复制代码我写这样的代码
AlertDialog.Builder dialog = new AlertDialog.Builder(this);
dialog
.setCancelable(true)
.setMessage("File Path: " + path + "\n"
+ "Duration: " + duration + "\n"
+ "File Format: " + format + "\n"
+ "File Status: " + status)
.setTitle("File Information")
.setPositiveButton("Play", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
Uri uri = null;
uri = Uri.parse(toPlay);
Intent intent = new Intent(android.content.Intent.ACTION_VIEW);
intent.setDataAndType(uri, "audio/mp3");
startActivity(intent);
}
})
.setNegativeButton("Delete", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
}
})
.setNeutralButton("Ok", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
}
});
其中path
和toPlay
等于/mnt/sdcard/MUSIC/Aey Nojwan.mp3
。现在当我按下dialog
上的播放按钮时,VLC播放器打开(没有从已安装的播放器中选择播放器)并显示一个包含以下错误的对话框:
VLC在此媒体上遇到错误。请尝试刷新媒体库
我尝试卸载VLC,但在这之后我的dialog
上的播放按钮什么也没做。可能是什么问题。
我也遇到过这个问题并且没有运气使用这种方式以及人们提到使用ACTION_VIEW的其他方式,所以我必须处理自己而不是传递默认播放器,我认为VLC没有正确接收Uri路径。您可以使用MediaPlayer类直接播放如下的音频文件
MediaPlayer mediaPlayer;
mediaPlayer = MediaPlayer.create(getApplicationContext(), R.raw.m_song);
if(!mediaPlayer.isPlaying())
mediaPlayer.start();
或者您也可以使用可以播放音频和视频的VideoView。实施将是这样的
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Bundle b = this.getIntent().getExtras();
String filePath= b.getString("file_path");
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.videoview);
video = (VideoView) findViewById(R.id.surface_view);
video.setVideoURI(Uri.parse(filePath));
MediaController mediaController = new MediaController(this);
video.setMediaController(mediaController);
video.requestFocus();
video.start();
}
<VideoView
android:id="@+id/surface_view"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_centerHorizontal="true"
android:layout_centerInParent="true"
android:layout_centerVertical="true" />