android媒体播放器显示音频但不显示视频
我正在使用媒体播放器播放视频。它只播放音频,不播放视频。有人能帮忙吗?我的代码如下android媒体播放器显示音频但不显示视频,android,streaming,media-player,Android,Streaming,Media Player,我正在使用媒体播放器播放视频。它只播放音频,不播放视频。有人能帮忙吗?我的代码如下 public class VideoViewApplication extends Application { @Override public void onCreate() { } @Override public void onTerminate() { } } public class VideoViewDemo extends Activity i
public class VideoViewApplication extends Application {
@Override
public void onCreate() {
}
@Override
public void onTerminate() {
}
}
public class VideoViewDemo extends Activity implements
OnErrorListener,OnBufferingUpdateListener, OnCompletionListener,
MediaPlayer.OnPreparedListener, SurfaceHolder.Callback {
private static final String TAG = "VideoViewDemo";
private MediaPlayer mp;
private EditText mPath;
private SurfaceHolder holder;
private ImageButton mPlay;
private ImageButton mPause;
private ImageButton mReset;
private ImageButton mStop;
private String current;
private SurfaceView mPreview;
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.main);
mPreview = (SurfaceView) findViewById(R.id.surface);
mPath = (EditText) findViewById(R.id.path);
mPath.setText("rtsp://video2.americafree.tv/AFTVHorrorH26496.sdp");
mPlay = (ImageButton) findViewById(R.id.play);
mPause = (ImageButton) findViewById(R.id.pause);
mReset = (ImageButton) findViewById(R.id.reset);
mStop = (ImageButton) findViewById(R.id.stop);
mPlay.setOnClickListener(new OnClickListener() {
public void onClick(View view) {
playVideo();
}
});
mPause.setOnClickListener(new OnClickListener() {
public void onClick(View view) {
if (mp != null) {
mp.pause();
}
}
});
mReset.setOnClickListener(new OnClickListener() {
public void onClick(View view) {
if (mp != null) {
mp.seekTo(0);
}
}
});
mStop.setOnClickListener(new OnClickListener() {
public void onClick(View view) {
if (mp != null) {
current = null;
mp.stop();
mp.release();
}
}
});
// getWindow().setFormat(PixelFormat.TRANSPARENT);
holder = mPreview.getHolder();
holder.addCallback(this);
holder.setFixedSize(100, 100);
runOnUiThread(new Runnable(){
public void run(){
playVideo();
}
});
}
private void playVideo() {
try {
final String path = mPath.getText().toString();
Log.v(TAG, "path: " + path);
if (path.equals(current) && mp != null) {
mp.start();
return;
}
current = path;
mp = new MediaPlayer();
mp.setOnErrorListener(this);
mp.setOnBufferingUpdateListener(this);
mp.setOnCompletionListener(this);
mp.setOnPreparedListener(this);
mp.setAudioStreamType(AudioManager.STREAM_MUSIC);
mp.setScreenOnWhilePlaying(true);
mp.setDisplay(mPreview.getHolder());
mp.setDataSource(path);
mp.prepare();
Log.v(TAG, "Duration: ===>" + mp.getDuration());
mp.start();
} catch (Exception e) {
Log.e(TAG, "error: "+ e.getMessage(), e);
if (mp != null) {
mp.stop();
mp.release();
}
}
}
public void surfaceChanged(SurfaceHolder arg0, int arg1, int arg2, int arg3) {
Log.d(TAG, "surfaceChanged called");
}
public void surfaceCreated(SurfaceHolder arg0) {
Log.d(TAG, "surfaceCreated called");
}
public void surfaceDestroyed(SurfaceHolder arg0) {
Log.d(TAG, "surfaceDestroyed called");
}
public void onPrepared(MediaPlayer arg0) {
Log.d(TAG, "onPrepared called");
}
public void onCompletion(MediaPlayer arg0) {
Log.d(TAG, "onCompletion called");
}
public void onBufferingUpdate(MediaPlayer mediaPlayer, int percent) {
Log.d(TAG, "onBufferingUpdate called ---> percent:" + percent);
}
public boolean onError(MediaPlayer mediaPlayer, int what, int extra) {
Log.e(TAG, "onError---> what:"+what+" extra:"+extra);
if (mediaPlayer != null) {
mediaPlayer.stop();
mediaPlayer.release();
}
return true;
}
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<EditText android:id="@+id/path"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
<SurfaceView
android:id="@+id/surface"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
</SurfaceView>
<LinearLayout
android:orientation="horizontal"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
>
<ImageButton android:id="@+id/play"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:src="@drawable/play"/>
<ImageButton android:id="@+id/pause"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:src="@drawable/pause"/>
<ImageButton android:id="@+id/reset"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:src="@drawable/reset"/>
<ImageButton android:id="@+id/stop"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:src="@drawable/stop"/>
</LinearLayout>
</LinearLayout>
公共类VideoViewApplication扩展应用程序{
@凌驾
public void onCreate(){
}
@凌驾
公共无效终止(){
}
}
公共类VideoViewDemo扩展了活动实现
OnErrorListener、OnBufferingUpdateListener、OnCompletionListener、,
MediaPlayer.OnPreparedListener,SurfaceHolder.Callback{
私有静态最终字符串标记=“VideoViewDemo”;
私人媒体播放器;
私人编辑文本mPath;
私人土地持有人;
私有图像按钮mPlay;
私人使用;
专用图像按钮mReset;
私人图像按钮mStop;
私有串电流;
私人SurfaceView mPreview;
@凌驾
创建公共空间(捆绑冰柱){
超级冰柱;
setContentView(R.layout.main);
mPreview=(SurfaceView)findViewById(R.id.surface);
mPath=(EditText)findViewById(R.id.path);
mPath.setText(“rtsp://video2.americafree.tv/AFTVHorrorH26496.sdp");
mPlay=(ImageButton)findViewById(R.id.play);
mPause=(ImageButton)findViewById(R.id.pause);
mReset=(ImageButton)findViewById(R.id.reset);
mStop=(ImageButton)findViewById(R.id.stop);
setOnClickListener(新的OnClickListener(){
公共void onClick(视图){
播放视频();
}
});
setOnClickListener(新的OnClickListener(){
公共void onClick(视图){
如果(mp!=null){
mp.pause();
}
}
});
mReset.setOnClickListener(新的OnClickListener(){
公共void onClick(视图){
如果(mp!=null){
希克托议员(0);
}
}
});
setOnClickListener(新的OnClickListener(){
公共void onClick(视图){
如果(mp!=null){
电流=零;
mp.stop();
mp.release();
}
}
});
//getWindow().setFormat(PixelFormat.TRANSPARENT);
holder=mPreview.getHolder();
holder.addCallback(本);
支架。设置固定尺寸(100100);
runOnUiThread(新的Runnable(){
公开募捐{
播放视频();
}
});
}
私人void playVideo(){
试一试{
最终字符串路径=mPath.getText().toString();
Log.v(标签,“路径:”+path);
if(路径等于(当前)&&mp!=null){
mp.start();
返回;
}
电流=路径;
mp=新媒体播放器();
mp.SetOneRorListener(本);
mp.setOnBufferingUpdateListener(本文件);
mp.setOnCompletionListener(此);
mp.setOnPreparedListener(此);
mp.setAudioStreamType(AudioManager.STREAM_MUSIC);
mp.setScreenOnWhilePlaying(正确);
setDisplay(mPreview.getHolder());
mp.setDataSource(路径);
mp.prepare();
Log.v(标记“Duration:=>”+mp.getDuration());
mp.start();
}捕获(例外e){
Log.e(标记“error:+e.getMessage(),e);
如果(mp!=null){
mp.stop();
mp.release();
}
}
}
公共无效表面更改(表面更改arg0、int arg1、int arg2、int arg3){
Log.d(标签“surfaceChanged called”);
}
已创建公共空白表面(表面文件夹arg0){
Log.d(标签“表面处理”);
}
公共空间表面已覆盖(表面层arg0){
Log.d(标签“表面处理”);
}
已准备好公开作废(MediaPlayer arg0){
Log.d(标签“onPrepared called”);
}
完成时的公共作废(MediaPlayer arg0){
Log.d(标记“onCompletion called”);
}
public void onBufferingUpdate(MediaPlayer,整数百分比){
Log.d(标记“OnBufferingUpdateCalled-->percent:+percent”);
}
公共布尔onError(MediaPlayer MediaPlayer,int what,int extra){
Log.e(标记“onError-->what:+what+”extra:+extra);
如果(mediaPlayer!=null){
mediaPlayer.stop();
mediaPlayer.release();
}
返回true;
}
}
我遇到了这个问题,通过使用此折旧方法设置类型来解决它
holder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
值得一试,如果它起作用,你可以调查为什么类型没有自动设置为它应该的样子。我也遇到了同样的问题,这是因为surface没有准备好播放视频 您应该尝试从surfaceCreated()处理程序调用playVideo()
我遇到了这个问题,并通过添加我的发布函数visible=转到surfaceview解决了这个问题:
public void release() {
if (mMediaPlayer != null) {
setVisibility(View.GONE);
mMediaPlayer.reset();
mMediaPlayer.release();
mMediaPlayer = null;
mCurrentState = STATE_IDLE;}
}
并在onprepared函数中设置visible=visible:
videoView.setOnPreparedListener(new OnPreparedListener() {
public void onPrepared(MediaPlayer mp) {
audio=false; video=false; int ty=mp.getTrackInfo().length;
for (int i=0; i<ty;i++)
{
if (mp.getAudioTrack()>-1) {audio=true;}
if (mp.getVideoTrack()>-1) {video=true;}
}
if (((audio==false)&&(skip==true))||((video==false)&&(skip2==true))||((video==true)&&(skip4==true)))
{ notifybar("...");
nexttr();} else {
if (vis==true) {
if (video==false) {
if (mVisualizerView.getVisibility()!=View.VISIBLE) {mVisualizerView.setVisibility(View.VISIBLE);}
mVisualizerView.link(videoView.getAudioSessionId());
vis2=true;
} else if (vis2==true){
mVisualizerView.release();
mVisualizerView.setVisibility(View.GONE);
vis2=false;
}}
//this
if (video==true) {
if (videoView.getVisibility()!=View.VISIBLE) {videoView.setVisibility(View.VISIBLE);}
} else {if (videoView.getVisibility()!=View.INVISIBLE) {videoView.setVisibility(View.INVISIBLE);}
}
videoView.setOnPreparedListener(新的OnPreparedListener(){
已准备好公开作废(MediaPlayer mp){
音频=false;视频=false;int-ty=mp.getTrackInfo().length;
对于(inti=0;i-1){audio=true;}
如果(mp.getVideoTrack()>-1){video=true;}
}
如果((音频==假)和&(跳过==真))| |((视频==假)和&(skip2==真))| |((视频==真)和&(skip4==真)))
{notifybar(“…”);
nexttr();}else{
如果(vis==true){
如果(视频==假){
如果(mVisualizerView.getVisibility()!=View.VISIBLE){mVisualizerView.setVisibility(View.VISIBLE);}
mVisualizerView.link(videoView.getAudioSessionId());
vis2=真;
videoView.setOnPreparedListener(new OnPreparedListener() {
public void onPrepared(MediaPlayer mp) {
audio=false; video=false; int ty=mp.getTrackInfo().length;
for (int i=0; i<ty;i++)
{
if (mp.getAudioTrack()>-1) {audio=true;}
if (mp.getVideoTrack()>-1) {video=true;}
}
if (((audio==false)&&(skip==true))||((video==false)&&(skip2==true))||((video==true)&&(skip4==true)))
{ notifybar("...");
nexttr();} else {
if (vis==true) {
if (video==false) {
if (mVisualizerView.getVisibility()!=View.VISIBLE) {mVisualizerView.setVisibility(View.VISIBLE);}
mVisualizerView.link(videoView.getAudioSessionId());
vis2=true;
} else if (vis2==true){
mVisualizerView.release();
mVisualizerView.setVisibility(View.GONE);
vis2=false;
}}
//this
if (video==true) {
if (videoView.getVisibility()!=View.VISIBLE) {videoView.setVisibility(View.VISIBLE);}
} else {if (videoView.getVisibility()!=View.INVISIBLE) {videoView.setVisibility(View.INVISIBLE);}
}