【问题标题】:Playing a local mp3 in MediaPlayer service, Android在 Android 的 MediaPlayer 服务中播放本地 mp3
【发布时间】:2012-06-12 11:44:52
【问题描述】:

使用服务内的 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;

【问题讨论】:

    标签: android media-player


    【解决方案1】:
    FileInputStream fileStream = new FileInputStream("/sdcard/Download/Bob_Marley-Jammin.mp3");
    mMediaPlayer.setDataSource(fileStream.getFD());
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-06-25
      • 1970-01-01
      • 2013-07-06
      • 1970-01-01
      • 2013-03-24
      • 2015-08-21
      相关资源
      最近更新 更多