无法播放mp4视频

问题描述:

我想在android中制作视频播放器。 它播放3GP格式的视频。 但它不支持mp4 video.Below是我在android中的代码相同。为什么它不支持设备和模拟器上的mp4格式?无法播放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; 
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. 
+0

我敢打赌LogCat说没有找到类似文件的东西。 – RvdK 2013-02-21 07:47:16

尝试正确的视频网址有协议:http://www.ooklnet.com/files/368/368007/video.mp4

问题可能与视频编码。 Android Froyo和姜饼不支持“Baseline”H264以外的H264格式。因此,如果您的视频是H264编码,请确保其编码为“AVC基准”。在Windows/Linux中使用一些工具,如“媒体信息”,并检查您的视频编码。如果可能,将视频转换为基线。

另一种解决方法是跳过视频视图并使用视频播放意图并将播放重​​定向到应用程序。系统将提示用户选择播放器来处理播放。显然,如果视频视图不能播放文件,默认播放器也不能处理文件。你可以选择一些其他已安装的播放器,如Mx-Player,它可以完美地播放文件。 希望能解决你的问题。