2012-02-08 290 views
0

我正在研究Android媒體播放器,它可以流式傳輸視頻和顯示。雖然我能夠聽到聲音,但應用程序能夠緩衝但視頻不能在表面視圖上顯示。 以下是我的代碼:Android媒體播放器流媒體

public class VideoPlayerActivity extends Activity implements OnPreparedListener, OnCompletionListener, OnBufferingUpdateListener, OnSeekCompleteListener, OnErrorListener, SurfaceHolder.Callback { 

    private VideoView videoView; 

    private MediaPlayer mp; 

    private SurfaceView surfaceView; 
    private SurfaceHolder surfaceHolder; 

    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 

     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 

     //  getWindow().setFormat(PixelFormat.UNKNOWN); 

     surfaceView = (SurfaceView) findViewById(R.id.surfaceView1); 

     surfaceHolder = surfaceView.getHolder(); 

     surfaceHolder.addCallback(this); 

     //  surfaceHolder.setFixedSize(176, 144); 
     //  surfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS); 


     try { 

      String path = "http://xxxxxxxxxxx/4_xxx.mp4"; 

      mp = new MediaPlayer(); 

      mp.setDataSource(path); 
      mp.setDisplay(surfaceHolder); 
      mp.prepare(); 

      mp.setOnPreparedListener(this); 
      mp.setOnBufferingUpdateListener(this); 
      mp.setOnCompletionListener(this); 
      mp.setOnSeekCompleteListener(this); 
      mp.setOnErrorListener(this); 
      mp.setAudioStreamType(AudioManager.STREAM_MUSIC); 



     } catch (IllegalArgumentException e) { 

      e.printStackTrace(); 

     } catch (IllegalStateException e) { 

      e.printStackTrace(); 

     } catch (IOException e) { 

      e.printStackTrace(); 

     } 

     mp.start(); 

     //  Uri uri = Uri.parse(path); 
     //   
     //  videoView = (VideoView) findViewById(R.id.videoView1); 
     //   
     //  videoView.setVideoURI(uri); 
     //   
     //  videoView.setMediaController(new MediaController(this)); 
     //   
     //   
     //   
     //  videoView.setOnPreparedListener(this); 
     //   
     //   
     //   
     //  videoView.requestFocus(); 
     //   
     //  videoView.start(); 
    } 

    @Override 
    public void onPrepared(MediaPlayer mp) { 

     System.out.println("=======In on prepared======Media Player=====" + mp); 

    } 

    @Override 
    public void onCompletion(MediaPlayer mp) { 

     System.out.println("=======In on completion====== Media player====" + mp); 

    } 

    @Override 
    public void onBufferingUpdate(MediaPlayer mp, int percent) { 

     System.out.println("=======In on Buffering update======Media Player====" + mp + "====percent====" + percent); 

    } 

    @Override 
    public boolean onError(MediaPlayer mp, int what, int extra) { 


     System.out.println("=======In on Error======Media Player====" + mp + "=====what====" + what + "====extra====" + extra); 

     return false; 
    } 

    @Override 
    public void onSeekComplete(MediaPlayer mp) { 

     System.out.println("=======In on seek complete======Media Player====" + mp); 

    } 

    @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 

    } 
} 
+0

你應該設置顯示在你的'surfaceCreated()'方法 – mangusta 2014-05-26 06:10:48

回答

0
@Override 
    public void surfaceCreated(SurfaceHolder holder) { 
     // TODO Auto-generated method stub 
     mp.setDisplay(holder); 
     play(); 
    }