我在 xml 文件中有一个 imageview 和按钮。加载 image1 并播放 song1.mp3 文件。当歌曲结束时,它应该加载下一张图像并播放 song2.mp3 歌曲,它应该一直播放到最后一张图像。用于关闭和退出应用程序的按钮。
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnCompletionListener;
import android.os.Bundle;
import android.widget.ImageView;
import android.app.Activity;
import android.content.res.AssetFileDescriptor;
public class MainActivity extends Activity {
public MediaPlayer mpp;
final int image[] = {R.drawable.apple,R.drawable.ball,R.drawable.cat};
int i=0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final ImageView img = (ImageView) findViewById(R.id.img);
String audio[]={"song1.mp3","song2.mp3","song3.mp3"};
AssetFileDescriptor descriptor;
try {
descriptor = getAssets().openFd(audio[i]);
mpp.setDataSource( descriptor.getFileDescriptor(), descriptor.getStartOffset(),descriptor.getLength());
descriptor.close();
mpp.prepare();
mpp.start();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
mpp.setOnCompletionListener(new OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer arg0) {
// TODO Auto-generated method stub
img.setImageResource(image[i]);
i++;
mpp.start();
}
});
} }
main.RuntimeException 中的致命异常无法启动活动