Android MediaPlayer每隔一次活动中心播放视频

时间:2012-08-19 08:32:19

标签: android android-activity android-mediaplayer android-videoview

我正在编写应用程序,其中一个活动可以播放视频文件。我录制视频,但是当尝试播放麻烦时开始。活动开始并首次播放视频。然后我按回按钮,返回上一个活动,选择视频(相同或其他),再次输入视频播放活动,所有视图(按钮,搜索栏等)显示,但视频不显示。我再回去,选择视频,打开视频播放活动,视频再次播放。所以它每隔一段时间播放一次。我试图调试,但无法找到问题所在,任何帮助将不胜感激。活动代码如下,eclipse的logCat输出也低于。

public class FightPlayerActivity extends Activity implements OnSeekBarChangeListener, SurfaceHolder.Callback, OnPreparedListener {

    private MediaPlayer mp=null;
    private SeekBar seekBar;

    private String filePath;

    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        Toast.makeText(this,"Create ", 2000).show();

        setContentView(R.layout.fight_player);

        filePath=getIntent().getStringExtra("filename");
        filePath=Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM)+"/FightAll_BJJ_Scoring/"+filePath;

        Toast.makeText(this,filePath, 2000).show();
        // seek bar

        seekBar=(SeekBar) findViewById(R.id.seek_bar);
        seekBar.setOnSeekBarChangeListener(this);
        try {
            SurfaceView sv=(SurfaceView) findViewById(id.video_preview);

            SurfaceHolder sh=sv.getHolder();
            sh.addCallback(this);

            sh.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);

        } catch (IllegalArgumentException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (SecurityException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IllegalStateException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }      
    }

    public void surfaceChanged(SurfaceHolder holder, int format, int width,
            int height) {
        // TODO Auto-generated method stub

    }

    public void onStop(View view)
    {
        super.onStop();

        mp.stop();
        mp.reset();
        mp.release();
    }

    public void onStart(View view)
    {
        super.onStart();

    }

    public void stop(View view)
    {
        mp.stop();
    }

    public void pause(View view)
    {
        mp.pause();
    }

    public void play(View view)
    {
        mp.start();
    }

    public void surfaceCreated(SurfaceHolder holder) {

        try {
            mp=new MediaPlayer();
            mp.setDataSource(filePath);
            mp.setDisplay(holder);  
            mp.setOnPreparedListener(this);
            mp.setAudioStreamType(AudioManager.STREAM_MUSIC);
            mp.prepare();

        } catch (IllegalStateException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } 
    }


    public void surfaceDestroyed(SurfaceHolder holder) {
        // TODO Auto-generated method stub

    }

    public void onPrepared(MediaPlayer mediaplayer) {
         mp.start(); 
         seekBar.setMax(mp.getDuration());
    }

    public void onProgressChanged(SeekBar sb,int progress,boolean fromUser)
    {
        //Toast.makeText(this, progress, 2000).show();
        mp.seekTo(progress);
    }

    public void onStartTrackingTouch(SeekBar seekBar) {
        // TODO Auto-generated method stub
        onProgressChanged(seekBar,seekBar.getProgress(),true);
    }

    public void onStopTrackingTouch(SeekBar seekBar) {
        // TODO Auto-generated method stub

 }  

并记录在下面:

08-19 18:26:39.325: E/MediaPlayer(7057): message received msg=200, ext1=1, ext2=44
08-19 18:26:39.325: W/MediaPlayer(7057): info/warning (1, 44)
08-19 18:26:39.325: E/MediaPlayer(7057): callback application
08-19 18:26:39.325: E/MediaPlayer(7057): back from callback
08-19 18:26:39.325: E/MediaPlayer(7057): message received msg=1, ext1=0, ext2=0
08-19 18:26:39.325: E/MediaPlayer(7057): callback application
08-19 18:26:39.325: E/MediaPlayer(7057): back from callback
08-19 18:26:39.361: E/MediaPlayer(7057): message received msg=5, ext1=176, ext2=144
08-19 18:26:39.361: E/MediaPlayer(7057): callback application
08-19 18:26:39.365: E/MediaPlayer(7057): back from callback
08-19 18:26:39.380: I/MediaPlayer(7057): Info (1,44)
08-19 18:26:45.661: E/MediaPlayer(7057): message received msg=200, ext1=1, ext2=44
08-19 18:26:45.661: W/MediaPlayer(7057): info/warning (1, 44)
08-19 18:26:45.661: E/MediaPlayer(7057): callback application
08-19 18:26:45.661: E/MediaPlayer(7057): back from callback
08-19 18:26:45.661: E/MediaPlayer(7057): message received msg=1, ext1=0, ext2=0
08-19 18:26:45.661: E/MediaPlayer(7057): callback application
08-19 18:26:45.661: E/MediaPlayer(7057): back from callback
08-19 18:26:45.708: I/MediaPlayer(7057): Info (1,44)
08-19 18:26:45.779: W/IMediaDeathNotifier(7057): media server died
08-19 18:26:45.779: E/MediaPlayer(7057): message received msg=100, ext1=100, ext2=0
08-19 18:26:45.779: E/MediaPlayer(7057): error (100, 0)
08-19 18:26:45.779: E/MediaPlayer(7057): callback application
08-19 18:26:45.779: E/MediaPlayer(7057): back from callback
08-19 18:26:45.779: E/MediaPlayer(7057): message received msg=100, ext1=100, ext2=0
08-19 18:26:45.779: E/MediaPlayer(7057): error (100, 0)
08-19 18:26:45.779: E/MediaPlayer(7057): callback application
08-19 18:26:45.779: E/MediaPlayer(7057): back from callback
08-19 18:26:45.779: W/Camera(7057): Camera server died!
08-19 18:26:45.786: E/MediaPlayer(7057): Error (100,0)
08-19 18:26:45.790: E/MediaPlayer(7057): Error (100,0)

1 个答案:

答案 0 :(得分:0)

已解决..必须将初始化代码从onCreate()移动到onStart()并从onStart()中删除“View view”。