Thread内部的方法失焦

时间:2011-11-22 09:58:34

标签: android multithreading audio media-player runnable

我有一个方法,我从onCreate(主线程)调用。在方法内部,我调用了另一个方法和一个重复调用相同方法的Runnable。这是它的外观,

 public void start() {
    try {
        if (start == 0) {
            playpause.setBackgroundDrawable(getResources().getDrawable(
                    R.drawable.play));
            initialPoiPlayStatus = true;
            playAudio();
        }
        if (play) {
            if (mMediaPlayer != null && mMediaPlayer.isPlaying()) {
                // for displaying view stub
                poi = listOfPOI.get(j);
                int pos = mMediaPlayer.getCurrentPosition();
                int convSeconds = -1;
                convSeconds = ((int) (pos) / 1000);
                // displaying the you tube pop up based on the parsed value
                // from the plist
                youTube(convSeconds);
                // call the method to change the image while the audio is
                // playing
                changeImage();
                seek_bar.setProgress(current_position);
                notification = new Runnable() {
                    public void run() {
                        start();
                    }
                };
                handler.postDelayed(notification, 1000);
            } else {
                if (j != (listOfPOI.size() - 1)) {
                    firstPlay = true;
                    initialPoiPlayStatus = true;
                    j++;
                    playAudio();
                    start();
                    // retrieve mascot info
                    if (isNetworkAvailable() == true) {
                        receiveResponse();
                    } else {

                    }
                } else {
                    threadHandler.sendEmptyMessage(0);
                }
            }
        }
    } catch (Exception e) {
        System.out.println(poi.getPlistPath());
        e.printStackTrace();
    }
}

现在在youtube中,

 private void youTube(int convSeconds) {
    frameLayout = (FrameLayout) findViewById(R.id.frameLayout);
    youtubeWebView = (WebView) findViewById(R.id.youtubewebView);
    try {
        if (poi.getIsYoutubePresent().equals("true")) {

            //mMediaPlayer.pause();
            isNetworkAvailable();
            if (isNetworkAvailable() == true) {

                String startTimeyoutube = poi.getYoutubestartTime();
                Integer youTubeStartTime = Integer
                        .parseInt(startTimeyoutube);
                if (convSeconds == youTubeStartTime) {

                    if (frameLayout.getVisibility() == View.GONE) {
                        frameLayout.setVisibility(View.VISIBLE);
                        slideUp = AnimationUtils.loadAnimation(this,
                                R.anim.slide_up);
                        frameLayout.startAnimation(slideUp);
                    }

                    final String youTubeurl = poi.getLink();
                    WebSettings websets = youtubeWebView.getSettings();
                    websets.setJavaScriptEnabled(true);
                    registerForContextMenu(youtubeWebView);
                    youtubeWebView
                            .setWebViewClient(new ItemsWebViewClient());
                    youtubeWebView.loadUrl(youTubeurl);
                    youtubeWebView.getSettings().setLayoutAlgorithm(
                            LayoutAlgorithm.SINGLE_COLUMN);
                    firstPlay = false;
                    initialPoiPlayStatus = false;
                    paused = true;
                    play = false;
                    playpause.setBackgroundDrawable(getResources().getDrawable(
                            R.drawable.play));
                    if (mMediaPlayer.isPlaying()) {
                        mMediaPlayer.pause(); 
                    }

                    Button goBtn = (Button) findViewById(R.id.youtubeButton);
                    goBtn.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View V) {
                            startActivity(new Intent(Intent.ACTION_VIEW,
                                    Uri.parse(youTubeurl)));
                        }
                    });

                    ImageButton close = (ImageButton) findViewById(R.id.youtubecloseButton);
                    close.setOnClickListener(new OnClickListener() {

                        @Override
                        public void onClick(View arg0) {
                            frameLayout.setVisibility(View.GONE);
                            if (!mMediaPlayer.isPlaying()) {
                                mMediaPlayer.start();
                            }
                                    playpause.setBackgroundDrawable(getResources().getDrawable(
                                    R.drawable.pause));
                        }
                    });
                } 
            } else {
                Toast.makeText(getApplicationContext(), "No network available", Toast.LENGTH_SHORT).show();
            }
        } else {
            frameLayout.setVisibility(View.INVISIBLE);
        }

    } catch (Exception e) {
        e.printStackTrace();
    }
    return;
}

此时,如果(convSeconds == youTubeStartTime){}成为真,我正在暂停音频,

 if (mMediaPlayer.isPlaying()) {
    mMediaPlayer.pause(); 
}

在此期间我还希望Runnable通知中的start()应该继续调用。但是,如果我暂停音频,则不会发生这种情况。在这种情况下,控制完全丢失。按下图像按钮关闭,我试图让音频再次像这样播放

if (!mMediaPlayer.isPlaying()) {
    mMediaPlayer.start();
}

音频现在继续播放,但是滑块/搜索栏没有移动,因为通知中的start()没有被调用。

有人知道为什么会这样吗?

注意:如果我不让媒体播放器暂停,它可以正常工作。当我让音频暂停时,我得到了问题onky

1 个答案:

答案 0 :(得分:0)

我最终解决了这个问题,它可能不是标准方式。但这对我有用。

我刚刚复制了

           notification = new Runnable() {

                public void run() {
                    start();
                }
            };
            handler.postDelayed(notification, 1000); 

我试图关闭图片按钮,即

  ImageButton close = (ImageButton) findViewById(R.id.youtubecloseButton);
                close.setOnClickListener(new OnClickListener() {

                    @Override
                    public void onClick(View arg0) {
                        frameLayout.setVisibility(View.GONE);
                        if (!mMediaPlayer.isPlaying()) {
                            mMediaPlayer.start();
                        }
           notification = new Runnable() {

                public void run() {
                    start();
                }
            };
            handler.postDelayed(notification, 1000); 
                    }
                });