andriod 錯誤:Only the original thread that created a view hierarchy can touch its views——Handler的使用

package com.example.yanlei.myapplication;

import android.media.MediaMetadataRetriever;
import android.media.MediaPlayer;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.SeekBar;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.VideoView;
import android.app.Activity;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnCompletionListener;
import android.media.MediaPlayer.OnErrorListener;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.SeekBar;
import android.widget.Toast;
import android.widget.VideoView;
import android.widget.SeekBar.OnSeekBarChangeListener;

import java.io.File;
import java.util.HashMap;

import android.os.Handler;

 

  private Handler handler = null;

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

        seekBar = (SeekBar) findViewById(R.id.seekBar);
        
        // 爲進度條添加進度更改事件
        seekBar.setOnSeekBarChangeListener(change);

        //建立屬於主線程的handler
        handler = new Handler();

    }
  protected void play(int msec) {
        Log.i(TAG, " 獲取視頻文件地址");
        String path = et_path.getText().toString().trim();
        File file = new File(path);
        if (!file.exists()) {
            Toast.makeText(this, "視頻文件路徑錯誤", 0).show();
            return;
        }

        Log.i(TAG, "指定視頻源路徑");
        vv_video.setVideoPath(file.getAbsolutePath());
        Log.i(TAG, "開始播放");
        vv_video.start();

        // 按照初始位置播放
        vv_video.seekTo(msec);
        // 設置進度條的最大進度爲視頻流的最大播放時長
        final int time = getTimeLong(file.getAbsolutePath()) / 100;
        seekBar.setMax(time);
        Toast.makeText(this, "長度:" + time, Toast.LENGTH_LONG).show();
        // 開始線程,更新進度條的刻度
        new Thread() {

            @Override
            public void run() {
                try {
                    isPlaying = true;
                    while (isPlaying) {
                        // 若是正在播放,0.1.毫秒更新一次進度條
                        final int current = vv_video.getCurrentPosition() / 100;
                        seekBar.setProgress(current);
                     /*   try {
                            myText = "總時間:" + time / 10 + "秒,目前:" + current / 10 + "秒";
                            // pTextView.setText("總時間:" + time / 10 + "秒,目前:" + current / 10 + "秒");
                            new WorkThread().start();
                        } catch (Exception ex) {

                            Log.d("錯誤:", ex.getMessage().toString());
                        }*/

                        new Thread() {
                            public void run() {
                                myText = "總時間:" + time / 10 + "秒,目前:" + current / 10 + "秒";
                                handler.post(updateUIRunnable);
                            }
                        }.start();

                        //Toast.makeText(this, "當前:"+current,Toast.LENGTH_LONG).show();
                        sleep(100);
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }.start();
        // 播放以後設置播放按鈕不可用
        btn_play.setEnabled(false);

        vv_video.setOnCompletionListener(new OnCompletionListener() {

            @Override
            public void onCompletion(MediaPlayer mp) {
                // 在播放完畢被回調
                btn_play.setEnabled(true);
            }
        });

        vv_video.setOnErrorListener(new OnErrorListener() {

            @Override
            public boolean onError(MediaPlayer mp, int what, int extra) {
                // 發生錯誤從新播放
                play(0);
                isPlaying = false;
                return false;
            }
        });
    }

    //工做線程
    private class WorkThread extends Thread {
        @Override
        public void run() {
            try {

                pTextView.setText(myText);
            } catch (Exception ex) {
                Log.e("嚴重錯誤:", ex.getMessage().toString());

            }
        }
    }

    // 構建Runnable對象,並在runnable中更新UI
    Runnable updateUIRunnable = new Runnable() {
        @Override
        public void run() {
            try {

                pTextView.setText(myText);
            } catch (Exception ex) {
                Log.e("嚴重錯誤:", ex.getMessage().toString());

            }
        }
    };
相關文章
相關標籤/搜索