Android - 使用 Intent 选择要播放的歌曲
我正在尝试使用“活动”来选择一首歌曲,该活动将提供有关歌曲的元信息。我想这样做而不是简单的文件浏览器。我有以下代码,但不幸的是,它也会在单击后播放歌曲。我只是希望用户能够从他们的 MediaStore
中选择一首歌曲,然后在不播放的情况下对其进行操作。
public class Main extends Activity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Intent intent = new Intent();
intent.setAction(android.content.Intent.ACTION_PICK);
intent.setData(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI);
startActivity(intent);
}
}
I am trying to pick a song using an Activity which will give meta-information about the songs. I want to do this instead of a simple file browser . I have the following code but it unfortunately also plays the song once clicked. I simply want the user to be able to select a song from their MediaStore
and act upon it later without playing.
public class Main extends Activity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Intent intent = new Intent();
intent.setAction(android.content.Intent.ACTION_PICK);
intent.setData(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI);
startActivity(intent);
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
据我所知,您看到的“选择器”是一种“预览”类型的设计(在我的手机上查看)。
例如,这有点像“选择”铃声...
您会看到一个列表,可以依次选择每个铃声来获得“预览”。当您决定所需的铃声后,单击“确定”即可返回选择。单击“取消”只会让一切保持原样(保留现有的铃声选择)。
我看不到任何方法来覆盖选择器的这种行为,也没有找到任何替代方法(例如意图参数)来实现您想要做的事情。
换句话说,据我了解,您只是希望用户默默地选择一段音乐并将其返回到您的活动,但(预览)选择器不能以这种方式工作。
当用户在选择器中单击“确定”时,您可以找到用户预览/选择的内容,但是,如果您使用...
请注意,1234 只是一个任意代码。
如果您检查返回到 onActivityResult() 的 Intent,它将包含用户在按下“确定”之前选择的音乐片段的内容 Uri。
As far as I can tell, the 'picker' you're seeing is a 'preview' type of design (looking at it on my phone).
It's a bit like 'picking' a ringtone, for example...
You see a list and can select each one in turn to get a 'preview'. When you decide on the ringtone you want, you click OK and that returns the selection. Clicking Cancel simply leaves things as they were (existing ringtone selection is kept).
I can't see any way of overriding this behaviour of the picker and haven't found any alternative way (Intent parameters, for example) to achieve what you want to do.
In other words, as I understand it, you simply want the user to silently pick a piece of music and it to return to your Activity but the (preview) picker doesn't work that way.
You can find out what the user previewed/selected when they click OK in the picker however, if you use...
Note, 1234 is just an arbitrary code.
If you check the Intent returned to onActivityResult() it will have the content Uri of the piece of music the user selected before they pressed OK.