質問

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;
}

ありがとう。

役に立ちましたか?

解決

VideoViewのものではありませんか getCurrentPosition() 何を探していますか?

TextViewの内容(または使用したいもの)を変更するために、「サブタイトル」を更新するのに十分な中心性でタイマーを設定します。そのティマタスクは、そのgetCurrentPosition()で再生時間を取得し、マップを使用してメッセージの値と時間をキーとして保存することができます。

ここに私が考えていることの模範があります:

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();
            }
        }
    });
}

そして最後に、aを使用します タイマー これの代わりに (タイマーと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();
        }
    }
}

それは本当に醜く、非効率的です。

だから、幸せなコーディング、そしてあなたがいくつかの輸入を見つけたなら、私は助けに優しくないからではなく、それがあなたのスキルを向上させるためのあなたの最高のチャンスだからです。

よろしく、マヌエル。

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top