2009-07-27 103 views
9

我正在創建一個錄像機,並且想要創建視頻播放器來預覽錄製的視頻。修改我創建了一個MediaPreview類以下方式代碼from this pageAndroid:如何創建視頻播放器?

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: ===> " + 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 <= 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); 
} 
} 

代碼執行成功(沒有任何例外)until mp.start();,但屏幕是空白的(只有在屏幕上的按鈕)。

有誰知道上面的代碼中可能出現了什麼問題,或者有沒有可用於網絡的任何示例?

我會很感激你的幫助

+0

我也有類似的問題......如果妳有發現任何的解決方案,讓我好心知道 – 2011-04-22 13:43:03

回答

6

即興,我想你需要一些更多的邏輯在一些回調方法,如surfaceCreated()

+1

這個鏈接被刪除。 – 2013-09-05 12:02:27

6

您需要將表面附加到表面創建後播放。

呼叫

mp.setDisplay(mPreview.getHolder()); 

從內

public void surfaceCreated(SurfaceHolder holder) 
相關問題