• Android通过意图使用内置的音频播放器


    假设实现一个音频文件的播放,那么在应用程序中提供播放音频文件功能的最简单的方式是利用内置的“Music(音乐)”应用程序的功能--即使用系统自带的或已安装好的音乐播放器来播放指定的音频文件。


    本例比較简单,以下直接给出源码:

    布局文件activity_main:

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:paddingBottom="@dimen/activity_vertical_margin"
        android:paddingLeft="@dimen/activity_horizontal_margin"
        android:paddingRight="@dimen/activity_horizontal_margin"
        android:paddingTop="@dimen/activity_vertical_margin"
        tools:context=".MainActivity" >
    
        <TextView
            android:id="@+id/textView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerHorizontal="true"
            android:text="音乐播放器实例" />
    
        <Button
            android:id="@+id/button"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_below="@id/textView"
            android:text="播放音乐" />
    
    </RelativeLayout>

    代码文件MainActivity:

    package com.mutimediademo3audio;
    
    import java.io.File;
    import android.app.Activity;
    import android.content.Intent;
    import android.net.Uri;
    import android.os.Bundle;
    import android.os.Environment;
    import android.view.View;
    import android.view.View.OnClickListener;
    import android.widget.Button;
    
    public class MainActivity extends Activity implements OnClickListener {
    	private Button button;
    
    	@Override
    	protected void onCreate(Bundle savedInstanceState) {
    		super.onCreate(savedInstanceState);
    		setContentView(R.layout.activity_main);
    
    		button = (Button) findViewById(R.id.button);
    		button.setOnClickListener(this);
    	}
    
    	@Override
    	public void onClick(View v) {
    		switch (v.getId()) {
    		case R.id.button:
    			/**
    			 * 将通用android.content.Intent.ACTION_VIEW意图的数据设置为一个音频文件的URI,
    			 * 并制定其MIME类型,这样Android就能挑选适当的应用程序进行播放。
    			 */
    			Intent intent = new Intent(android.content.Intent.ACTION_VIEW);
    			File sdcard = Environment.getExternalStorageDirectory();
    			File audioFile = new File(sdcard.getPath() + "/good.mp3");//此处须要在sd卡中放置一个文件名称为good的mp3文件。
    			intent.setDataAndType(Uri.fromFile(audioFile), "audio/mp3");
    			startActivity(intent);
    			break;
    
    		default:
    			break;
    		}
    	}
    
    }
    

    源码:

    点击下载源代码


  • 相关阅读:
    关于 Kubernetes 中的 Volume 与 GlusterFS 分布式存储
    使用 Kubeadm 升级 Kubernetes 版本
    Kubernetes 中的核心组件与基本对象概述
    使用 Kubeadm 安装部署 Kubernetes 1.12.1 集群
    比较 Spring AOP 与 AspectJ
    关于 Spring Security OAuth2 中 CORS 跨域问题
    Prometheus 入门与实践
    MySQL 分支的选择:Percona 还是 MariaDB
    Spring Boot 集成 Swagger2 与配置 OAuth2.0 授权
    关于 Spring Security 5 默认使用 Password Hash 算法
  • 原文地址:https://www.cnblogs.com/mengfanrong/p/4079538.html
Copyright © 2020-2023  润新知