我们在上一篇文章 如何实现一个简单的文件浏览器的基础上,使用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();
}
}