如何在android中播放视频的下方显示文字?

发布于 2024-10-15 19:28:57 字数 4177 浏览 0 评论 0原文

我正在开发一个应用程序,其中使用 VideoView 来播放视频。我想要的是我需要在正在播放的视频下方显示一些文本,并且文本应该随着视频播放而改变,我的意思是根据经过的时间。就像 SRT 一样。那么,如何在android中获取视频的经过时间呢?当我们暂停视频时,相应的文本也应该暂停,之后当我们恢复视频时,应该显示文本和后面的文本。

任何帮助将不胜感激。


@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
    setContentView(R.layout.playing);

    mVideoView = (VideoView)findViewById(R.id.VideoView);
    uri = Uri.parse("android.resource://com.abhan.video/" + R.raw.abhan);

    Date dt = new Date();
    mHours = dt.getHours();
    mMinutes = dt.getMinutes();
    mSeconds = dt.getSeconds();
    String curTime = mHours + ":"+ mMinutes + ":" + mSeconds;

    mVideoView.setVideoURI(uri);
    mVideoView.start();

    Runnable runnable = new CountDownRunner();
    myThread= new Thread(runnable);   
    myThread.start();

    mVideoView.setOnPreparedListener(new OnPreparedListener() {
        @Override
        public void onPrepared(MediaPlayer mp) {
            Log.i("TAG", "On Prepared");
        }
    });

    mVideoView.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
        @Override
        public void onCompletion(MediaPlayer mp) {
        Log.v("TAG", "On Completion");
        myThread.stop();
        Intent i = new Intent(Playing.this, VideoPlay.class);
        startActivity(i);
        finish();
        }
    });
}

class CountDownRunner implements Runnable {
    public void run() {
        while(!Thread.currentThread().isInterrupted()) {
            try {
                doWork();
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                Thread.currentThread().interrupt();
                e.printStackTrace();
            } catch(Exception e) {
                e.printStackTrace();
            }
        }
    }
}

public void doWork() {
    runOnUiThread(new Runnable() {
        public void run()  {
            try {
                mText = (TextView)findViewById(R.id.SetText);
                Date dt = new Date();
                int hours = dt.getHours();
                int minutes = dt.getMinutes();
                int seconds = dt.getSeconds();
                String curTime = hours + ":"+ minutes + ":" + seconds;
                if(minutes == mMinutes && seconds == mSeconds) {
                    mText.setText(getString(R.string.one));
                } else if(minutes == mMinutes && seconds == mSeconds+20) {
                    mText.setText(getString(R.string.two));
                } else if(minutes == mMinutes && seconds == mSeconds+38) {
                    mText.setText(getString(R.string.three));
                } else if(minutes == mMinutes && seconds == mSeconds+47) {
                    mText.setText(getString(R.string.four));
                } else if(minutes == mMinutes+1 && seconds == mSeconds2+2) {
                    mText.setText(getString(R.string.five));
                } else if(minutes == mMinutes+1 && seconds == mSeconds2+22) {
                    mText.setText(getString(R.string.six));
                } else if(minutes == mMinutes+2) {
                    mText.setText(getString(R.string.seven));
                } else if(minutes == mMinutes+2 && seconds == mSeconds2+2) {
                    mText.setText("");
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    });
}

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
    if ((!(android.os.Build.VERSION.SDK_INT > android.os.Build.VERSION_CODES.DONUT) 
            &&keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0))
    {
        onBackPressed();
    }
    return super.onKeyDown(keyCode, event);
}

public void onBackPressed() {
    Intent intent = new Intent(Playing.this, VideoPlay.class);
    intent.setFlags(Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
    startActivity(intent);
    finish();
    return;
}

谢谢。

I am developing an application in which I used VideoView to play a video. What I want is I need to display some text underneath the playing video and the text should be changed as the video plays I mean depending on elapsed time. Like SRT. So, How to get elapsed time of video in android? And when we pause the video according text should be paused as well and after that when we resume video the text and the following text should be displayed.

Any help would be appreciated.


@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
    setContentView(R.layout.playing);

    mVideoView = (VideoView)findViewById(R.id.VideoView);
    uri = Uri.parse("android.resource://com.abhan.video/" + R.raw.abhan);

    Date dt = new Date();
    mHours = dt.getHours();
    mMinutes = dt.getMinutes();
    mSeconds = dt.getSeconds();
    String curTime = mHours + ":"+ mMinutes + ":" + mSeconds;

    mVideoView.setVideoURI(uri);
    mVideoView.start();

    Runnable runnable = new CountDownRunner();
    myThread= new Thread(runnable);   
    myThread.start();

    mVideoView.setOnPreparedListener(new OnPreparedListener() {
        @Override
        public void onPrepared(MediaPlayer mp) {
            Log.i("TAG", "On Prepared");
        }
    });

    mVideoView.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
        @Override
        public void onCompletion(MediaPlayer mp) {
        Log.v("TAG", "On Completion");
        myThread.stop();
        Intent i = new Intent(Playing.this, VideoPlay.class);
        startActivity(i);
        finish();
        }
    });
}

class CountDownRunner implements Runnable {
    public void run() {
        while(!Thread.currentThread().isInterrupted()) {
            try {
                doWork();
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                Thread.currentThread().interrupt();
                e.printStackTrace();
            } catch(Exception e) {
                e.printStackTrace();
            }
        }
    }
}

public void doWork() {
    runOnUiThread(new Runnable() {
        public void run()  {
            try {
                mText = (TextView)findViewById(R.id.SetText);
                Date dt = new Date();
                int hours = dt.getHours();
                int minutes = dt.getMinutes();
                int seconds = dt.getSeconds();
                String curTime = hours + ":"+ minutes + ":" + seconds;
                if(minutes == mMinutes && seconds == mSeconds) {
                    mText.setText(getString(R.string.one));
                } else if(minutes == mMinutes && seconds == mSeconds+20) {
                    mText.setText(getString(R.string.two));
                } else if(minutes == mMinutes && seconds == mSeconds+38) {
                    mText.setText(getString(R.string.three));
                } else if(minutes == mMinutes && seconds == mSeconds+47) {
                    mText.setText(getString(R.string.four));
                } else if(minutes == mMinutes+1 && seconds == mSeconds2+2) {
                    mText.setText(getString(R.string.five));
                } else if(minutes == mMinutes+1 && seconds == mSeconds2+22) {
                    mText.setText(getString(R.string.six));
                } else if(minutes == mMinutes+2) {
                    mText.setText(getString(R.string.seven));
                } else if(minutes == mMinutes+2 && seconds == mSeconds2+2) {
                    mText.setText("");
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    });
}

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
    if ((!(android.os.Build.VERSION.SDK_INT > android.os.Build.VERSION_CODES.DONUT) 
            &&keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0))
    {
        onBackPressed();
    }
    return super.onKeyDown(keyCode, event);
}

public void onBackPressed() {
    Intent intent = new Intent(Playing.this, VideoPlay.class);
    intent.setFlags(Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
    startActivity(intent);
    finish();
    return;
}

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

So尛奶瓶 2024-10-22 19:28:57

这不是您要找的 VideoView 的 getCurrentPosition() 吗?

要更改 TextView 的内容(或您想使用的任何内容),我会设置一个计时器,以足够的频率来更新您的“字幕”。它的TimerTask可以通过getCurrentPosition()获取播放时间,并使用Map来存储消息值和时间作为键。

这是我的想法的示例:

00 -“视频开始!”

05 - “发生了一些有趣的事情”

12 - “视频结束!”

class MySubtitlePoster extends TimerTask{
    private VideoView video;
    private TreeMap <Integer, String> messages; // populate it somewhere

    public MySubtitlePoster(VideoView v) {
        video = v;
    }

    public void run() {
        int videoPos = video.getCurrentPosition();
        String messageToDisplay = messages.floorKey(new Integer(videoPos));
        // If all this is right, now you can get the message and post it, probably using a Handler
    }
}

============================================

看到你的完整代码后,我可以为您提供更详细的提示,但编码是您的工作,所以...

创建地图:

messages = new TreeMap();
messages.put(new Integer(0), getString(R.string.one));
messages.put(new Integer(20), getString(R.string.two));
...
messages.put(new Integer(62), getString(R.string.four));

完成工作:

public void doWork(){
    runOnUiThread(new Runnable() {
        public void run() {
            try{
                mText = (TextView)findViewById(R.id.SetText);
                //If it returns  milliseconds, divide by 1000
                int playTime = mVideoView.getCurrentPosition();  
                String textValue = messages.ceilingEntry(new Integer(playtime)).getValue();
                mText.setText(textValue);
            }catch (Exception e) {
                e.printStackTrace();
            }
        }
    });
}

最后,使用 计时器 而不是这个(这里有一篇关于计时器和 UI 的文章):

public void run() 
{
    while(!Thread.currentThread().isInterrupted())
    {
        try 
        {
            doWork();
            Thread.sleep(1000);
        }catch (InterruptedException e) 
        {
            Thread.currentThread().interrupt();
            e.printStackTrace();
        }catch(Exception e)
        {
            e.printStackTrace();
        }
    }
}

它真的很丑陋且效率低下。

所以,祝您编码愉快,如果您发现一些不正确的地方,请尝试自己解决它,不是因为我不乐意提供帮助,而是因为这是您提高技能的最佳机会。

问候,曼努埃尔。

Isn't it VideoView's getCurrentPosition() what you are looking for?

To change the contents of your TextView (or whatever yo want to use), I would set a Timer, with enough frecuency to update your "subtitle". Its TimerTask could get the playback time with that getCurrentPosition(), and use a Map to store messages values and the time as the key.

Here it's and example of what I'm thinking:

00 - "Video begins!"

05 - "something funny happens"

12 - "Video ends!"

class MySubtitlePoster extends TimerTask{
    private VideoView video;
    private TreeMap <Integer, String> messages; // populate it somewhere

    public MySubtitlePoster(VideoView v) {
        video = v;
    }

    public void run() {
        int videoPos = video.getCurrentPosition();
        String messageToDisplay = messages.floorKey(new Integer(videoPos));
        // If all this is right, now you can get the message and post it, probably using a Handler
    }
}

==========================================

After seeing your complete code, I can give you more detailed tips, but the coding thing is your job, so...

To create the map:

messages = new TreeMap();
messages.put(new Integer(0), getString(R.string.one));
messages.put(new Integer(20), getString(R.string.two));
...
messages.put(new Integer(62), getString(R.string.four));

To do the work:

public void doWork(){
    runOnUiThread(new Runnable() {
        public void run() {
            try{
                mText = (TextView)findViewById(R.id.SetText);
                //If it returns  milliseconds, divide by 1000
                int playTime = mVideoView.getCurrentPosition();  
                String textValue = messages.ceilingEntry(new Integer(playtime)).getValue();
                mText.setText(textValue);
            }catch (Exception e) {
                e.printStackTrace();
            }
        }
    });
}

And finally, use a Timer instead of this (There is an article here about Timers and UI):

public void run() 
{
    while(!Thread.currentThread().isInterrupted())
    {
        try 
        {
            doWork();
            Thread.sleep(1000);
        }catch (InterruptedException e) 
        {
            Thread.currentThread().interrupt();
            e.printStackTrace();
        }catch(Exception e)
        {
            e.printStackTrace();
        }
    }
}

It is really ugly and inefficient .

So, happy coding, and please if you find some inccurancy try to solve it yourself, not because I'm not kind on helping, but it's your best chance to improve your skills.

Regards, Manuel.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文