【发布时间】: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