Android在按钮点击的新活动中全屏播放视频

时间:2015-07-09 10:04:32

标签: android video video-streaming playback

我正在我的应用中播放视频,但效果很好。 问题是,当我尝试将其全屏显示(在新活动或其他情况下)时,屏幕为空白。

我已按照建议here

尝试在不启动新活动的情况下执行此操作

在我的主要活动中:

butFullScreen.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            if(isFS)
                setContentView(R.layout.activity_full_screen_video);
            else
                setContentView(R.layout.activity_starting_point);
        }
});

我也尝试在新活动(我更喜欢)中打开它:

butFullScreen.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            //Start fs intent
            Intent myIntent = new Intent(StartingPoint.this, FullScreenVideo.class);
            StartingPoint.this.startActivity(myIntent);
        }
});

FullScreenVideo:

public class FullScreenVideo extends StartingPoint{

    //private VideoView vv;

    public FullScreenVideo(){
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getSupportActionBar().hide();
        setContentView(R.layout.activity_full_screen_video);
        vv.start();
    }
}

和activity_full_screen_video:

<?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:layout_alignParentRight="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"
    android:layout_alignParentBottom="true"
    android:background="#ff000000">

    <VideoView
        android:id="@+id/vv"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_centerVertical="true"
        android:layout_centerHorizontal="true" />

</RelativeLayout>

在我的清单中:

<activity
            android:name=".FullScreenVideo"
            android:label="@string/app_name"
            android:screenOrientation="landscape"
            android:configChanges="keyboardHidden|orientation|screenSize" />

它不显示任何错误或崩溃,只是一个空白屏幕。此外,我没有使用媒体控制器,所以没有它的答案将不胜感激:)

编辑::

起点:

public class PlayVid extends ActionBarActivity {

int play = -1;
int k;
int m = 0;
int where;
int pausePressed = 0;
int displaySub = 0;
String curSub = " ";
ArrayList<Stime> timeArray = new ArrayList<Stime>();

Button but;
Button butStop;
Button butSub;
Button butFS;
TextView subs, log;
VideoView vv;
ProgressBar pBar;

int isFS, space;

String srt = "00:00:01,478 --> 00:00:04,020\n" +
        "VimeoSrtPlayer Example\n" +
        "\n" +
        "00:00:05,045 --> 00:00:09,545\n" +
        "Support for <i>italic</i> font\n" +
        "\n" +
        "00:00:09,378 --> 00:00:13,745\n" +
        "Support for <b>bold</b> font\n" +
        "\n" +
        "00:00:14,812 --> 00:00:16,144\n" +
        "Multi\n" +
        "Line\n" +
        "Support ;)\n" +
        "\n" +
        "00:00:18,211 --> 00:00:21,211\n" +
        "Fonts: DejaVu\n" +
        "http://dejavu-fonts.org\n" +
        "\n" +
        "00:00:22,278 --> 00:00:25,678 \n" +
        "END OF EXAMPLE FILE";

subParse sp = new subParse(this, srt);

@Override
protected void onCreate(Bundle savedInstanceState) {
    requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
    setProgressBarIndeterminateVisibility(true);
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_starting_point);

    pBar = (ProgressBar) findViewById(R.id.progressBar);
    subs = (TextView) findViewById(R.id.subtitleBox);
    log = (TextView) findViewById(R.id.logBox);
    but = (Button) findViewById(R.id.but);
    butStop = (Button) findViewById(R.id.butStop);
    butSub = (Button) findViewById(R.id.butSub);
    butFS = (Button) findViewById(R.id.butFS);

    vv = (VideoView) findViewById(R.id.vv);
    vv.setVideoPath("@string/video_link");

    String[] lines = {" \n", " \n"};
    sp.parseSub(srt);

    subs.setText("--Click Play to start--\n--Click on SUB to view subtitles--");
    final String[] sub2 = log.getText().toString().split(System.getProperty("line.separator"));
    final subHelper sob = new subHelper(this, sub2);
    final onPauseHelper oph = new onPauseHelper();
    final fsHelper fsh = new fsHelper(this);
    final playHelper ph = new playHelper();
    timeArray.add(new Stime(vv.getDuration(), 0));

    but.setOnClickListener(new View.OnClickListener() {
        long startTime = 0, trop = 0;
        int space = 0;

        public void onClick(View v) {
            subs.setText(curSub);
            fsh.space = space;
            if (fsh.isFS == 1) {
                //Chill
            } else if (play == 0 || play == -1) {
                vv.start();
                pBar.setVisibility(View.VISIBLE);
                if (vv.isPlaying()) {
                    startTime = Calendar.getInstance().getTimeInMillis();
                    pBar.setVisibility(View.INVISIBLE);
                    //Play
                    if (play == -1) {
                        sob.setDefaults();
                    }
                    oph.putAttr(3, startTime);
                    but.setText("||");
                    but.setTextColor(Color.parseColor("#ffaaaaaa"));

                    if (k < m) {
                        play = 1;
                        final Handler handler = new Handler();
                        final Runnable run = new Runnable() {
                            @Override
                            public void run() {
                                if (k < m && play == 1 && fsh.isFS == 0) {
                                    if (pausePressed == 0) {
                                        oph.putAttr(0, Calendar.getInstance().getTimeInMillis());
                                    } else {
                                        oph.putAttr(0, startTime);
                                        where--;
                                    }
                                    //oph.nextPressed=Calendar.getInstance().getTimeInMillis();
                                    if (space == 0) {
                                        space = 1;
                                        fsh.space = space;
                                        play = sob.subPlay(startTime, 0);
                                        fsh.play = play;
                                        trop = (timeArray.get(k).getTime(1) - timeArray.get(k).getTime(0));
                                        handler.postDelayed(this, Math.abs(trop > 0 ? (trop - oph.done) : 0));
                                    } else if (space == 1) {
                                        space = 0;
                                        fsh.space = space;
                                        play = sob.subPlay(startTime, 1);
                                        fsh.play = play;
                                        trop = timeArray.get(k + 1).getTime(0) - timeArray.get(k).getTime(1);
                                        handler.postDelayed(this, Math.abs(trop > 0 ? (trop - oph.done) : 0));
                                        k++;
                                    }
                                    oph.putAttr(1, Calendar.getInstance().getTimeInMillis());
                                    pausePressed = 0;
                                    sob.pp = 0;
                                    oph.reset();
                                    oph.done = 0;
                                } else if (k >= m && fsh.isFS == 0) {
                                    final Runnable run = new Runnable() {
                                        @Override
                                        public void run() {
                                            but.setText("↻");
                                            but.setTextColor(Color.parseColor("#ffcdcdcd"));
                                            subs.setText("Play Again?");
                                            handler.removeCallbacks(this);
                                            play = -1;
                                            fsh.play = play;
                                            //ph.saveState(play,0,k,0,curSub,displaySub);
                                            curSub = " ";
                                        }
                                    };
                                    while (vv.isPlaying()) {
                                    }
                                    handler.postDelayed(run, 0);
                                }
                            }
                        };
                        if (k == 0)
                            handler.postDelayed(run, (timeArray.get(0).getTime(0)));
                        else if (k < m) handler.postDelayed(run, 0);
                    }
                }

            } else if (play == 1) {
                //Pause
                vv.pause();
                oph.reset();
                oph.putAttr(2, Calendar.getInstance().getTimeInMillis());
                subs.setText(curSub);
                pausePressed = 1;
                oph.done = +oph.getProg();
                sob.pp++;
                but.setText("▶");
                but.setTextColor(Color.parseColor("#ffcdcdcd"));
                if (k < m) {
                    if (space == 0) {
                        space = 1;
                        fsh.space = space;
                    } else if (space == 1) {
                        space = 0;
                        fsh.space = space;
                    }
                    play = sob.subPause(oph.getAttr(2));
                    fsh.play = play;
                }
            }
        }
    });

    butStop.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            vv.stopPlayback();
            sob.setDefaults();
            play = 0;
            fsh.play = play;
            but.setText("▶");
            subs.setText("--Click Play to start--");
            curSub = " ";
        }
    });

    butSub.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            if (displaySub == 0) {
                displaySub = 1;
                subs.setText(curSub);
                butSub.setTextColor(Color.parseColor("#ffaaaaaa"));
            } else if (displaySub == 1) {
                displaySub = 0;
                subs.setText(" ");
                butSub.setTextColor(Color.parseColor("#ffcdcdcd"));
            }
        }
    });

    butFS.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            //Start fs intent
            Intent myIntent = new Intent(PlayVid.this, TwoFullScreenVideo.class);
            PlayVid.this.startActivity(myIntent);
        }
    });
}

}

FullScreenVideo:

public class FullScreenVideo extends StartingPoint {

    fsHelper fsh;
    onPauseHelper oph;
    subHelper sob;
    playHelper ph;

    protected PlayVid context;

    TextView subs;
    Button but, butStop, butSub, butFS;

    public FullScreenVideo(){}

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getSupportActionBar().hide();
        setContentView(R.layout.activity_full_screen_video);

        subs = (TextView) findViewById(R.id.subtitleBox);
        but = (Button) findViewById(R.id.but);
        butStop = (Button) findViewById(R.id.butStop);
        butSub = (Button) findViewById(R.id.butSub);
        butFS = (Button) findViewById(R.id.butFS);
        vv = (VideoView) findViewById(R.id.vv);

        butFS.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                FullScreenVideo.this.finish();
            }
        });

    }
}

0 个答案:

没有答案