假设实现一个音频文件的播放,那么在应用程序中提供播放音频文件功能的最简单的方式是利用内置的“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;
}
} }

源码:

点击下载源代码

Android通过意图使用内置的音频播放器的更多相关文章

  1. 基于c开发的全命令行音频播放器

    cmus是一个内置了音频播放器的强大的音乐文件管理器.用它的基于ncurses的命令行界面,你可以浏览你的音乐库,并从播放列表或队列中播放音乐,这一切都是在命令行下. Linux上安装cmus 首先, ...

  2. Android学习笔记(十七)——使用意图调用内置应用程序

    使用意图调用内置应用程序 1.创建一个新的Android项目并命名为Intents,在main.xml文件里加入两个Button: <Button android:id="@+id/b ...

  3. Python之函数(自定义函数,内置函数,装饰器,迭代器,生成器)

    Python之函数(自定义函数,内置函数,装饰器,迭代器,生成器) 1.初始函数 2.函数嵌套及作用域 3.装饰器 4.迭代器和生成器 6.内置函数 7.递归函数 8.匿名函数

  4. 全双工音频播放器在c#中使用waveIn / waveOut api

    http://www.codeproject.com/Articles/4889/A-full-duplex-audio-player-in-C-using-the-waveIn-w 一篇关于低级音频 ...

  5. HTML5 音频播放器-Javascript代码(短小精悍)

    直接上干货咯! //HTML5 音频播放器 lzpong 2015/01/19 var wavPlayer = function () { if(window.parent.wavPlayer) re ...

  6. 【jquery】一款不错的音频播放器——Amazing Audio Player

    前段时间分享了一款视频播放器,点击这里.今天介绍一款不错的音频播放器——Amazing Audio Player. 介绍: Amazing Audio Player 是一个使用很方便的 Windows ...

  7. IOS开发之简单音频播放器

    今天第一次接触IOS开发的UI部分,之前学OC的时候一直在模拟的使用Target-Action回调模式,今天算是真正的用了一次.为了熟悉一下基本控件的使用方法,和UI部分的回调,下面开发了一个特别简易 ...

  8. 与众不同 windows phone (14) - Media(媒体)之音频播放器, 视频播放器, 与 Windows Phone 的音乐和视频中心集成

    原文:与众不同 windows phone (14) - Media(媒体)之音频播放器, 视频播放器, 与 Windows Phone 的音乐和视频中心集成 [索引页][源码下载] 与众不同 win ...

  9. Unity3D音频播放器 动态装载组件

    大多数在线Unity有关如何只教程Unity在播放音乐.之后如何通过拖动它们无法继续添加音频文件 但有时在游戏中的对象要玩几个声音.这时候我们就需要使用代码控制,拖动推教程AudioClip颂值的方法 ...

随机推荐

  1. C++出现计算机术语5

    class template(类模板) 可以用来定义一个类定义了一组特定类型的类的.类模板template keyword其次是尖括号(<>)附上.的列表来定义. export keywo ...

  2. poj3233(矩阵快速幂)

    poj3233 http://poj.org/problem?id=3233 给定n ,k,m 然后是n*n行, 我们先可以把式子转化为递推的,然后就可以用矩阵来加速计算了.  矩阵是加速递推计算的一 ...

  3. 《深入了解Android:Wi-Fi、NFC和GPS音量》勘误表

    资源下载更新(我不知道为什么被115网盘屏蔽) 深入了解android它wifi-nfc-gps 115网盘礼包码:5lbee5qn4g5ghttp://115.com/lb/5lbee5qn4g5g ...

  4. sql时间比较

  5. APK ubuntu下 数字签名

    Android系统要求每个Android应用程序必需要经过数字签名才可以安装到系统中,也就是说假设一个Android应用程序没有经过数字签名,是没有办法安装到系统中的! Android通过数字签名来标 ...

  6. linux下&quot;=&quot;号与&quot;==&quot;号

    <鸟哥的linux私房菜>基础学习篇P382中说,在bash中"="与"=="是同样的,都代表推断是否相等,仅仅只是因为其它语言一般写法使用&quo ...

  7. 【设计模式】Template Method模式

    OO基金会 封装 多态 继承 OO原则 封装变化 多用组合,少用继承 针对接口编程.不针对实现编程 为交互对象的松耦合设计而努力 类应该对扩展开放,对改动关闭 依赖抽象,不要依赖详细类 仅仅和朋友交谈 ...

  8. Android使用Activity用作弹出式对话框

    转载请表明出处:http://blog.csdn.net/lmj623565791/article/details/23116115 Android中可用于实现对话框的有Dialog,PopupWin ...

  9. Codeforces 385B Bear and Strings

    题目链接:Codeforces 385B Bear and Strings 记录下每一个bear的起始位置和终止位置,然后扫一遍记录下来的结构体数组,过程中用一个变量记录上一个扫过的位置,用来去重. ...

  10. 乐在其中设计模式(C#) - 桥接模式(Bridge Pattern)

    原文:乐在其中设计模式(C#) - 桥接模式(Bridge Pattern) [索引页][源码下载] 乐在其中设计模式(C#) - 桥接模式(Bridge Pattern) 作者:webabcd 介绍 ...