我在第一次應用程序旅程中遇到了另一個錯誤:)我想在應用程序加載時播放聲音。這是一個.wav文件。它持續2秒,但當我在舊三星S4上運行應用程序時,它不會播放。 IDE中沒有任何錯誤,或者我可以看到的任何錯誤,我檢查過「mp」是否有值並且確實如此。環顧職位,大多數人都有'mp'= null的問題。而我的價值只是沒有聲音從電話裏出來......再次,任何幫助表示讚賞!在Android上播放聲音
public class OpeningScreen extends Activity {
@Override
// create the screen state
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// connect the xml layout file
setContentView(R.layout.activity_opening_screen);
final MediaPlayer mp = new MediaPlayer();
mp.create(this, R.raw.welcome_message);
mp.start();
// create the on touch listener
ConstraintLayout layout = (ConstraintLayout) findViewById(R.id.opening_layout);
layout.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
// change the screen to a new state
Intent intent = new Intent(OpeningScreen.this, GameScreen.class);
// start the new activity
startActivity(intent);
// stop welcome sound (if still playing)
mp.stop();
return true;
}
});
}
}
這就是我的第一次。但通過這行代碼後,mp仍然是空的。 –
我只是做了一遍,這次它工作。也許我搞砸了文件名或某種類型。謝謝! –