将视图放在顶级SurfaceView Android上

时间:2017-01-28 06:59:36

标签: android android-mediaplayer surfaceview

我在Android上的代码需要帮助,我一直在尝试搜索3个小时但没有运气

所以我是一个调用MediaPlayerSDK的库,我在我的应用程序中使用它来播放视频。该库正在使用SurfaceView播放视频。现在,我要将getSurfaceView()。setZOrderOnTop(true)包含在播放器中,这样我就可以显示视频,否则它将不会显示(仅限音频)。但是,当我使用getSurfaceView()。setZOrderOnTop(true)时,我无法在播放器上放置任何视图。我想为玩家建立一个控制器,但我不能。我已经厌倦了getSurfaceView()。setZOrderMediaOverlay(true)和同样的问题视频没有显示。

活动的XML代码:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/transparent">

<FrameLayout
    android:id="@+id/playerViewLayout"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content" >

    <TextView
        android:text="...."
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/ChTitle"
        android:textColor="@color/white"
        android:textStyle="bold"
        android:layout_marginLeft="5dp"/>

<veg.mediaplayer.sdk.MediaPlayer
    android:id="@+id/playerView"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_alignParentTop="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true" />

</FrameLayout>


</RelativeLayout>

java代码是

public class PlayerActivity extends AppCompatActivity implements MediaPlayer.MediaPlayerCallback {


private static final String TAG      = "Tag";

private boolean                     playing = false;
private MediaPlayer                 player = null;

private enum PlayerStates
{
    Busy,
    ReadyForUse
};

private enum PlayerConnectType
{
    Normal,
    Reconnecting
};

private PlayerStates player_state = PlayerStates.ReadyForUse;
private PlayerConnectType reconnect_type = PlayerConnectType.Normal;
private int mOldMsg = 0;

// Event handler

private Handler handler = new Handler()
{
    String strText = "Connecting";

    String sText;
    String sCode;

    @Override
    public void handleMessage(Message msg)
    {
        MediaPlayer.PlayerNotifyCodes status = (MediaPlayer.PlayerNotifyCodes) msg.obj;
        switch (status)
        {
            case CP_CONNECT_STARTING:
                if (reconnect_type == PlayerConnectType.Reconnecting)
                    strText = "Reconnecting";
                else
                    strText = "Connecting";

                player_state = PlayerStates.Busy;
                //showStatusView();

                reconnect_type = PlayerConnectType.Normal;
                break;

            case PLP_BUILD_SUCCESSFUL:
                sText = player.getPropString(MediaPlayer.PlayerProperties.PP_PROPERTY_PLP_RESPONSE_TEXT);
                sCode = player.getPropString(MediaPlayer.PlayerProperties.PP_PROPERTY_PLP_RESPONSE_CODE);
                Log.i(TAG, "=Status PLP_BUILD_SUCCESSFUL: Response sText="+sText+" sCode="+sCode);
                break;

            case VRP_NEED_SURFACE:
                player_state = PlayerStates.Busy;
                //showVideoView();
                break;

            case PLP_PLAY_SUCCESSFUL:
                player_state = PlayerStates.ReadyForUse;
                //stopProgressTask();
                break;

            case PLP_CLOSE_STARTING:
                player_state = PlayerStates.Busy;
                //stopProgressTask();
                //showStatusView();
                setUIDisconnected();
                break;

            case PLP_CLOSE_SUCCESSFUL:
                player_state = PlayerStates.ReadyForUse;
                //stopProgressTask();
                //showStatusView();
                System.gc();
                setUIDisconnected();
                break;

            case PLP_CLOSE_FAILED:
                player_state = PlayerStates.ReadyForUse;
                //stopProgressTask();
                //showStatusView();
                setUIDisconnected();
                break;

            case CP_CONNECT_FAILED:
                player_state = PlayerStates.ReadyForUse;
                //stopProgressTask();
                //showStatusView();
                setUIDisconnected();
                break;

            case PLP_BUILD_FAILED:
                sText = player.getPropString(MediaPlayer.PlayerProperties.PP_PROPERTY_PLP_RESPONSE_TEXT);
                sCode = player.getPropString(MediaPlayer.PlayerProperties.PP_PROPERTY_PLP_RESPONSE_CODE);
                Log.i(TAG, "=Status PLP_BUILD_FAILED: Response sText="+sText+" sCode="+sCode);

                player_state = PlayerStates.ReadyForUse;
                //stopProgressTask();
                //showStatusView();
                setUIDisconnected();
                break;

            case PLP_PLAY_FAILED:
                player_state = PlayerStates.ReadyForUse;
                //stopProgressTask();
                //showStatusView();
                setUIDisconnected();
                break;

            case PLP_ERROR:
                player_state = PlayerStates.ReadyForUse;
                //stopProgressTask();
                //showStatusView();
                setUIDisconnected();
                break;

            case CP_INTERRUPTED:
                player_state = PlayerStates.ReadyForUse;
                //stopProgressTask();
                //showStatusView();
                setUIDisconnected();
                break;
            case CP_RECORD_STARTED:
                Log.v(TAG, "=handleMessage CP_RECORD_STARTED");
            {
                String sFile = player.RecordGetFileName(1);
                Toast.makeText(getApplicationContext(),"Record Started. File "+sFile, Toast.LENGTH_LONG).show();
            }
            break;

            case CP_RECORD_STOPPED:
                Log.v(TAG, "=handleMessage CP_RECORD_STOPPED");
            {
                String sFile = player.RecordGetFileName(0);
                Toast.makeText(getApplicationContext(),"Record Stopped. File "+sFile, Toast.LENGTH_LONG).show();
            }
            break;

            //case CONTENT_PROVIDER_ERROR_DISCONNECTED:
            case CP_STOPPED:
            case VDP_STOPPED:
            case VRP_STOPPED:
            case ADP_STOPPED:
            case ARP_STOPPED:
                if (player_state != PlayerStates.Busy)
                {
                    //stopProgressTask();
                    player_state = PlayerStates.Busy;
                    player.Close();
                    //showStatusView();
                    player_state = PlayerStates.ReadyForUse;
                    setUIDisconnected();
                }
                break;

            case CP_ERROR_DISCONNECTED:
                if (player_state != PlayerStates.Busy)
                {
                    player_state = PlayerStates.Busy;

                    player.Close();

                    //showStatusView();
                    player_state = PlayerStates.ReadyForUse;
                    setUIDisconnected();

                    Toast.makeText(getApplicationContext(), "Demo Version!",
                            Toast.LENGTH_SHORT).show();

                }
                break;
            default:
                player_state = PlayerStates.Busy;
        }
    }
};

// callback from Native Player
@Override
public int OnReceiveData(ByteBuffer buffer, int size, long pts)
{
    Log.e(TAG, "Form Native Player OnReceiveData: size: " + size + ", pts: " + pts);
    return 0;
}

// All event are sent to event handlers
@Override
public int Status(int arg)
{

    MediaPlayer.PlayerNotifyCodes status = MediaPlayer.PlayerNotifyCodes.forValue(arg);
    if (handler == null || status == null)
        return 0;

    Log.e(TAG, "Form Native Player status: " + arg);
    switch (MediaPlayer.PlayerNotifyCodes.forValue(arg))
    {
        default:
            Message msg = new Message();
            msg.obj = status;
            handler.removeMessages(mOldMsg);
            mOldMsg = msg.what;
            handler.sendMessage(msg);
    }

    return 0;
}
Intent getI;
String Title, Path = TEST_URL;

TextView ChannelTitle;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_myplayer);

    getI = getIntent();


    // Create Player instance
    player = (MediaPlayer)findViewById(R.id.playerView);
    ChannelTitle = (TextView) findViewById(R.id.ChTitle);

    Title = getI.getStringExtra("title");
    Path = getI.getStringExtra("path");

    ChannelTitle.setText(Title);
    player.getSurfaceView().setZOrderMediaOverlay(true);    // necessary
    SurfaceHolder sfhTrackHolder = player.getSurfaceView().getHolder();
    sfhTrackHolder.setFormat(PixelFormat.TRANSPARENT);

    player.setOnTouchListener(new View.OnTouchListener()
    {
        @Override
        public boolean onTouch(View view, MotionEvent motionEvent)
        {
            switch (motionEvent.getAction() & MotionEvent.ACTION_MASK)
            {
                case MotionEvent.ACTION_DOWN:
                {
                    if (player.getState() == MediaPlayer.PlayerState.Paused)
                        player.Play();
                    else
                    if (player.getState() == MediaPlayer.PlayerState.Started)
                        player.Pause();
                }
            }

            return true;
        }
    });

    if (player != null)
    {

        player.getConfig().setConnectionUrl(Path);
        if (player.getConfig().getConnectionUrl().isEmpty())
            return;


        //player_record.Close();

        player.Close();

        PlayerSettings sett = new PlayerSettings();
        boolean bPort = (getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE);
        int aspect = bPort ? 1 : sett.rendererEnableAspectRatio;

        MediaPlayerConfig conf = new MediaPlayerConfig();

        player.setVisibility(View.INVISIBLE);

        conf.setConnectionUrl(player.getConfig().getConnectionUrl());

        conf.setConnectionNetworkProtocol(sett.connectionProtocol);
        conf.setConnectionDetectionTime(sett.connectionDetectionTime);
        conf.setConnectionBufferingTime(sett.connectionBufferingTime);
        conf.setDecodingType(sett.decoderType);
        conf.setRendererType(sett.rendererType);
        conf.setSynchroEnable(sett.synchroEnable);
        conf.setSynchroNeedDropVideoFrames(sett.synchroNeedDropVideoFrames);
        conf.setEnableColorVideo(sett.rendererEnableColorVideo);
        conf.setEnableAspectRatio(aspect);
        conf.setDataReceiveTimeout(30000);
        conf.setNumberOfCPUCores(0);


        // Open Player
        player.Open(conf, this);


        //record only
        //conf.setMode(MediaPlayer.PlayerModes.PP_MODE_RECORD);
        //conf.setRecordTrimPosStart(10000); //from 10th sec
        //conf.setRecordTrimPosEnd(20000); //to 20th sec
        /*player_record.Open(conf, new MediaPlayerCallback(){

            @Override
            public int Status(int arg) {
                Log.i(TAG, "=player_record Status arg="+arg);
                return 0;
            }

            @Override
            public int OnReceiveData(ByteBuffer buffer, int size,
                    long pts) {
                // TODO Auto-generated method stub
                return 0;
            }

        });*/


        playing = true;

    }

}

private int[] mColorSwapBuf = null;                        // used by saveFrame()
public Bitmap getFrameAsBitmap(ByteBuffer frame, int width, int height)
{
    Bitmap bmp = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
    bmp.copyPixelsFromBuffer(frame);
    return bmp;
}

protected void onPause()
{
    Log.e("SDL", "onPause()");
    super.onPause();

    if (player != null)
        player.onPause();
}
@Override
protected void onResume()
{
    Log.e("SDL", "onResume()");
    super.onResume();
    if (player != null)
        player.onResume();
}

@Override
protected void onStart()
{
    Log.e("SDL", "onStart()");
    super.onStart();
    if (player != null)
        player.onStart();
}

@Override
protected void onStop()
{
    Log.e("SDL", "onStop()");
    super.onStop();
    if (player != null)
        player.onStop();



}
@Override
public void onBackPressed() {
    player.Close();
    setUIDisconnected();
    if (!playing)
    {
        super.onBackPressed();
        return;
    }


}

@Override
public void onWindowFocusChanged(boolean hasFocus)
{
    Log.e("SDL", "onWindowFocusChanged(): " + hasFocus);
    super.onWindowFocusChanged(hasFocus);
    if (player != null)
        player.onWindowFocusChanged(hasFocus);
}

@Override
public void onLowMemory()
{
    Log.e("SDL", "onLowMemory()");
    super.onLowMemory();
    if (player != null)
        player.onLowMemory();
}

@Override
protected void onDestroy()
{
    Log.e("SDL", "onDestroy()");

    if (player != null)
        player.onDestroy();

    System.gc();

    super.onDestroy();
}

protected void setUIDisconnected()
{
    playing = false;
}


@Override
public void onConfigurationChanged(Configuration newConfig) {
    super.onConfigurationChanged(newConfig);
}

}

我需要帮助吗

谢谢

0 个答案:

没有答案