码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2017-10-06 12:28:22      阅读:222      评论:0      收藏:0      [点我收藏+]

标签:bar   views   ons   end   工作   ppc   地址   pac   bsp   

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

            }
        }
    };

 

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

标签:bar   views   ons   end   工作   ppc   地址   pac   bsp   

原文地址:http://www.cnblogs.com/gisoracle/p/7631287.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!