Android中音乐文件的信息详解【安卓源码解析二】

                前段时间处理android音乐文件信息,上网查资料发现Android系统自己提供了MediaScanner,MediaProvider,MediaStore等接口并且提供了一套数据库表格,通过 Content Provider的方式把内容共享给用户。说明一下【Android数据是私有的】可以通过Content Provider的方式共享数据,前面我大致介绍了这个Content Provider,Android中ContentProvider简介【安卓进化二十七】 。当手机开机或者有SD卡插拔等事件发生时,系统将会自动扫描SD卡和手机内存上的媒体文件,如 audio,video,图片等,将相应的信息放到定义好的数据库表格中。如果不插拔手机内存卡,如果把相应的音乐文件删除或移到别的文件夹中,系统就不会自动扫描手机内存卡,查询的Cursor对象存在,但是cursor.getCount()的值为0。在这个程序中,我们不需要关心如何去扫描手机中的文件,只要了解如何查询和使用这些信息就可以了。MediaStore中定义了一系列的数据表格,(这个数据表格是android系统自己建立的,我们不用关心如何建立,我们关心如何使用就可以了!)通过ContentResolver提供的查询接口,我们可以得到各种需要的信息。

        先来了解一下ContentResolver的查询接口,和sqlite数据库查询的方法是一样的

通过 query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder); 得到一个Cursor对象,这个Cursor对象中有数据库字段对应相应的音乐的信息:       

        Uri:指明要查询的数据库名称加上表的名称,从MediaStore中我们可以找到相应信息的参数,具体请参考开发文档。
        Projection: 指定查询数据库表中的哪几列,返回的游标中将包括相应的信息。Null则返回所有信息。
        selection: 指定查询条件
        selectionArgs:参数selection里有 ?这个符号是,这里可以以实际值代替这个问号。如果selection这个没有?的话,那么这个String数组可以为null。
        SortOrder:指定查询结果的排列顺序

下面的命令将返回所有在外部存储卡上的音乐文件的信息:

        先得到一个ContentResolver对象:ContentResolver cr = this.getContentResolver();

        Cursor cursor = cr.query(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, null,  
                    null, null, MediaStore.Audio.Media.DEFAULT_SORT_ORDER);得到cursor后,我们可以调用Cursor的相关方法具体的音乐信息:


           歌曲ID:MediaStore.Audio.Media._ID 
          int id = cursor.getInt(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media._ID));  


          歌曲的名称 :MediaStore.Audio.Media.TITLE
          String tilte = cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.TITLE));  
  
          歌曲的专辑名:MediaStore.Audio.Media.ALBUM
          String album = cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.ALBUM));  
  
          歌曲的歌手名: MediaStore.Audio.Media.ARTIST
          String artist = cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.ARTIST));  
  
          歌曲文件的路径 :MediaStore.Audio.Media.DATA
         String dataurl = cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.DATA));  
  
         歌曲的总播放时长 :MediaStore.Audio.Media.DURATION
         int duration = cursor.getInt(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.DURATION));  
  
         歌曲文件的大小 :MediaStore.Audio.Media.SIZE 
         long size = cursor.getLong(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.SIZE)); 



     下面看我写的小程序的截图:

 

                   程序的开始界面:                                                  点击button按钮后的界面:

                                              

 

                        点击选择曲目后的界面:                                          点击情歌后的界面:

                                               

 

下面看代码:在NotificationActivity工程下面

在com.cn.daming包下的NotificationActivity.java的代码:

package com.cn.daming;

import android.app.Activity;
import android.content.ContentResolver;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

public class NotificationActivity extends Activity {
	
	private  Button mButton2;
	private TextView textview3;
	private static final int MUSIC_PICKED = 3;
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        textview3 = (TextView)findViewById(R.id.textview3);
        mButton2 = (Button)findViewById(R.id.button2);
        mButton2.setOnClickListener(new OnClickListener(){

			public void onClick(View v) {
				Intent innerIntent = new Intent(Intent.ACTION_GET_CONTENT);
		        innerIntent.setType("audio/*");
//		        innerIntent.setType("audio/mp3");
//		        innerIntent.setType("audio/midi");
		        Intent wrapperIntent = Intent.createChooser(innerIntent, null);
		        startActivityForResult(wrapperIntent, MUSIC_PICKED);
			}
        });
    }

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
		if (resultCode != RESULT_OK) {  
            return;  
        } else {  
            String mCustomRingtone = null;
        	if(requestCode == MUSIC_PICKED){
    		    Uri pickedUri = data.getData();
   	            if (pickedUri != null)
   	            {
   	            	mCustomRingtone = pickedUri.toString();
   	            	ContentResolver cr = this.getContentResolver();
   	            	Cursor cursor = cr.query(pickedUri, null, null, null, null); 
   	            	cursor.moveToFirst();
   	            	String url = cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.DATA)); 
   	            	String title = cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.TITLE));
   	            	String artist = cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.ARTIST));
   	            	int duration = cursor.getInt(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.DURATION));  
   	            	long size = cursor.getLong(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.SIZE));
   	            	textview3.setText("mCustomRingtone:"+mCustomRingtone+
   	            			"\n\n pickedUri.getPath()=" +pickedUri.getPath() + 
   	            			"\n\n file url ="+url+
   	            			"\n\n file title="+title+
   	            			"\n\n file singer = "+artist+
   	            			"\n\n music duration="+duration+
   	            			"\n\n file size="+size);
   	            }else{
   	            	textview3.setText("null:");
   	            }
    	   }
        }
	}
}

 

在layout包下的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"
    >
	<TextView  
	    android:id="@+id/textview"
	    android:layout_width="fill_parent" 
	    android:layout_height="wrap_content" 
	    android:gravity="center_horizontal"
	    android:layout_marginTop="20dip"
	    android:text="@string/hello"
	    />
    <Button
	    android:id="@+id/button2"
	    android:layout_width="fill_parent"
	    android:layout_height="wrap_content"
	    android:layout_marginTop="20dip"
	    android:text="调用内存卡铃声"
	/>
	<TextView  
	    android:id="@+id/textview3"
	    android:layout_width="fill_parent" 
	    android:layout_height="wrap_content" 
	    android:layout_marginTop="20dip"
	/>
</LinearLayout>

 

在res下的string.xml中的代码:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string name="hello">大明原创,音乐信息</string>
    <string name="app_name">NotificationApp</string>
</resources>

有问题的可以留言,欢迎大家点评,以纠正错误!



  • 4
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值