我是 android 新手,正在创建一个图片库项目
我已经成功创建了一个旋转图片库
现在我想播放一个声音文件,但发生的事情是,一旦图像改变,音乐就会中断或停止
我试过用不同的线程播放音乐,但没有用。这是我的代码:
public class MainActivity extends Activity {
Timer timer;
TimerTask task;
int cnt = -1;
ImageView icon;
String[] images = {"alienaquacamera", "alienaquadesktop","alienaquaexcel"};
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
icon = (ImageView) findViewById(R.id.img);
MediaPlayer mPlayer = MediaPlayer.create(MainActivity.this, R.raw.ghatotkach);
mPlayer.start();
final Handler mHandler = new Handler();
// Create runnable for posting
final Runnable mUpdateResults = new Runnable() {
public void run() {
changeImage();
}
};
timer = new Timer();
task = new TimerTask() {
@Override
public void run() {
mHandler.post(mUpdateResults);
}
};
timer.schedule(task, 0, 3000);
}
public void changeImage(){
++cnt;
if(cnt==images.length)
cnt = 0;
Resources res = getResources();
String drawableName = images[cnt];
int resID = res.getIdentifier(drawableName , "drawable", getPackageName());
System.out.println("cnt = " + cnt + " , image = " + drawableName + " , ID = " + resID);
if(resID>0)
icon.setImageResource(resID);
}
}
我无法理解出了什么问题