setLooping在我的视频播放器中不起作用

时间:2012-09-01 14:58:33

标签: android

我是Android新手。我想设计我的个人视频播放器来播放流媒体音乐,所以我使用MediaPlayer和SurfaceView。该项目成功播放流媒体视频,但无法进行环路播放。我有谷歌很多文章和阅读Android开发人员。我无法弄清楚为什么mediaPlayer.setLooping(true)在这里不起作用。感谢^^

mediaPlayer.reset();
mediaPlayer.setDataSource(videoUrl);
mediaPlayer.setLooping(true);
mediaPlayer.prepare();    

代码包括两部分:1.Test_Videoplayer.java 2.Player.java

1.Test_Videoplayer.java

public class Test_Videoplayer extends Activity {
private SurfaceView surfaceView;
private Button btnPause, btnPlayUrl, btnStop;
private SeekBar skbProgress;
private Player player;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
    surfaceView = (SurfaceView) this.findViewById(R.id.surfaceView1);

    btnPlayUrl = (Button) this.findViewById(R.id.btnPlayUrl);
    btnPlayUrl.setOnClickListener(new ClickEvent());

    btnPause = (Button) this.findViewById(R.id.btnPause);
    btnPause.setOnClickListener(new ClickEvent());

    btnStop = (Button) this.findViewById(R.id.btnStop);
    btnStop.setOnClickListener(new ClickEvent());

    skbProgress = (SeekBar) this.findViewById(R.id.skbProgress);
    skbProgress.setOnSeekBarChangeListener(new SeekBarChangeEvent());

    player = new Player(surfaceView, skbProgress);

}

class ClickEvent implements OnClickListener {
    @Override
    public void onClick(View arg0) {
        if (arg0 == btnPause) {
            player.pause();
        } else if (arg0 == btnPlayUrl) {
            String url = "http://sites.google.com/site/ronforwork/Home/android-2/littleMonster.3gp";
            player.playUrl(url);
        } else if (arg0 == btnStop) {
            player.stop();
        }
    }
}

class SeekBarChangeEvent implements SeekBar.OnSeekBarChangeListener {
    int progress;

    @Override
    public void onProgressChanged(SeekBar seekBar, int progress,
            boolean fromUser) {
        this.progress = progress * player.mediaPlayer.getDuration()
                / seekBar.getMax();
    }

    @Override
    public void onStartTrackingTouch(SeekBar seekBar) {
    }

    @Override
    public void onStopTrackingTouch(SeekBar seekBar) {
        player.mediaPlayer.seekTo(progress);
    }
}

}

2.Player.java< - 这里是mediaPlayer.setLooping(true)

public class Player implements OnBufferingUpdateListener, OnCompletionListener,
    MediaPlayer.OnPreparedListener, SurfaceHolder.Callback {

private int videoWidth;
private int videoHeight;

public MediaPlayer mediaPlayer;

private SurfaceHolder surfaceHolder;
private SeekBar skbProgress;

private Timer mTimer = new Timer();

public Player(SurfaceView surfaceView, SeekBar skbProgress) {
    this.skbProgress = skbProgress;

    surfaceHolder = surfaceView.getHolder();
    surfaceHolder.addCallback(this);
    surfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);

    mTimer.schedule(mTimerTask, 0, 1000);
}

TimerTask mTimerTask = new TimerTask() {
    @Override
    public void run() {
        if (mediaPlayer == null)
            return;
        if (mediaPlayer.isPlaying() && skbProgress.isPressed() == false) {
            handleProgress.sendEmptyMessage(0);
        }
    }
};

Handler handleProgress = new Handler() {
    public void handleMessage(Message msg) {
        int position = mediaPlayer.getCurrentPosition();
        int duration = mediaPlayer.getDuration();

        if (duration > 0) {
            long pos = skbProgress.getMax() * position / duration;
            skbProgress.setProgress((int) pos);
        }
    };
};

public void play() {
    mediaPlayer.start();
}

public void playUrl(String videoUrl) {
    try {
        mediaPlayer.reset();
        mediaPlayer.setDataSource(videoUrl);
                    mediaPlayer.setLooping(true);
        mediaPlayer.prepare();
    } catch (IllegalArgumentException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IllegalStateException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

public void pause() {
    mediaPlayer.pause();
}

public void stop() {
    if (mediaPlayer != null) {
        mediaPlayer.stop();
        mediaPlayer.release();
        mediaPlayer = null;
    }
}

@Override
public void surfaceChanged(SurfaceHolder arg0, int arg1, int arg2, int arg3) {
    Log.e("mediaPlayer", "surface changed");
}

@Override
public void surfaceCreated(SurfaceHolder arg0) {
    try {
        mediaPlayer = new MediaPlayer();
        mediaPlayer.setDisplay(surfaceHolder);
        mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
        mediaPlayer.setOnBufferingUpdateListener(this);
        mediaPlayer.setOnPreparedListener(this);
    } catch (Exception e) {
        Log.e("mediaPlayer", "error", e);
    }
    Log.e("mediaPlayer", "surface created");
}

@Override
public void surfaceDestroyed(SurfaceHolder arg0) {
    Log.e("mediaPlayer", "surface destroyed");
}

@Override
public void onPrepared(MediaPlayer arg0) {
    videoWidth = mediaPlayer.getVideoWidth();
    videoHeight = mediaPlayer.getVideoHeight();
    if (videoHeight != 0 && videoWidth != 0) {
        arg0.start();
    }
    Log.e("mediaPlayer", "onPrepared");
}

@Override
public void onCompletion(MediaPlayer arg0) {
    // TODO Auto-generated method stub
}

@Override
public void onBufferingUpdate(MediaPlayer arg0, int bufferingProgress) {
    skbProgress.setSecondaryProgress(bufferingProgress);
    int currentProgress = skbProgress.getMax()
            * mediaPlayer.getCurrentPosition() / mediaPlayer.getDuration();
    Log.e(currentProgress + "% play", bufferingProgress + "% buffer");
}

}

0 个答案:

没有答案