在 Android 中添加 MediaPlayer 对象(歌曲)?
Add MediaPlayer object (Song) in Android?
我正在尝试在我的第一个简单应用程序中播放短歌。在我添加 "ourSong" 对象之前一切正常
package com.example.helloworld;
import java.util.Timer;
import android.app.Activity;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.Bundle;
public class Splash extends Activity {
MediaPlayer ourSong;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.splash);
ourSong = MediaPlayer.create(Splash.this, R.raw.splashsound);
ourSong.start();
Thread timer = new Thread() {
public void run() {
try {
sleep(5000);
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
Intent openStartingPoint = new Intent(
"com.example.helloworld.MAINACTIVITY");
startActivity(openStartingPoint);
}
}
};
timer.start();
}
@Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
ourSong.release();
finish();
}
}
感谢任何帮助
确保您的 "raw" 文件夹位于 "res"。
有关详细信息,请查看 this。
在真实设备上试用!
我的问题是模拟器。我换到我的移动设备(S4),它运行良好。
我正在尝试在我的第一个简单应用程序中播放短歌。在我添加 "ourSong" 对象之前一切正常
package com.example.helloworld;
import java.util.Timer;
import android.app.Activity;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.Bundle;
public class Splash extends Activity {
MediaPlayer ourSong;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.splash);
ourSong = MediaPlayer.create(Splash.this, R.raw.splashsound);
ourSong.start();
Thread timer = new Thread() {
public void run() {
try {
sleep(5000);
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
Intent openStartingPoint = new Intent(
"com.example.helloworld.MAINACTIVITY");
startActivity(openStartingPoint);
}
}
};
timer.start();
}
@Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
ourSong.release();
finish();
}
}
感谢任何帮助
确保您的 "raw" 文件夹位于 "res"。
有关详细信息,请查看 this。
在真实设备上试用!
我的问题是模拟器。我换到我的移动设备(S4),它运行良好。