如今,任何名副其实的智能手机都具有音频播放功能。当然,基于android的设备也不例外,它允许你建立音乐播放器,音频书籍,播客或任何围绕音频播放的其他应用类程序。本次将讨论Android在格式和编解码器支持方面的功能同时还将构建几个不同的播放程序。

音频播放

Android支持多种用于播放的音频文件格式和编解码器(同时也支持录音)

  • AAC
  • MP3
  • AMR
  • Ogg
  • PCM

具体的格式介绍可以自行查阅资料

通过意图使用系统内置的播放器

通过意图来促发播放制定的文件,使用android.content.Intent.ACTION_VIEW意图的数据设置为一个音频文件的URI,并指定其MIME类型,这样Android就可以挑选设当的应用程序播放。

Intent intent=new Intent(android.content.Intent.ACTION_VIEW);

intent.setDataAndType(audioFileUri,"audio/mp3");

startActivity(intent);

下面是一个完整的示例,

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;

  1. /**
  2. * 本示例演示如何利用Android自带的Music来播放程序
  3. * 和Camera一样,可以通过Intent来启动它。
  4. * 我们需要指定一个ACTION_VIEW的Action
  5. * 同时一个Uri来指定我们要播放文件的路径
  6. * 最后指定一个MIME类型,指定所要播放的文件类型
  7. * 每种文件类型对应的都有一个MIME,他一般是类似于audio/mp3格式
  8. * 前部分是一个较大的类型,后面是更具体的类型
  9. *
  10. * 同样的,对于Audio类型的多媒体,系统存储在MediaStore.Audio中
  11. * 包括Media,Album,Genre等信息体
  12. *
  13. * 本文将以列表的形式列出所有的Album信息,供用户选择
  14. * 当用户选择某个Album时,系统将打开这个ALbum下的所有Audio
  15. * @author Administrator
  16. *
  17. */

在触发播放音频之前,活动将监听是否按下一个按钮。由于活动实现OnClickListener,因此它可以响应该事件。

public class IntentAudioPlayer extends Activity implements OnClickListener {

Button playButton;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

playButton = (Button) this.findViewById(R.id.Button01);
playButton.setOnClickListener(this);
}

public void onClick(View v) {
Intent intent = new Intent(android.content.Intent.ACTION_VIEW);

File sdcard = Environment.getExternalStorageDirectory();
File audioFile = new File(sdcard.getPath()
+ "/Music/goodmorningandroid.mp3");

intent.setDataAndType(Uri.fromFile(audioFile), "audio/mp3");
startActivity(intent);
}
}

以下是布局文件:

<?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"
>
<Button android:text="Play Audio" android:id="@+id/Button01" android:layout_width="wrap_content" android:layout_height="wrap_content"></Button>
</LinearLayout>

关于这一部分,还有一篇进阶的文章大家可以看看:http://blog.csdn.net/chenjie19891104/article/details/6330383

更多相关文章

  1. Android音乐播放器(一):搜索手机存储的音乐
  2. c++11 + SDL2 + ffmpeg +OpenAL + java = Android播放器
  3. Android(安卓)使用LayerDrawable自定制SeekBar的外观
  4. Android视频播放框架Vitamio
  5. ANDROID音频系统散记之二:resample-1
  6. Android使用Intent Filter来响应隐式Intent
  7. android中的键盘处理
  8. 开源的Android视频播放器
  9. Android(安卓)动画实现

随机推荐

  1. Android AsyncTask异步实现大文件下载
  2. android创建可拖动的悬浮窗,并实现点击回
  3. Android中的WiFi P2P
  4. Android 热修复(腾讯bugly)
  5. android 存储操作 大小显示换算 kb mb KB
  6. 【Android】glide:3.7.0->4.8.0 升级导入
  7. Android app widget 支持的Layout和widge
  8. Settings中蓝牙连接流程
  9. Activity设置透明背景
  10. Java/Android倒计时(开始,暂停,恢复,停止)