我是 Java 新手,我的 friend 帮助我编写了这段代码。我收到错误,因为我认为我使用了错误的语法。我已经在代码中添加了我遇到问题的注释。我什至不确定是否应该将此代码放在 oncreate
内部还是外部。任何人都可以为我指出一个好的 Java 学习资源的说明吗?
@TargetApi(Build.VERSION_CODES.HONEYCOMB)
public class MainActivity extends ActionBarActivity {
public MediaPlayer mediaPlayer;
@Override
protected void onCreate(Bundle savedInstanceState) {
/*super.onCreate(savedInstanceState);*/
setContentView(R.layout.activity_main);
getActionBar().hide();
mediaPlayer = new MediaPlayer(); //getting an error on this line for invalid syntax.
//not sure what to put here
playB() throws IOException {
playSound(R.raw.b)
}
//not sure what to put here
playSound(int resId) throws IOException {
URI uri = URI.parse("android://com.max.bead/" + resId);
if(mediaPlayer.isPlaying())
{
mediaPlayer.stop();
}
else
{
mediaPlayer.setDataSource(getApplicationContext(), uri);
mediaPlayer.prepare();
mediaPlayer.play();
}
}
}
最佳答案
试试这个。
@Override
protected void onCreate(Bundle savedInstanceState) {
/*super.onCreate(savedInstanceState);*/
setContentView(R.layout.activity_main);
getActionBar().hide();
mediaPlayer = new MediaPlayer(); //getting an error on this line for invalid syntax.
}
//not sure what to put here
private void playB() throws IOException {
playSound(R.raw.b);
}
//not sure what to put here
private void playSound(int resId) throws IOException {
URI uri = URI.parse("android://com.max.bead/" + resId);
if(mediaPlayer.isPlaying())
{
mediaPlayer.stop();
}
else
{
mediaPlayer.setDataSource(getApplicationContext(), uri);
mediaPlayer.prepare();
mediaPlayer.play();
}
}
关于java - 使用媒体播放器时出现奇怪的错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27223703/