智能机能少了播放器么?Android 多媒体开发-音频
2012-07-31 15:37
411 查看
如今,任何名副其实的智能手机都具有音频播放功能。当然,基于android的设备也不例外,它允许你建立音乐播放器,音频书籍,播客或任何围绕音频播放的其他应用类程序。本次将讨论Android在格式和编解码器支持方面的功能同时还将构建几个不同的播放程序。f.hualongxiang.com
音频播放
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;
在触发播放音频之前,活动将监听是否按下一个按钮。由于活动实现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>
音频播放
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;
在触发播放音频之前,活动将监听是否按下一个按钮。由于活动实现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>
相关文章推荐
- 木瓜妮子多媒体开发教程---第六天---Android下的音频均衡器
- 【Android开发】多媒体应用开发-使用MediaPlayer播放音频
- 木瓜妮子多媒体开发教程---第五天---Android下音频录制和播放
- 原创:Android应用开发记录-Andorid歌词秀(2)先来一个音频播放器
- Android本地视频播放器开发--ffmpeg解码视频文件中的音频(1)
- Android多媒体开发【6】-- 播放器的基本模型
- android多媒体开发--本地音频文件播放
- android开发笔记之多媒体—播放音频(音乐)
- android开发笔记之多媒体—音频和视频的录制
- Android初级开发(八)——手机多媒体—音频和视频的使用
- Android多媒体开发【6】-- 播放器的基本模型
- Android本地视频播放器开发--ffmpeg解码视频文件中的音频(1)
- Android本地视频播放器开发--ffmpeg解码视频文件中的音频(2)
- Android多媒体之视频播放器高级开发
- Android开发中音频播放器的调用
- Android本地视频播放器开发--ffmpeg解码视频文件中的音频(2)
- 【Android 多媒体开发】 MediaPlayer 网络视频播放器
- 【Android 多媒体开发】 MediaPlayer 网络视频播放器
- Android本地视频播放器开发--ffmpeg解码视频文件中的音频(1)
- 【Android开发】多媒体应用开发-使用SoundPool播放音频