我们在上一篇文章 如何实现一个简单的文件浏览器的基础上,使用MeidaPlayer实现对音视频文件的播放功能。
主要代码如下:
Manifest文件
<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
<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
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