1、什么是Open Core?
Open Core 是 Android 多媒体框架的核心,所有 Android 平台的音频、视频的采用以及播放等操作,都是通过它来实现。
2、Open Core 的具体功能
- 1、多媒体文件的播放、下载,包括:3GPP,MPEG-4,AAC 和 MP3 containers。
- 2、流媒体文件的下载、实时播放,包括:3GPP,HTTP 和 RTSP/RTP。
- 3、动态视频和静态图像的编码、解码,例如:MPEG-4,H.263 和 AVC (H.264),JPEG。
- 4、语音编码格式:MP3,AAC,AAC+。
- 5、视频和图像格式:3GPP,MPEG-4 和 JPEG。
- 6、视频会议:基于 H324-M 标准。
Open Core 是一个多媒体的框架,从宏观上来看,它主要包含了两大方面的内容:
- 1、PVPPlayer:提供媒体播放器的功能,完成各种音频(Audio)、视频(Video)流的回放(Playback)功能。
- 2、PVAuthor:提供媒体流记录的功能,完成各种音频、视频流以及静态图像的捕获功能。
3、Mediaplayer 介绍
MediaPlayer 类可以用来播放音频、视频和流媒体,MediaPlayer 包含了 Audio 和 Video 的播放功能,在 Android 的界面上, Music 和 Video 两个应用程序都是调用 MediaPlayer 实现的。下面两张图是 MediaPlayer 在 Android 和 Open Core 的示例图:
下面是图例显示 MediaPlayer 的生命周期
3.1、MediaPlayer 常用方法介绍
4、完整音乐播放例子演示
实现的功能:
1.暂停/播放、下一首/上一首,点击某一首时播放
2.支持拖动进度条快进
3.列表排序
4.来电话时,停止播放,挂断后继续播放
5.可在后台播放
界面:
main.xml:
- <pre name="code" class="html"><span style="font-size:14px;"><?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"
- >
- <TextView
- Android:id="@+id/name"
- Android:layout_width="fill_parent"
- Android:layout_height="wrap_content"
- />
- <SeekBar
- Android:id="@+id/seekBar"
- Android:layout_width="fill_parent"
- Android:layout_height="wrap_content"
- Android:layout_marginBottom="5dp"
- />
- <LinearLayout
- Android:layout_width="fill_parent"
- Android:layout_height="wrap_content"
- Android:layout_marginBottom="20dp"
- >
- <Button
- Android:layout_width="40dp"
- Android:layout_height="40dp"
- Android:text="|◀"
- Android:onClick="previous"
- />
- <Button
- Android:id="@+id/pp"
- Android:layout_width="40dp"
- Android:layout_height="40dp"
- Android:text="▶"
- Android:onClick="pp"
- />
- <Button
- Android:layout_width="40dp"
- Android:layout_height="40dp"
- Android:text="▶|"
- Android:onClick="next"
- />
- </LinearLayout>
- <ListView
- Android:id="@+id/list"
- Android:layout_width="fill_parent"
- Android:layout_height="fill_parent"
- />
- </LinearLayout></span>
item.xml:
- <span style="font-size:14px;"><?xml version="1.0" encoding="utf-8"?>
- <LinearLayout
- xmlns:Android="http://schemas.android.com/apk/res/android"
- Android:layout_width="fill_parent"
- Android:layout_height="wrap_content"
- Android:padding="10dp"
- >
- <TextView
- Android:id="@+id/mName"
- Android:layout_width="fill_parent"
- Android:layout_height="wrap_content"
- Android:textSize="15sp"
- />
- </LinearLayout></span><span style="font-size: 18px;"> </span>
MainActivity:
- <pre name="code" class="java"><span style="font-size:14px;">import java.io.File;
- import java.util.ArrayList;
- import java.util.Collections;
- import java.util.HashMap;
- import java.util.List;
- import java.util.Map;
-
- import Android.app.Activity;
- import Android.content.BroadcastReceiver;
- import Android.content.Context;
- import Android.content.Intent;
- import Android.content.IntentFilter;
- import Android.media.MediaPlayer;
- import Android.media.MediaPlayer.OnCompletionListener;
- import Android.os.Bundle;
- import Android.os.Environment;
- import Android.os.Handler;
- import Android.telephony.PhoneStateListener;
- import Android.telephony.TelephonyManager;
- import Android.view.View;
- import Android.widget.AdapterView;
- import Android.widget.Button;
- import Android.widget.ListView;
- import Android.widget.SeekBar;
- import Android.widget.SimpleAdapter;
- import Android.widget.TextView;
- import Android.widget.AdapterView.OnItemClickListener;
- import Android.widget.SeekBar.OnSeekBarChangeListener;
-
- public class MainActivity extends Activity {
- private TextView nameTextView;
- private SeekBar seekBar;
- private ListView listView;
- private List<Map<String, String>> data;
- private int current;
- private MediaPlayer player;
- private Handler handler = new Handler();
- private Button ppButton;
- private boolean isPause;
- private boolean isStartTrackingTouch;
-
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
-
- nameTextView = (TextView) findViewById(R.id.name);
- seekBar = (SeekBar) findViewById(R.id.seekBar);
- listView = (ListView) findViewById(R.id.list);
- ppButton = (Button) findViewById(R.id.pp);
-
-
- player = new MediaPlayer();
-
-
- generateListView();
-
-
- seekBar.setOnSeekBarChangeListener(new MySeekBarListener());
-
-
- player.setOnCompletionListener(new MyPlayerListener());
-
-
- IntentFilter filter = new IntentFilter();
-
-
- filter.addAction("Android.intent.action.NEW_OUTGOING_CALL");
- registerReceiver(new PhoneListener(), filter);
-
-
- TelephonyManager manager = (TelephonyManager) getSystemService(TELEPHONY_SERVICE);
-
-
- manager.listen(new MyPhoneStateListener(), PhoneStateListener.LISTEN_CALL_STATE);
- }
-
-
- private final class MyPhoneStateListener extends PhoneStateListener {
- public void onCallStateChanged(int state, String incomingNumber) {
- pause();
- }
- }
-
-
- private final class MyPlayerListener implements OnCompletionListener {
-
- public void onCompletion(MediaPlayer mp) {
- next();
- }
- }
-
-
- public void next(View view) {
- next();
- }
-
-
- public void previous(View view) {
- previous();
- }
-
-
- private void previous() {
- current = current - 1 < 0 ? data.size() - 1 : current - 1;
- play();
- }
-
-
- private void next() {
- current = (current + 1) % data.size();
- play();
- }
-
-
- private final class MySeekBarListener implements OnSeekBarChangeListener {
-
- public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
- }
-
-
- public void onStartTrackingTouch(SeekBar seekBar) {
- isStartTrackingTouch = true;
- }
-
-
- public void onStopTrackingTouch(SeekBar seekBar) {
- player.seekTo(seekBar.getProgress());
- isStartTrackingTouch = false;
- }
- }
-
-
- private void generateListView() {
- List<File> list = new ArrayList<File>();
-
-
- findAll(Environment.getExternalStorageDirectory(), list);
-
-
- Collections.sort(list);
-
- data = new ArrayList<Map<String, String>>();
- for (File file : list) {
- Map<String, String> map = new HashMap<String, String>();
- map.put("name", file.getName());
- map.put("path", file.getAbsolutePath());
- data.add(map);
- }
-
- SimpleAdapter adapter = new SimpleAdapter(this, data, R.layout.item, new String[] { "name" }, new int[] { R.id.mName });
- listView.setAdapter(adapter);
-
- listView.setOnItemClickListener(new MyItemListener());
- }
-
- private final class MyItemListener implements OnItemClickListener {
- public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
- current = position;
- play();
- }
- }
-
-
- private void play() {
- try {
-
- player.reset();
-
-
- player.setDataSource(data.get(current).get("path"));
-
-
- player.prepare();
-
-
- player.start();
-
-
- nameTextView.setText(data.get(current).get("name"));
-
-
- seekBar.setMax(player.getDuration());
-
-
- ppButton.setText("||");
-
-
- handler.post(new Runnable() {
- public void run() {
-
- if (!isStartTrackingTouch)
- seekBar.setProgress(player.getCurrentPosition());
-
- handler.postDelayed(this, 1000);
- }
- });
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
-
-
- private void findAll(File file, List<File> list) {
- File[] subFiles = file.listFiles();
- if (subFiles != null)
- for (File subFile : subFiles) {
- if (subFile.isFile() && subFile.getName().endsWith(".mp3"))
- list.add(subFile);
- else if (subFile.isDirectory())
- findAll(subFile, list);
- }
- }
-
-
- public void pp(View view) {
-
-
- if (!player.isPlaying() && !isPause) {
- play();
- return;
- }
-
- Button button = (Button) view;
-
- if ("||".equals(button.getText())) {
- pause();
- button.setText("▶");
- } else {
- resume();
- button.setText("||");
- }
- }
-
-
- private void resume() {
- if (isPause) {
- player.start();
- isPause = false;
- }
- }
-
-
- private void pause() {
- if (player != null && player.isPlaying()) {
- player.pause();
- isPause = true;
- }
- }
-
-
- private final class PhoneListener extends BroadcastReceiver {
- public void onReceive(Context context, Intent intent) {
- pause();
- }
- }
-
-
- protected void onResume() {
- super.onResume();
- resume();
- }
- }
- </span>
- <pre name="code" class="html"><?xml version="1.0" encoding="utf-8"?>
- <manifest xmlns:Android="http://schemas.android.com/apk/res/android"
- package="cn.itcast.audio"
- Android:versionCode="1"
- Android:versionName="1.0">
- <application Android:icon="@drawable/icon" android:label="@string/app_name">
- <activity Android:name=".MainActivity"
- Android:label="@string/app_name">
- <intent-filter>
- <action Android:name="android.intent.action.MAIN" />
- <category Android:name="android.intent.category.LAUNCHER" />
- </intent-filter>
- </activity>
-
- </application>
- <uses-sdk Android:minSdkVersion="8" />
-
- <!-- 监听电话呼出 -->
- <uses-permission Android:name="android.permission.PROCESS_OUTGOING_CALLS"/>
- <!-- 监听电话状态改变 -->
- <uses-permission Android:name="android.permission.READ_PHONE_STATE" />
-
- </manifest>
转载:http://blog.csdn.net/chaoyu168/article/details/51150983