Note that there are some explanatory texts on larger screens.

plurals
  1. POAndroid: How to create video player?
    primarykey
    data
    text
    <p>I am creating a video recorder and would like to create video player to preview the recorded videos. Modifying the code <a href="http://davanum.wordpress.com/2007/12/29/android-videomusic-player-sample-from-local-disk-as-well-as-remote-urls/" rel="noreferrer">from this page</a> I have created a MediaPreview class the following way:</p> <pre><code>public class MediaPreview extends Activity implements OnErrorListener, OnBufferingUpdateListener, OnCompletionListener, OnPreparedListener, SurfaceHolder.Callback{ private static final String TAG = "MediaPreview"; private MediaPlayer mp; private SurfaceView mPreview; private SurfaceHolder holder; private Button btnPlay; private Button btnPause; private Button btnReset; private Button btnStop; private String mPath; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.media_preview); mPreview = (SurfaceView)findViewById(R.id.mPreview); btnPlay = (Button)findViewById(R.id.btnPlay); btnPause = (Button)findViewById(R.id.btnPause); btnReset = (Button)findViewById(R.id.btnReset); btnStop = (Button)findViewById(R.id.btnStop); getPathFromParentDialog(); btnPlay.setOnClickListener(new View.OnClickListener(){ @Override public void onClick(View v) { playVideo(); } }); btnPause.setOnClickListener(new View.OnClickListener(){ @Override public void onClick(View v) { // TODO Auto-generated method stub if(mp != null){ mp.pause(); } } }); btnReset.setOnClickListener(new View.OnClickListener(){ @Override public void onClick(View v) { if(mp != null){ mp.seekTo(0); } } }); btnStop.setOnClickListener(new View.OnClickListener(){ @Override public void onClick(View v) { if(mp != null){ mp.stop(); mp.release(); } } }); getWindow().setFormat(PixelFormat.TRANSPARENT); holder = mPreview.getHolder(); holder.addCallback(this); holder.setFixedSize(400, 300); } @Override protected void onResume() { setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE); super.onResume(); } @Override public boolean onError(MediaPlayer mp, int what, int extra) { if(mp != null){ mp.stop(); mp.release(); } return false; } @Override public void onBufferingUpdate(MediaPlayer mp, int percent) { // TODO Auto-generated method stub } @Override public void onCompletion(MediaPlayer mp) { // TODO Auto-generated method stub } @Override public void onPrepared(MediaPlayer mp) { // TODO Auto-generated method stub } @Override public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) { // TODO Auto-generated method stub } @Override public void surfaceCreated(SurfaceHolder holder) { // TODO Auto-generated method stub } @Override public void surfaceDestroyed(SurfaceHolder holder) { // TODO Auto-generated method stub } private void playVideo() { try{ mp = new MediaPlayer(); mp.setOnErrorListener(this); mp.setOnBufferingUpdateListener(this); mp.setOnCompletionListener(this); mp.setOnPreparedListener(this); mp.setAudioStreamType(2); mp.setDisplay(mPreview.getHolder()); Runnable r = new Runnable(){ @Override public void run() { try{ setDataSource(mPath); } catch(Exception ex){ Log.e(TAG, ex.getMessage()); } try { mp.prepare(); Log.v(TAG, "Duration: ===&gt; " + mp.getDuration()); } catch (IllegalStateException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); Log.e(TAG, e.getMessage()); } mp.start(); } }; new Thread(r).start(); } catch(Exception ex){ String sDummy = ex.toString(); if(mp != null){ mp.stop(); mp.release(); } } } private void setDataSource(String path) throws IOException { if(!URLUtil.isNetworkUrl(mPath)){ mp.setDataSource(mPath); } else{ URL url = new URL(mPath); URLConnection cn = url.openConnection(); cn.connect(); InputStream stream = cn.getInputStream(); if(stream == null){ throw new RuntimeException("stream is null"); } File fileTemp = File.createTempFile("mediaplayerTmp", "dat"); String tempPath = fileTemp.getAbsolutePath(); FileOutputStream out = new FileOutputStream(fileTemp); byte buf[] = new byte[128]; do{ int numRead = stream.read(buf); if(numRead &lt;= 0){ break; } out.write(buf, 0, numRead); }while(true); mp.setDataSource(tempPath); try{ stream.close(); } catch(Exception ex){ String sDummy = ex.toString(); } } } private void getPathFromParentDialog() { Intent intent = getIntent(); mPath = intent.getExtras().getString(MediaLibrary.FILENAME); } } </code></pre> <p>The code successfully executes (without any exceptions) <code>until mp.start();</code>, but the screen is blank (there are only buttons on the screen).</p> <p>Does anyone know what could be wrong in the code above or is there any example that works available on the web?</p> <p>I would really appreciate your help</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload