android media player 显示音频但没有视频

标签 android streaming media-player

我正在使用媒体播放器播放视频。它只播放音频而不播放视频。有人能帮忙吗?我的代码如下。

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>

最佳答案

我遇到了这个问题,并通过使用这种折旧方法设置类型来解决它。

holder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);

值得一试,如果可行,您可以调查为什么类型没有按预期自动设置。

关于android media player 显示音频但没有视频,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5507801/

相关文章:

android - iOS 和 Android 应用程序中 Facebook 深度链接的升级流程?

android - 单击 Android 中 Horizo​​ntalListView 内的项目

android - 蓝牙 socket

java - 从远程 EJB 调用返回大文件

android - 即使应用被杀死,声音仍会继续播放

java - 转换为 Dalvik 格式失败,错误 1 ​​- 重复,但无法解析

java - 我可以在 SBE 复合类型中添加可变长度成员吗?

java - Xuggler 编码和混合

Android 服务播放音乐并在暂停和停止时停止

asynchronous - 进行地面AsyncTask