保存由sharedprefrence保存的videoView的最后一个位置,并从最后一个位置开始(android studio) [英] save last position of videoView saved by sharedprefrence and start in last position (android studio)

查看:133
本文介绍了保存由sharedprefrence保存的videoView的最后一个位置,并从最后一个位置开始(android studio)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

保存由sharedprefrence保存的videoView的最后位置,并从最后位置开始(android studio) 你好,我在stackaverflow和其他参考中请求我的答辩,但我没有得到答案 . 我在我的应用中使用VideoView 我想将videview的最后一个位置保存在sharedPrefrence中,以从最后一个位置重新开始,但是每次都从0开始 请检查我的代码,然后给我打电话.

save last position of videoView saved by sharedprefrence and start in last position (android studio) hello I serch for my problam in stackaverflow and other refrences but I dont get my answer . I use VideoView In my app I want to save the last position of videview in a sharedPrefrence to start again from the last position but it start in 0 every time please check my code and tel me what should i do.

package com.rewass.qurankurdishaudiotranslatev2.activitys;

import android.content.Context;
import android.content.DialogInterface;
import android.content.SharedPreferences;
import android.media.MediaPlayer;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.preference.PreferenceManager;
import android.util.Log;
import android.view.View;
import android.view.ViewTreeObserver;
import android.widget.MediaController;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.VideoView;

import androidx.annotation.RequiresApi;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.content.ContextCompat;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.InterstitialAd;
import com.rewass.qurankurdishaudiotranslatev2.R;
import com.rewass.qurankurdishaudiotranslatev2.adapters.RecyclerViewAdapter;
import com.rewass.qurankurdishaudiotranslatev2.model.ModelRecycler;
import com.rewass.qurankurdishaudiotranslatev2.text.Text002;

import java.util.ArrayList;
import java.util.List;

public class A002 extends AppCompatActivity {
    List<ModelRecycler> listItems;
    private InterstitialAd mInterstitialAd;
    String AudioURL = "https://download.quranicaudio.com/quran/mishaari_raashid_al_3afaasee/002.mp3";
    VideoView videoView;
    ProgressBar progressBar008;
    TextView te008;
    int progress;
    RecyclerView myrv;
    String kurdish002 = "http://sirwaan.com/aaap/audios/kurdish/sound/quran/qurankurdish/002.mp3";
    String v2kurdish002 = "https://sirwaan.com/aaap/audios/kurdish/sound/quran/newversionqurankurdish/002.mp3";
    Handler handler;
    GridLayoutManager layoutManager;
    int seekkk;
    @RequiresApi(api = Build.VERSION_CODES.M)

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

        audioSora();


    }

    SharedPreferences preferences ;
    private void audioSora() {
        preferences = getSharedPreferences("pref55",MODE_PRIVATE);

        videoView = findViewById(R.id.videoview002);

        videoView.setVideoPath(v2kurdish002);

        //control media
        MediaController mediaController = new MediaController(this);
        //set view with controller
        videoView.setMediaController(mediaController);
        //set Controller to view


        mediaController.setAnchorView(videoView);

        videoView.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
            @Override
            public void onPrepared(MediaPlayer mp) {

                handler = new Handler();
                final Runnable r = new Runnable() {
                    public void run() {
                        SharedPreferences.Editor editor = preferences.edit();
                    int progress = videoView.getCurrentPosition();
                        editor.putInt("seekto", progress);
                        editor.apply();
                        seekkk = preferences.getInt("seekto", 0);
                        Toast.makeText(A002.this, seekkk+"", Toast.LENGTH_SHORT).show();
                       handler.postDelayed(this, 1000); }};

                handler.postDelayed(r, 1000);
                videoView.seekTo(seekkk);
                videoView.start();

            }
        });

    }

}

推荐答案

private int time = 0;

private ScheduledExecutorService mScheduledExecutorService;
    Runnable getPositionVideo = () -> {
        time = binding.epVideoView.getCurrentPosition();
        Log.d(TAG, ": " + time);
    };


     binding.epVideoView.setOnPreparedListener(mediaPlayer -> {
            mediaController.setAnchorView(binding.epVideoView);
            binding.epVideoView.start();
            mScheduledExecutorService = new ScheduledThreadPoolExecutor(1);
            mScheduledExecutorService.scheduleWithFixedDelay(() -> {
                runOnUiThread(getPositionVideo);
                MediaControllerCustom.init(url, time);
            }, 1000, 1000, TimeUnit.MILLISECONDS);
        });

这篇关于保存由sharedprefrence保存的videoView的最后一个位置,并从最后一个位置开始(android studio)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆