【Android】如何用MediaPlayer实现一个简单的音视频播放器
来源:互联网 发布:linux 分页 0.11 编辑:程序博客网 时间:2024/04/27 13:21
我们在上一篇文章 如何实现一个简单的文件浏览器的基础上,使用MeidaPlayer实现对音视频文件的播放功能。
主要代码如下:
Manifest文件
<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.loushuai.simpleplayer"> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> <uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS" /> <application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:supportsRtl="true" android:theme="@style/AppTheme"> <activity android:name=".MainActivity" android:label="@string/title_simple_palyer"> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <activity android:name=".PlayerActivity"></activity> </application></manifest>
activity_player.xml
<?xml version="1.0" encoding="utf-8"?><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="com.example.loushuai.simpleplayer.PlayerActivity"> <SurfaceView android:id="@+id/surfaceView1" android:layout_height="fill_parent" android:layout_width="fill_parent"></SurfaceView></RelativeLayout>
MainActiviry.java
package com.example.loushuai.simpleplayer;import android.app.ListActivity;import android.content.Intent;import android.os.Bundle;import android.os.Environment;import android.util.Log;import android.view.View;import android.widget.ListView;import android.widget.SimpleAdapter;import android.widget.Toast;import java.io.File;import java.util.ArrayList;import java.util.HashMap;import java.util.List;import java.util.Map;import java.util.Stack;public class MainActivity extends ListActivity { public static final String COLUMN_NAME_NAME = "name"; private SimpleAdapter adapter = null; private List<Map<String, Object>> itemList; private Stack<String> pathHistory = null; private String curPath; String[] from = { COLUMN_NAME_NAME } ; int[] to = {R.id.myView1}; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); pathHistory = new Stack<String>(); String sDStateString = Environment.getExternalStorageState(); if(sDStateString.equals(Environment.MEDIA_MOUNTED)) { File SDFile = Environment.getExternalStorageDirectory(); curPath = SDFile.getAbsolutePath() + "/"; itemList = getData(curPath); adapter = new SimpleAdapter(this, itemList, R.layout.list_item, from, to); setListAdapter(adapter); } } private List<Map<String, Object>> getData(String path) { List<Map<String, Object>> list = new ArrayList<Map<String,Object>>(); Map<String, Object> map = new HashMap<String, Object>(); map.put(COLUMN_NAME_NAME, ".."); list.add(map); File file = new File(path); if (file.listFiles().length > 0) { for (File f : file.listFiles()) { if (fileFilter(f)) { map = new HashMap<String, Object>(); String name = f.getName(); if (f.isDirectory()) { name += "/"; } map.put(COLUMN_NAME_NAME, name); list.add(map); Log.d("Scan", " path " + path + f.getName()); } } } return list; } @Override protected void onListItemClick(ListView l, View v, int position, long id) { super.onListItemClick(l, v, position, id); String path; if (position > 0) { pathHistory.push(curPath); path = curPath + itemList.get(position).get(COLUMN_NAME_NAME); } else { // uplevel if (!pathHistory.empty()) path = pathHistory.pop(); else // root path = curPath; } Log.d("List View Click", " position: " + position + " name: " + path); File file = new File(path); if (file.isDirectory()) { updateList(path); curPath = path; } else { Intent intent = new Intent(MainActivity.this, PlayerActivity.class); intent.putExtra("path", path); startActivity(intent); } } private void updateList(String path) { itemList.clear(); itemList = getData(path); adapter = new SimpleAdapter(this, itemList, R.layout.list_item, from, to); setListAdapter(adapter); adapter.notifyDataSetChanged(); } private boolean fileFilter(File file) { if(file.isDirectory()) { return true; } else { String name = file.getName(); if (name.endsWith(".mp3") || name.endsWith(".mp4")) return true; else return false; } }}
PlayerActivity.java
package com.example.loushuai.simpleplayer;import android.app.Activity;import android.content.Intent;import android.content.pm.ActivityInfo;import android.os.Bundle;import android.util.Log;import android.view.SurfaceView;import android.view.Window;import android.view.WindowManager;public class PlayerActivity extends Activity { private SurfaceView surfaceView; private Player player; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); requestWindowFeature(Window.FEATURE_NO_TITLE); getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN); setContentView(R.layout.activity_player); Intent intent = getIntent(); String path = intent.getStringExtra("path"); Log.d("PlayerActivity", " url:" + path); surfaceView = (SurfaceView) this.findViewById(R.id.surfaceView1); player = new Player(surfaceView, path); } @Override protected void onResume() { if(getRequestedOrientation()!= ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE){ setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE); } super.onResume(); }}
Player.java
package com.example.loushuai.simpleplayer;import android.app.Activity;import android.content.Intent;import android.content.pm.ActivityInfo;import android.os.Bundle;import android.util.Log;import android.view.SurfaceView;import android.view.Window;import android.view.WindowManager;public class PlayerActivity extends Activity { private SurfaceView surfaceView; private Player player; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); requestWindowFeature(Window.FEATURE_NO_TITLE); getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN); setContentView(R.layout.activity_player); Intent intent = getIntent(); String path = intent.getStringExtra("path"); Log.d("PlayerActivity", " url:" + path); surfaceView = (SurfaceView) this.findViewById(R.id.surfaceView1); player = new Player(surfaceView, path); } @Override protected void onResume() { if(getRequestedOrientation()!= ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE){ setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE); } super.onResume(); }}
参考文献:
http://blog.csdn.net/hellogv/article/details/6429455
0 0
- 【Android】如何用MediaPlayer实现一个简单的音视频播放器
- android使用MediaPlayer+Surface实现简单视频播放器
- Android SurfaceView + MediaPlayer实现视频播放器
- Android之多媒体MediaPlayer(一个简单的音乐播放器)
- android MediaPlayer实现简单的音乐播放
- Android实现简单音乐播放器(MediaPlayer)
- Android实现简单音乐播放器(MediaPlayer)
- Android之使用MediaPlayer和SurfaceView组件播放一个简单的视频
- 简单的MediaPlayer+SurfaceView实现视频横竖屏播放
- android Mediaplayer 播放音视频
- Android视频播放器mediaplayer
- android列表中使用MediaPlayer播放视频的一个问题
- 如何用 FFmpeg 编写一个简单播放器.pdf
- Android提高之MediaPlayer播放网络视频的实现方法
- Android之MediaPlayer播放网络视频的实现方法
- 用MediaPlayer+TextureView封装一个实现全屏、小窗口的视频播放器
- Android通过MediaPlayer实现播放视频实例
- Android MediaPlayer+SurfaceView实现视频播放
- 香蕉好处多 但吃香蕉要注意这些禁忌!
- Caffe源码解析4: Data_layer
- Android activity四大启动模式详解
- 安装MySQL时停在start server解决办法
- Ubuntu和XP之间实现共享
- 【Android】如何用MediaPlayer实现一个简单的音视频播放器
- Linux驱动开发-11、设备阻塞访问-等待队列
- Android ContentObserver
- 游戏开发---影子跟随算法
- Java爬虫(一)利用GET和POST发送请求,获取服务器返回信息
- 最小的k个数
- 日志组件之debug动态开关
- 用内置max(),min()获得可迭代对象的最值及其index
- 一个简单的英汉词典小程序