android 播放MP3

android 播放MP3
<?xml version="1.0" encoding="utf-8"?>
<!-- 定义当前布局的基本LinearLayout -->
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <!-- 定义提示用户播放mp3的显示控件 -->
    <TextView
        android:id="@+id/Tv"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="点击按钮播放/sdcard/1.mp3文件"
        />

    <!-- 定义用户点击播放声音的按钮控件 -->
    <Button
        android:id="@+id/Btn"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="播放声音"
        />
</LinearLayout>
package com.example.yanlei.yl2;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;


public class MainActivity extends AppCompatActivity {
    // 定义布局中的播放声音的Button控件
    private Button btn;
    // 定义显示标签的控件
    private TextView Tv;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //设置当前Activity的布局文件为activity_main
        setContentView(R.layout.activity_main);
        //得到浏览器中的控件对象
        findView();
        //设置对象的监听器
        setListener();
    }

    private void setListener() {
        // 设置btn的点击监听器
        btn.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                //定义intent对象,设置action属性为Intent.ACTION_VIEW
                Intent it = new Intent(Intent.ACTION_VIEW);
                //定义sdcard下的song.mp3文件的uri
                Uri uri = Uri.parse("file:///sdcard/1.mp3");//不是内存卡
                //设置intent的数据类型为audio/mp3,这样就可以启动系统程序打开mp3文件了
                it.setDataAndType(uri, "audio/mp3");
                //通过intent打开activity
                startActivity(it);
            }
        });
    }

    private void findView() {
        // 得到布局中的开始加载的Button的对象
        btn = (Button) findViewById(R.id.Btn);
        // 得到布局中的开始加载的EditText的对象
        Tv = (TextView) findViewById(R.id.Tv);
    }
}