【问题标题】:Android: How to display text in certain milliseconds while media (mp3) is playingAndroid:如何在播放媒体(mp3)时在特定毫秒内显示文本
【发布时间】:2011-03-24 14:06:30
【问题描述】:

我正在尝试制作一个类似卡拉 OK 的应用程序。 我想在特定毫秒到来时显示一个或多个单词。例如:

1148 毫秒 -> 打印“尼古拉斯”
1826 毫秒 -> 打印“是”
2766 毫秒 -> 打印“较旧”
...
*** 毫秒 -> 显示“***”

这是我的代码:


包 com.example.hellomedia;

导入 java.io.IOException; 导入android.app.Activity; 导入android.media.MediaPlayer; 导入android.os.Bundle; 导入android.os.Handler; 导入android.widget.TextView;

公共类 HelloMedia 扩展 Activity {

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

    final Handler mHandler = new Handler();
    final TextView tv = new TextView(this);

    tv.setText("Playing... ");
    setContentView(tv);

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




    final String words[] = {
            "Nicholas ",// 0
            "was ", // 1
            "older ",// 2
            "than ",// 3
            "sin ",// 4
            "and ",// 5
            "his ",// 6
            "beard ",// 7
            "could ",// 8
            "go ",// 9
            "no ",// 10
            "whiter. "// 11
    };



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

            //tv.setText(" [ " + mPlayer.getCurrentPosition() + " ] ");
            if( mPlayer.getCurrentPosition() == 1148 ){//0
                tv.append(words[0]);
            }
            if( mPlayer.getCurrentPosition() == 1826 ){//1
                tv.append(words[1]);
            }
            if( mPlayer.getCurrentPosition() == 2766 ){//2
                tv.append(words[2]);
            }
            mHandler.postDelayed(this, 1);
        }    
    });


}

}

当我运行它时,没有打印数组中的任何单词。

我是安卓开发新手。提前谢谢了。 :)


感谢您对@Matthew Willis、@MarvinLabs 和@Bill Mote 的所有回复。我想出了我需要的东西。我的代码是这样的:

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.widget.TextView;

public class HelloMedia extends Activity {

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

    final Handler mHandler = new Handler();
    final TextView tv = new TextView(this);

    tv.setText("Playing1... ");
    setContentView(tv);

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

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


    final String words[] = {
            "Nicholas ",// 0
            "was ", // 1
            "older ",// 2
            "than ",// 3
            "sin ",// 4
            "and ",// 5
            "his ",// 6
            "beard ",// 7
            "could ",// 8
            "go ",// 9
            "no ",// 10
            "whiter. "// 11
    };

    final long startEndTime[][]={
            {   //start time
                1148,// 0,0
                1826, // 0,1
                2766,// 0,2
                3079,// 0,3
                3549,// 0,4
                4540,// 0,5
                4697,// 0,6
                4801,// 0,7
                5114,// 0,8
                5323,// 0,9
                5532,// 0,10
                5845// 0,11
            },
            {   //end time
                1357,// 1,0
                2192, // 1,1
                3027,// 1,2
                3183,// 1,3
                3966,// 1,4
                4645,// 1,5
                4749,// 1,6
                4958,// 1,7
                5219,// 1,8
                5427,// 1,9
                5740,// 1,10
                6210// 1,11
            }

        };

    mHandler.post(new Runnable(){

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

            int x = 0;

            while( x < 12){
                if( currentPos > startEndTime[0][x] && currentPos < startEndTime[1][x] ){//0
                    tv.append(words[x]);
                    words[x]="";
                }
                x++;
            }

            mHandler.postDelayed(this, 1);
        }    
    });


}
}

【问题讨论】:

    标签: android arrays handler media-player


    【解决方案1】:

    您不能保证每毫秒都会准确地调用您的消息处理程序。你必须放松一下:

     mHandler = new Handler() {
            public void handleMessage(Message msg){     
                final long currentPos = mPlayer.getCurrentPosition();
    
                if (currentPos > 1100 && currentPos < 1300) {
                    tv.append(words[0]);
                } else if (currentPos > 1300 && currentPos < 1400) {
                    tv.append(words[1]);
                } 
    
                mHandler.sendEmptyMessageDelayed(0, 1);
            }
        };
    
    
    mHandler.sendEmptyMessage(0);
    

    【讨论】:

    • 如果您不跟踪是否显示每个单词,这将导致显示多个单词。
    • @Matthew 我同意,只是使用 OP 发布的内容来显示错误所在。他会很快注意到屏幕上充满了文字:)
    【解决方案2】:

    使用不等式来衡量毫秒怎么样:

    if (mPlayer.getCurrentPosition() > 1148) {
        ...
    

    当播放位置正好是 1148 时,你的函数被调用的可能性很小。

    然后你需要跟踪你已经显示的单词,这样你就不会显示两次。

    【讨论】:

    • 我也认为这是问题所在......你认为我正在尝试做的应用程序是不可能的吗?
    • 不,只需使用不等式并记录到目前为止您已显示的单词数。您的应用似乎很有可能,而且您快到了。
    • 同意 Matthew 让您走上正确的道路。马修,“对”的数组可能吗?前几天看到 .pair 并没有用它做任何事情。这是否是将单词和位置(以毫秒为单位)保持在一起的途径?
    • @Bill,是的,这似乎是合理的。然后,您可以在前进时将它们从队列的前面弹出。
    • @Matthew Willis / @Bill Mote:我如何能够记录到目前为止我显示了多少字?我有这些条件: if( currentPos > 1148 && currentPos
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多