Android 无法播放mp4视频
我正在尝试用android制作一个视频播放器。 它播放3GP格式的视频。 但它不支持mp4视频。下面是我在android中的代码。为什么它在设备和模拟器上不支持mp4格式Android 无法播放mp4视频,android,mp4,Android,Mp4,我正在尝试用android制作一个视频播放器。 它播放3GP格式的视频。 但它不支持mp4视频。下面是我在android中的代码。为什么它在设备和模拟器上不支持mp4格式 package com.example.videoplayer; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.net.URL
package com.example.videoplayer;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import org.json.JSONObject;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.ProgressDialog;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.StrictMode;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.webkit.URLUtil;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.Toast;
import android.widget.VideoView;
public class VideoViewDemo extends Activity {
private static final String TAG = "VideoViewDemo";
private VideoView mVideoView;
private EditText mPath;
private ImageButton mPlay;
private ImageButton mPause;
private ImageButton mReset;
private ImageButton mStop;
private String current;
@SuppressLint({ "NewApi", "NewApi", "NewApi" })
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.activity_main);
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
mVideoView = (VideoView) findViewById(R.id.surface_view);
mPath = (EditText) findViewById(R.id.path);
mPath.setText("ooklnet.com/files/368/368007/video.mp4");
mPlay = (ImageButton) findViewById(R.id.play);
mPause = (ImageButton) findViewById(R.id.pause);
mReset = (ImageButton) findViewById(R.id.reset);
mStop = (ImageButton) findViewById(R.id.stop);
mPlay.setOnClickListener(new OnClickListener() {
public void onClick(View view) {
playVideo();
}
});
mPause.setOnClickListener(new OnClickListener() {
public void onClick(View view) {
if (mVideoView != null) {
mVideoView.pause();
}
}
});
mReset.setOnClickListener(new OnClickListener() {
public void onClick(View view) {
if (mVideoView != null) {
mVideoView.seekTo(0);
}
}
});
mStop.setOnClickListener(new OnClickListener() {
public void onClick(View view) {
if (mVideoView != null) {
current = null;
mVideoView.stopPlayback();
}
}
});
/*runOnUiThread(new Runnable(){
public void run() {
sleep(2000);
playVideo();
}
});*/
Thread _trd1 = new Thread() {
public void run() {
try {
sleep(2000);
runOnUiThread(new Runnable() {
public void run() {
playVideo();
}
});
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
};
_trd1.start();
// new DoBackgroundTask().execute();
}
public class DoBackgroundTask extends AsyncTask
<String, Void, String> {
@Override
protected void onPreExecute() {
// TODO Auto-generated method stub
}
protected String doInBackground(String... locationNames) {
playVideo();
return null;
}
protected void onPostExecute(String addresses){
}
}
private void playVideo() {
try {
final String path = mPath.getText().toString();
Log.v(TAG, "path: " + path);
if (path == null || path.length() == 0) {
Toast.makeText(VideoViewDemo.this, "File URL/path is empty",
Toast.LENGTH_LONG).show();
} else {
// If the path has not changed, just start the media player
if (path.equals(current) && mVideoView != null) {
mVideoView.start();
mVideoView.requestFocus();
return;
}
current = path;
mVideoView.setVideoPath(getDataSource("ooklnet.com/files/368/368007/video.mp4"));
mVideoView.start();
mVideoView.requestFocus();
}
} catch (Exception e) {
Log.e(TAG, "error: " + e.getMessage(), e);
if (mVideoView != null) {
mVideoView.stopPlayback();
}
}
}
private String getDataSource(String path) throws IOException {
if (!URLUtil.isNetworkUrl(path)) {
return path;
} else {
URL url = new URL(path);
URLConnection cn = url.openConnection();
cn.connect();
InputStream stream = cn.getInputStream();
if (stream == null)
throw new RuntimeException("stream is null");
File temp = File.createTempFile("mediaplayertmp", "dat");
temp.deleteOnExit();
String tempPath = temp.getAbsolutePath();
FileOutputStream out = new FileOutputStream(temp);
byte buf[] = new byte[128];
do {
int numread = stream.read(buf);
if (numread <= 0)
break;
out.write(buf, 0, numread);
} while (true);
try {
stream.close();
} catch (IOException ex) {
Log.e(TAG, "error: " + ex.getMessage(), ex);
}
return tempPath;
}
}
}
Please advise as soon as possible.
Thanks.
package com.example.videoplayer;
导入java.io.File;
导入java.io.FileOutputStream;
导入java.io.IOException;
导入java.io.InputStream;
导入java.net.URL;
导入java.net.URLConnection;
导入java.util.ArrayList;
导入org.json.JSONObject;
导入android.annotation.SuppressLint;
导入android.app.Activity;
导入android.app.ProgressDialog;
导入android.os.AsyncTask;
导入android.os.Bundle;
导入android.os.StrictMode;
导入android.util.Log;
导入android.view.view;
导入android.view.view.OnClickListener;
导入android.webkit.URLUtil;
导入android.widget.EditText;
导入android.widget.ImageButton;
导入android.widget.Toast;
导入android.widget.VideoView;
公共类VideoViewDemo扩展活动{
私有静态最终字符串标记=“VideoViewDemo”;
私有视频视图;
私人编辑文本mPath;
私有图像按钮mPlay;
私人使用;
专用图像按钮mReset;
私人图像按钮mStop;
私有串电流;
@SuppressLint({“NewApi”、“NewApi”、“NewApi”})
@凌驾
创建公共空间(捆绑冰柱){
超级冰柱;
setContentView(R.layout.activity_main);
StrictMode.ThreadPolicy policy=新建StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(策略);
mVideoView=(VideoView)findviewbyd(R.id.surface\u视图);
mPath=(EditText)findViewById(R.id.path);
setText(“ooklnet.com/files/368/368007/video.mp4”);
mPlay=(ImageButton)findViewById(R.id.play);
mPause=(ImageButton)findViewById(R.id.pause);
mReset=(ImageButton)findViewById(R.id.reset);
mStop=(ImageButton)findViewById(R.id.stop);
setOnClickListener(新的OnClickListener(){
公共void onClick(视图){
播放视频();
}
});
setOnClickListener(新的OnClickListener(){
公共void onClick(视图){
如果(mVideoView!=null){
mVideoView.pause();
}
}
});
mReset.setOnClickListener(新的OnClickListener(){
公共void onClick(视图){
如果(mVideoView!=null){
mVideoView.seekTo(0);
}
}
});
setOnClickListener(新的OnClickListener(){
公共void onClick(视图){
如果(mVideoView!=null){
电流=零;
mVideoView.stopPlayback();
}
}
});
/*runOnUiThread(新的Runnable(){
公开募捐{
睡眠(2000年);
播放视频();
}
});*/
线程_trd1=新线程(){
公开募捐{
试一试{
睡眠(2000年);
runOnUiThread(新的Runnable(){
公开募捐{
播放视频();
}
});
}捕捉(中断异常e){
//TODO自动生成的捕捉块
e、 printStackTrace();
}
}
};
_trd1.start();
//新建DoBackgroundTask().execute();
}
公共类DoBackgroundTask扩展了AsyncTask
{
@凌驾
受保护的void onPreExecute(){
//TODO自动生成的方法存根
}
受保护的字符串doInBackground(字符串…位置名称){
播放视频();
返回null;
}
受保护的void onPostExecute(字符串地址){
}
}
私人void playVideo(){
试一试{
最终字符串路径=mPath.getText().toString();
Log.v(标签,“路径:”+path);
if(path==null | | path.length()==0){
Toast.makeText(VideoViewDemo.this,“文件URL/路径为空”,
Toast.LENGTH_LONG).show();
}否则{
//如果路径没有改变,只需启动媒体播放器
if(path.equals(current)&&mVideoView!=null){
mVideoView.start();
mVideoView.requestFocus();
返回;
}
电流=路径;
mVideoView.setVideoPath(getDataSource(“ooklnet.com/files/368/368007/video.mp4”);
mVideoView.start();
mVideoView.requestFocus();
}
}捕获(例外e){
Log.e(标记“error:+e.getMessage(),e);
如果(mVideoView!=null){
mVideoView.stopPlayback();
}
}
}
私有字符串getDataSource(字符串路径)引发IOException{
如果(!URLUtil.isNetworkUrl(路径)){
返回路径;
}否则{
URL=新URL(路径);
URLConnection cn=url.openConnection();
cn.connect();
InputStream=cn.getInputStream();
if(流==null)
抛出新的RuntimeException(“流为null”);
File temp=File.createTempFile(“mediaplayertmp”、“dat”);
temp.deleteOnExit();
字符串tempPath=temp.getAbsolutePath();
FileOutputStream out=新的FileOutputStream(临时);
字节buf[]=新字节[128];
做{
int numread=stream.read(buf);
如果(numread使用协议尝试正确的视频url:http://www.ooklnet.com/files/368/368007/video.mp4
问题可能在于视频编码。Android Froyo和Gingerbread可以