Android:open default audio player

狂风中的少年 提交于 2019-12-11 13:58:00

问题


I am making an app in which I have to open default audio player. My code is as follows:

Intent intent = new Intent();           
intent.setAction(android.content.Intent.ACTION_VIEW);  
File file = new File(songs.get(position));  
//String introURI = "file:///sdcard/"+".mp3";  
intent.setDataAndType(Uri.fromFile(file), "audio/*");  
startActivity(intent);

When the song is touched, the player gets open but it gives message that "this type of file is not supported".


回答1:


The MediaPlayer class should be used when you want to implement your own media player. If you want to use an existing player, you'll have to launch the appropriate intent, for example:

Uri uri = Uri.parse("http://www.example.com/file.mp3");
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
startActivity(intent);

EDIT

How to get android local files uri




回答2:


It will work. Try this

    Intent intent = new Intent();
    intent.setAction(android.content.Intent.ACTION_VIEW);
    File file = new File(path);// path where your while is placed.For me like /storage/sdcard0/Media/audio/%2F1506580826442?alt=media&token=0e22f657-743c-4aed-9fed-48de69aced73.mp3
    intent.setDataAndType(Uri.fromFile(file), "audio/*");
    ActivityChatView.mContext.startActivity(intent);


来源:https://stackoverflow.com/questions/10027709/androidopen-default-audio-player

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!