Domanda

Sto sviluppando un'applicazione in cui ho usato Videoview per riprodurre un video. Quello che voglio è che ho bisogno di visualizzare un po 'di testo sotto il video di riproduzione e il testo dovrebbe essere modificato man mano che il video riproduce, intendo a seconda del tempo trascorso. Come SRT. Quindi, come far passare il tempo di video in Android? E quando mettiamo in pausa anche il video secondo il testo dovrebbe essere messo in pausa e successivamente quando riprendiamo il video il testo e il seguente testo dovrebbero essere visualizzati.

Qualsiasi aiuto sarebbe apprezzato.


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

Grazie.

È stato utile?

Soluzione

Non è quello di Videoview getCurrentPosition() cosa stai cercando?

Per modificare il contenuto del tuo TextView (o qualunque cosa tu voglia usare), imposterei un timer, con abbastanza frecenza per aggiornare il tuo "sottotitolo". Il suo TimerTask potrebbe ottenere il tempo di riproduzione con quel GetCurrentPosition () e utilizzare una mappa per archiviare i valori dei messaggi e il tempo come chiave.

Eccolo ed esempio di ciò che sto pensando:

00 - "Inizia il video!"
05 - "Succede qualcosa di divertente"
12 - "Video finisce!"

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

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

Dopo aver visto il tuo codice completo, posso darti suggerimenti più dettagliati, ma la cosa della codifica è il tuo lavoro, quindi ...

Per creare la mappa:

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

Per fare il lavoro:

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

E infine, usa un file Timer Invece di questo (C'è un articolo qui su timer e interfaccia utente):

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

È davvero brutto e inefficiente.

Quindi, felice programmazione, e per favore, se trovi un po 'di incucanza, prova a risolverlo da solo, non perché non sono gentile ad aiutare, ma è la tua migliore possibilità di migliorare le tue abilità.

Saluti, Manuel.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top