clcurie_11 2012-12-07 06:29 采纳率: 6.7%
浏览 2209
已采纳

点击按钮后停止媒体播放器

我在程序中设置一个媒体播放器,有play和stop按钮,当我运行以下代码时,play按钮是没有问题的,但是当我点击stop按钮时,却得到一个强行关闭的消息。这是怎么回事呢?
这是我所用使用的代码:

package com.example.hellomedia;
import java.io.IOException;

    import android.app.Activity;
    import android.media.MediaPlayer;
    import android.os.Bundle;
    import android.os.Handler;
    import android.view.View;
    import android.widget.TextView;

    public class HelloMedia extends Activity {

          /** Called when the activity is first created. */
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);

            final MediaPlayer mPlayer = MediaPlayer.create(HelloMedia.this, R.raw.nicholas);

            setContentView(R.layout.main);

            final Handler mHandler = new Handler();

            final TextView customTextBG = (TextView) findViewById(R.id.customTextBG);
            final TextView customTextHL = (TextView) findViewById(R.id.customTextHL);

            customTextBG.setText("Nicholas was… \n\nOlder than sin, ");
            customTextHL.setText("");

            final String words[] = {
                    "Nicholas ", // 0
                    "was... \n\n", // 1
                    "Older ", // 2
                    "than ", // 3
                    "sin, ", // 4
            };

            final long startEndTime[][]={
                    {   //start time
                        1148,// 0,0
                        1826, // 0,1
                        2766,// 0,2
                        3079,// 0,3
                        3549,// 0,4

                    },
                    {   //end time
                        1357,// 1,0
                        2192, // 1,1
                        3027,// 1,2
                        3183,// 1,3
                        3966,// 1,4
                    }
                };


            customTextBG.setText("Nicholas was… \n\nOlder than sin, ");
            customTextHL.setText("");


            View.OnClickListener handler = new View.OnClickListener(){
                public void onClick(View v) {

                    switch (v.getId()) {
                        case R.id.widget30: // PLAY

                            if( !mPlayer.isPlaying() ){

                                try {
                                    mPlayer.prepare();
                                } catch (IllegalStateException e) {
                                    // TODO Auto-generated catch block
                                    e.printStackTrace();
                                } catch (IOException e) {
                                    // TODO Auto-generated catch block
                                        e.printStackTrace();
                                }

                                mPlayer.start();

                                mHandler.post(new Runnable(){

                                        public void run(){ 
                                        final long currentPos = mPlayer.getCurrentPosition();

                                            int x = 0;
                                            while( x < 102){
                                                if( currentPos > startEndTime[0][x] && currentPos < startEndTime[1][x] ){//0
                                                customTextHL.append(words[x]);
                                                words[x]="";
                                            }
                                            x++;
                                        } mHandler.postDelayed(this, 1);
                                        }

                                  });
                            }

                            break;

                        case R.id.widget31: // RESET

                            mHandler.post(new Runnable(){

                                public void run(){ 
                                    customTextBG.setText("Nicholas was… \n\nOlder than sin, ");
                                    customTextHL.setText("");
                                }

                          });

                            mPlayer.stop();
                            mPlayer.release();

                            break;
                    }
                }
            };

            // BUTTONS
            findViewById(R.id.widget30).setOnClickListener(handler); // PLAY
            findViewById(R.id.widget31).setOnClickListener(handler); // RESET

        }
    }

main.xml:

android:id="@+id/customTextBG" 
android:textSize="18sp" 
android:text="@+id/customTextBG" 
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/widget31"
android:layout_alignParentLeft="true"
>
</TextView>
<TextView
android:textStyle="bold" 
android:id="@+id/customTextHL" 
android:textSize="18sp" 
android:text="@+id/customTextHL" 
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/widget31"
android:layout_alignParentLeft="true"
android:textColor="#00FF00"
>
</TextView>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_toRightOf="@+id/widget30"
android:text="@string/reset" android:id="@+id/widget31">
</Button>
<Button
android:id="@+id/widget30"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
  • 写回答

2条回答 默认 最新

  • Billy_崔海斌 2012-12-07 06:46
    关注

    在Play的时候运行的runnable要先stop然后才能release, 否则mPlayer.getCurrentPosition()会导致crash。

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 LiBeAs的带隙等于0.997eV,计算阴离子的N和P
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 matlab有关常微分方程的问题求解决
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿
  • ¥15 回答4f系统的像差计算
  • ¥15 java如何提取出pdf里的文字?
  • ¥100 求三轴之间相互配合画圆以及直线的算法