一下代码就是对最新vlc1.7.5库的用法
PlayerActivity类就是实现简单直播流rtsp 播放,暂停,静音,seekto功能
的MediaPlayer类是对VLC库的简单封装供上层调用。
下载地址 https://yunpan.cn/cRgzQuxuBPGDm (提取码:c037)
此demo直播的地址是自己搭建的地址 用者可以修改成自己的直播地址
如果没有rtsp的直播流 可以自己搭建一个rtsp的直播流操作步骤如下地址
http://jingyan.baidu.com/article/4f34706ed38610e387b56d38.html
/ ** 版权所有作者xupingyong
* /
package libvlc.vlc_player_175;
import android.app.Activity;
import android.content.res.Configuration;
import android.media.AudioManager;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import org.videolan.libvlc.media.MediaPlayer;
public class PlayerActivity extends Activity implements SurfaceHolder.Callback {
/** Called when the activity is first created. */
MediaPlayer player;
SurfaceView surface;
SurfaceHolder mSurfaceHolder;
private Button play , pause , mute ,no_mute , next;
private int mVideoHeight;
private int mVideoWidth;
private int mVideoVisibleHeight;
private int mVideoVisibleWidth;
private int mSarNum;
private int mSarDen;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
surface=(SurfaceView)findViewById(R.id.video);
play = (Button) findViewById(R.id.play);
pause = (Button) findViewById(R.id.pause);
mute = (Button) findViewById(R.id.mute);
no_mute = (Button) findViewById(R.id.no_mute);
next = (Button) findViewById(R.id.next);
pause.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
player.pause();
}
});
play.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
player.start();
}
});
mute.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
player.setVolume(0,0);
}
});
no_mute.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
player.setVolume(20 , 20);
}
});
next.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int currentTime = player.getCurrentPosition();
int duration = player.getDuration();
player.seekTo(currentTime + 100);
Log.i("currentTime:",currentTime+","+duration);
}
});
mSurfaceHolder=surface.getHolder();//SurfaceHolder是SurfaceView的控制接口
mSurfaceHolder.addCallback(this); //因为这个类实现了SurfaceHolder.Callback接口,所以回调参数直接this
//mSurfaceHolder.setFixedSize(320, 220);//显示的分辨率,不设置为视频默认
mSurfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);//Surface类型
mSurfaceHolder.getSurface();
surface.setKeepScreenOn(true);
player = org.videolan.libvlc.media.MediaPlayer.create(this, Uri.parse("rtsp://192.168.2.14:8554/aa"),mSurfaceHolder);
}
@Override
public void surfaceCreated(SurfaceHolder arg0) {
//必须在surface创建后才能初始化MediaPlayer,否则不会显示图像
if (player == null){
player = org.videolan.libvlc.media.MediaPlayer.create(this, Uri.parse("rtsp://192.168.2.14:8554/aa"),mSurfaceHolder);
}
//player=new MediaPlayer();
player.setAudioStreamType(AudioManager.STREAM_MUSIC);
player.setDisplay(mSurfaceHolder);
//设置显示视频显示在SurfaceView上
try {
player.setDataSource(this,Uri.parse("rtsp://192.168.2.14:8554/aa"));
player.start();
} catch (Exception e) {
e.printStackTrace();
}
}
@Override
protected void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
if(player.isPlaying()){
player.stop();
}
player.release();
//Activity销毁时停止播放,释放资源。不做这个操作,即使退出还是能听到视频播放的声音
}
@Override
public void onConfigurationChanged(Configuration newConfig) {
setSurfaceSize(mVideoWidth, mVideoHeight, mVideoVisibleWidth, mVideoVisibleHeight, mSarNum, mSarDen);
super.onConfigurationChanged(newConfig);
}
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
mSurfaceHolder = holder;
if (player != null) {
//player.setDisplay(mSurfaceHolder);//, width, height
}
if (width > 0) {
mVideoHeight = height;
mVideoWidth = width;
}
}
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
}
//@Override
public void setSurfaceSize(int width, int height, int visible_width, int visible_height, int sar_num, int sar_den) {
mVideoHeight = height;
mVideoWidth = width;
mVideoVisibleHeight = visible_height;
mVideoVisibleWidth = visible_width;
mSarNum = sar_num;
mSarDen = sar_den;
mHandler.removeMessages(HANDLER_SURFACE_SIZE);
mHandler.sendEmptyMessage(HANDLER_SURFACE_SIZE);
}
private static final int HANDLER_BUFFER_START = 1;
private static final int HANDLER_BUFFER_END = 2;
private static final int HANDLER_SURFACE_SIZE = 3;
private static final int SURFACE_BEST_FIT = 0;
private static final int SURFACE_FIT_HORIZONTAL = 1;
private static final int SURFACE_FIT_VERTICAL = 2;
private static final int SURFACE_FILL = 3;
private static final int SURFACE_16_9 = 4;
private static final int SURFACE_4_3 = 5;
private static final int SURFACE_ORIGINAL = 6;
private int mCurrentSize = SURFACE_BEST_FIT;
private Handler mHandler = new Handler() {
@Override
public void handleMessage(Message msg) {
switch (msg.what) {
case HANDLER_BUFFER_START:
//showLoading();
break;
case HANDLER_BUFFER_END:
//hideLoading();
break;
case HANDLER_SURFACE_SIZE:
changeSurfaceSize();
break;
}
}
};
private void changeSurfaceSize() {
// get screen size
int dw = getWindowManager().getDefaultDisplay().getWidth();
int dh = getWindowManager().getDefaultDisplay().getHeight();
// calculate aspect ratio
double ar = (double) mVideoWidth / (double) mVideoHeight;
// calculate display aspect ratio
double dar = (double) dw / (double) dh;
switch (mCurrentSize) {
case SURFACE_BEST_FIT:
if (dar < ar)
dh = (int) (dw / ar);
else
dw = (int) (dh * ar);
break;
case SURFACE_FIT_HORIZONTAL:
dh = (int) (dw / ar);
break;
case SURFACE_FIT_VERTICAL:
dw = (int) (dh * ar);
break;
case SURFACE_FILL:
break;
case SURFACE_16_9:
ar = 16.0 / 9.0;
if (dar < ar)
dh = (int) (dw / ar);
else
dw = (int) (dh * ar);
break;
case SURFACE_4_3:
ar = 4.0 / 3.0;
if (dar < ar)
dh = (int) (dw / ar);
else
dw = (int) (dh * ar);
break;
case SURFACE_ORIGINAL:
dh = mVideoHeight;
dw = mVideoWidth;
break;
}
mSurfaceHolder.setFixedSize(mVideoWidth, mVideoHeight);
ViewGroup.LayoutParams lp = surface.getLayoutParams();
lp.width = dw;
lp.height = dh;
surface.setLayoutParams(lp);
surface.invalidate();
}
}
/*****************************************************************************
* MediaPlayer.java
*****************************************************************************
* Copyright © 2015 VLC authors and VideoLAN
*
* Authors Jean-Baptiste Kempf @videolan.org>
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation; either version 2.1 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
*****************************************************************************/
package org.videolan.libvlc.media;
import android.content.Context;
import android.media.AudioAttributes;
import android.media.MediaFormat;
import android.media.TimedText;
import android.net.Uri;
import android.os.Parcel;
import android.os.Parcelable;
import android.view.Surface;
import android.view.SurfaceHolder;
import org.videolan.libvlc.LibVLC;
import org.videolan.libvlc.Media;
import java.io.FileDescriptor;
import java.io.IOException;
import java.util.Map;
public class MediaPlayer
{
public static final int MEDIA_ERROR_UNKNOWN = 1;
public static final int MEDIA_ERROR_SERVER_DIED = 100;
public static final int MEDIA_ERROR_NOT_VALID_FOR_PROGRESSIVE_PLAYBACK = 200;
public static final int MEDIA_ERROR_IO = -1004;
public static final int MEDIA_ERROR_MALFORMED = -1007;
public static final int MEDIA_ERROR_UNSUPPORTED = -1010;
public static final int MEDIA_ERROR_TIMED_OUT = -110;
public static final int MEDIA_INFO_UNKNOWN = 1;
public static final int MEDIA_INFO_STARTED_AS_NEXT = 2;
public static final int MEDIA_INFO_VIDEO_RENDERING_START = 3;
public static final int MEDIA_INFO_VIDEO_TRACK_LAGGING = 700;
public static final int MEDIA_INFO_BUFFERING_START = 701;
public static final int MEDIA_INFO_BUFFERING_END = 702;
public static final int MEDIA_INFO_BAD_INTERLEAVING = 800;
public static final int MEDIA_INFO_NOT_SEEKABLE = 801;
public static final int MEDIA_INFO_METADATA_UPDATE = 802;
public static final int MEDIA_INFO_EXTERNAL_METADATA_UPDATE = 803;
public static final int MEDIA_INFO_TIMED_TEXT_ERROR = 900;
public static final int MEDIA_INFO_UNSUPPORTED_SUBTITLE = 901;
public static final int MEDIA_INFO_SUBTITLE_TIMED_OUT = 902;
public static final int VIDEO_SCALING_MODE_SCALE_TO_FIT = 1;
public static final int VIDEO_SCALING_MODE_SCALE_TO_FIT_WITH_CROPPING = 2;
private Media mCurrentMedia = null;
private final LibVLC mLibVLC;
private org.videolan.libvlc.MediaPlayer mMediaPlayer;
public MediaPlayer() {
mLibVLC = new LibVLC(); //FIXME, this is wrong
mMediaPlayer = new org.videolan.libvlc.MediaPlayer(mLibVLC);
}
public static MediaPlayer create(Context context, Uri uri) {
return create (context, uri, null);
}
public static MediaPlayer create(Context context, Uri uri, SurfaceHolder holder) {
return create(context, uri, holder, null, 0);
}
public static MediaPlayer create(Context context, Uri uri, SurfaceHolder holder,
AudioAttributes audioAttributes, int audioSessionId) {
MediaPlayer player = new MediaPlayer();
//player.setDataSource(context, uri); This throws exception, but not this create()
return player;
}
public static MediaPlayer create(Context context, int resid) {
return create(context, resid, null, 0);
}
public static MediaPlayer create(Context context, int resid,
AudioAttributes audioAttributes, int audioSessionId) {
return null;
}
public void setDataSource(Context context, Uri uri)
throws IOException, IllegalArgumentException, SecurityException, IllegalStateException {
setDataSource(context, uri, null);
}
// FIXME, this is INCORRECT, @headers are ignored
public void setDataSource(Context context, Uri uri, Map headers)
throws IOException, IllegalArgumentException, SecurityException, IllegalStateException {
mCurrentMedia = new Media(mLibVLC, uri);
mMediaPlayer.setMedia(mCurrentMedia);
}
public void setDataSource(String path)
throws IOException, IllegalArgumentException, SecurityException, IllegalStateException {
mCurrentMedia = new Media(mLibVLC, path);
mMediaPlayer.setMedia(mCurrentMedia);
}
public void setDataSource(FileDescriptor fd)
throws IOException, IllegalArgumentException, IllegalStateException {
mCurrentMedia = new Media(mLibVLC, fd);
mMediaPlayer.setMedia(mCurrentMedia);
}
// FIXME, this is INCORRECT, @offset and @length are ignored
public void setDataSource(FileDescriptor fd, long offset, long length)
throws IOException, IllegalArgumentException, IllegalStateException {
setDataSource(fd);
}
public void prepare() throws IOException, IllegalStateException {
}
public void prepareAsync() {
mCurrentMedia.addOption(":video-paused");
mMediaPlayer.play();
}
public void setDisplay(SurfaceHolder sh) {
mMediaPlayer.getVLCVout().setVideoSurface(sh.getSurface(), sh);
mMediaPlayer.getVLCVout().attachViews();
}
public void setSurface(Surface surface) {
mMediaPlayer.getVLCVout().setVideoSurface(surface, null);
}
public void setVideoScalingMode(int mode) {
}
public void start() throws IllegalStateException {
mMediaPlayer.play();
}
public void stop() throws IllegalStateException {
mMediaPlayer.stop();
}
public void pause() throws IllegalStateException {
// FIXME, this is toggling for now.
mMediaPlayer.pause();
}
public void setWakeMode(Context context, int mode) {
}
public void setScreenOnWhilePlaying(boolean screenOn) {
}
public int getVideoWidth() {
return -1;
}
public int getVideoHeight() {
return -1;
}
public boolean isPlaying() {
return mMediaPlayer.isPlaying();
}
public void seekTo(int msec) throws IllegalStateException {
mMediaPlayer.setTime(msec);
}
// This is of course, less precise than VLC
public int getCurrentPosition() {
return (int)mMediaPlayer.getTime();
}
// This is of course, less precise than VLC
public int getDuration() {
return (int)mMediaPlayer.getLength();
}
public void setNextMediaPlayer(MediaPlayer next) {
}
public void release() {
mMediaPlayer.release();
}
public void reset() {
}
public void setAudioStreamType(int streamtype) {
}
public void setAudioAttributes(AudioAttributes attributes) throws IllegalArgumentException {
}
public void setLooping(boolean looping) {
}
public boolean isLooping() {
return false;
}
public void setVolume(float leftVolume, float rightVolume) {
mMediaPlayer.setVolume( (int)((leftVolume + rightVolume) * 100/2));
}
public void setAudioSessionId(int sessionId) throws IllegalArgumentException, IllegalStateException {
}
public int getAudioSessionId() {
return 0;
}
public void attachAuxEffect(int effectId) {
}
public void setAuxEffectSendLevel(float level) {
}
static public class TrackInfo implements Parcelable {
public static final int MEDIA_TRACK_TYPE_UNKNOWN = 0;
public static final int MEDIA_TRACK_TYPE_VIDEO = 1;
public static final int MEDIA_TRACK_TYPE_AUDIO = 2;
public static final int MEDIA_TRACK_TYPE_TIMEDTEXT = 3;
public static final int MEDIA_TRACK_TYPE_SUBTITLE = 4;
TrackInfo(Parcel in) {
}
public int getTrackType() {
return MEDIA_TRACK_TYPE_UNKNOWN;
}
public String getLanguage() {
return "und";
}
public MediaFormat getFormat() {
return null;
}
@Override
public int describeContents() {
return 0;
}
@Override
public void writeToParcel(Parcel dest, int flags) {
}
@Override
public String toString() {
return "";
}
}
public TrackInfo[] getTrackInfo() throws IllegalStateException {
//FIXME
TrackInfo trackInfo[] = new TrackInfo[1];
return trackInfo;
}
public static final String MEDIA_MIMETYPE_TEXT_SUBRIP = "application/x-subrip";
public void addTimedTextSource(String path, String mimeType) {
mMediaPlayer.setSubtitleFile(path);
}
// FIXME: This is incorrect, since libVLC can only add local subtitles
public void addTimedTextSource(Context context, Uri uri, String mimeType) {
mMediaPlayer.setSubtitleFile(uri.getPath());
}
public void addTimedTextSource(FileDescriptor fd, String mimeType)
throws IllegalArgumentException, IllegalStateException {
}
public void addTimedTextSource(FileDescriptor fd, long offset, long length, String mime)
throws IllegalArgumentException, IllegalStateException {
}
public int getSelectedTrack(int trackType) throws IllegalStateException {
return 0;
}
public void selectTrack(int index) throws IllegalStateException {
}
public void deselectTrack(int index) throws IllegalStateException {
}
@Override
protected void finalize() {}
public interface OnPreparedListener
{
void onPrepared(MediaPlayer mp);
}
public void setOnPreparedListener(OnPreparedListener listener) {
}
public interface OnCompletionListener
{
void onCompletion(MediaPlayer mp);
}
public void setOnCompletionListener(OnCompletionListener listener) {
}
public interface OnBufferingUpdateListener
{
void onBufferingUpdate(MediaPlayer mp, int percent);
}
public void setOnBufferingUpdateListener(OnBufferingUpdateListener listener) {
}
public interface OnSeekCompleteListener
{
public void onSeekComplete(MediaPlayer mp);
}
public void setOnSeekCompleteListener(OnSeekCompleteListener listener) {
}
public interface OnVideoSizeChangedListener
{
public void onVideoSizeChanged(MediaPlayer mp, int width, int height);
}
public void setOnVideoSizeChangedListener(OnVideoSizeChangedListener listener) {
}
public interface OnTimedTextListener
{
public void onTimedText(MediaPlayer mp, TimedText text);
}
public void setOnTimedTextListener(OnTimedTextListener listener) {
}
public interface OnErrorListener
{
boolean onError(MediaPlayer mp, int what, int extra);
}
public void setOnErrorListener(OnErrorListener listener) {
}
public interface OnInfoListener
{
boolean onInfo(MediaPlayer mp, int what, int extra);
}
public void setOnInfoListener(OnInfoListener listener) {
}
}