前几天有人向我要音乐播放器源代码 而自己因为系统问题而丢失了 所以决定重写一遍
为了与以前不同 所以今天打算不用ListView 而用RadioGroup 其实是换汤不换药而已
[代码 步骤]
1. 定制化有RadioGroup的界面:main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
>
<Button
android:id="@+id/cmd"
android:layout_width="wrap_content"
android:layout_height="500dip"
android:text="Loading.."
/>
</LinearLayout>
<RadioGroup
android:id="@+id/rgroup"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>
2. 初始化所有View
public void initialView(){
rGroup = (RadioGroup) findViewById(R.id.rgroup);
cmd = (Button) findViewById(R.id.cmd);
}
3. 检索MediaStore 得到sdcard所以音乐的Uri
Cursor c = getContentResolver().query(
MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,
null, null, null, null);
4. 把上面的音乐文件装入RadioButton
public void loadMusic(Cursor c){
int index = c.getColumnIndex(MediaStore.Audio.AudioColumns.TITLE);
int i=BASE_ID;
for(c.moveToFirst();!c.isAfterLast();c.moveToNext()){
String title = c.getString(index);
RadioButton rb = new RadioButton(this);
rb.setText(title);
rb.setId(i++);
rGroup.addView(rb);
}
}
5. 设立RadioGroup的选择监听器 并播放选中目标
rGroup.setOnCheckedChangeListener(new OnCheckedChangeListener(){
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
// TODO Auto-generated method stub
int id = group.getCheckedRadioButtonId()-BASE_ID+1;
try {
playMusic(id);
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
public void playMusic(long arg3) throws IllegalArgumentException, IllegalStateException, IOException{
Uri uri = Uri.withAppendedPath(
MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,
String.valueOf(arg3));
mPlayer.release();
mPlayer = MediaPlayer.create(this, uri);
mPlayer.start();
}
5. emulator 运行截图:
分享到:
相关推荐
ListView与RadioButton.zip ListView与RadioButton.zip ListView与RadioButton.zip ListView与RadioButton.zip ListView与RadioButton.zip
对于RadioGroup和RadioButton两个常用控件来说,一般不会单独使用RadioButton,而是两者结合起来使用。如何使用呢?下载代码一观究竟吧!
android 自定义RadioButton的样式android 自定义RadioButton的样式
Radiobutton流式自动换行
菜单radiobutton+viewpage
开发语言C#,开发环境.net,控制控件RadioButton进行美化。
android 自定义 单选按钮 radioButton
使用VS2005开发,VS2005及以上版本都可以使用
c# WPF 上图下文字radiobutton样式,且可动态生成radiobutton,搭配触摸滚动条,完整编译通过解决方案
wpf radiobutton模版设计成button样式
仿美团支付自定义radiobutton。样式可以自己修改。 仿美团支付自定义radiobutton。样式可以自己修改。 仿美团支付自定义radiobutton。样式可以自己修改。
在工作中遇到了这样一个问题,就是在同一界面下放了多个RadioButton来进行双向绑定,但是经过绑定后,在修改对象值后,发现数据和界面不同步的问题。然后在网上找了一些资料很多解决办法不好。用来很长时间找到的...
andrdoid自定义RadioButton ,设置选中框与字体的距离
RadioButton和RadioButtonList控件
RadioButton 改按钮样式,RadioButton 改按钮样式,RadioButton 改按钮样式
安卓Android源码——(RadioButton与监听).zip
WPF 实现RadioButton样式两种方式:1资源样式 2代码调用资源样式 ,vs2010
内嵌RadioButton的ListBox源码
实现RadioButton双击代码,平台vs2010,vb.net实现RadioButton双击简单代码