Потоковое воспроизведение мультимедиа Android - PullRequest
0 голосов
/ 08 февраля 2012

Я работаю на Android Media Player, который потоковое видео и отображает.Приложение может буферизовать, но видео не отображается на поверхности, хотя я могу слышать звук.Вот мой код:

 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

     }
 }

1 Ответ

0 голосов
/ 25 июля 2014
@Override
    public void surfaceCreated(SurfaceHolder holder) {
        // TODO Auto-generated method stub
        mp.setDisplay(holder);
        play();
    }
...