使用服务内的 MediaPlayer 在 sdcard 上的默认下载文件夹中播放本地 .mp3 存储的最佳方式是什么?
该文件现在正在播放,但是当我尝试调用 .pause() 函数时,什么也没有发生。
public class PlayService extends Service{
@Override
public int onStartCommand(Intent intent, int flags, int startId){
int key = intent.getIntExtra("key", 0);
MediaPlayer mMediaPlayer = new MediaPlayer();
FileInputStream fileStream = null;
try {
fileStream = new FileInputStream("/sdcard/Download/Bob_Marley-Jammin.mp3");
} catch (FileNotFoundException e1) {
e1.printStackTrace();
}
try {
mMediaPlayer.setDataSource(fileStream.getFD());
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
try {
mMediaPlayer.prepare();
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
switch (key) {
case 0: //Play
if (mMediaPlayer.isPlaying()){
break;
}
else
mMediaPlayer.start();
break;
case 1: //Pause
if(mMediaPlayer.isPlaying())
mMediaPlayer.pause();
break;
case 2: //Stop
if(mMediaPlayer.isPlaying()){
mMediaPlayer.pause();
mMediaPlayer.seekTo(0);
}
break;
default:
break;