2 hemdenry hemdenry 于 2014.10.14 09:37 提问

MediaPlayer的setDataSource出错

我想用service播放音乐
直接上代码:
private static final MediaPlayer mp = new MediaPlayer();
private String songId;
private int op;

@Override
public IBinder onBind(Intent intent) {
    // TODO Auto-generated method stub
    return null;
}

@Override
public void onCreate() {
    // TODO Auto-generated method stub
    // mp = new MediaPlayer();
    mp.setOnCompletionListener(this);
    System.out.println("start mediaplayer");
}

public void onDestroy() {
    System.out.println("stop mediaplayer");
    if (mp != null) {
        mp.stop();
        mp.release();
    }
}

@Override
public void onCompletion(MediaPlayer mp) {
    // TODO Auto-generated method stub
    MainActivity.next();
}

@Override
@Deprecated
public void onStart(Intent intent, int startId) {
    // TODO Auto-generated method stub

}

public void option() {
    switch (op) {
    case 1:
        mp.stop();
        break;
    case 2:
        playMusic();
        break;
    default:
        Toast.makeText(this, "无法找到操作码", Toast.LENGTH_SHORT).show();
        break;
    }
}

public void playMusic() {

    System.out.println("start playing music");

    if (mp.isPlaying()) {
        mp.stop();
    }

    Uri uri = Uri.withAppendedPath(
            MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, songId);

    System.out.println("uri is wrong");

    try {
        mp.reset();
        mp.setDataSource(MusicService.this, uri);
        // mp.setLooping(true);
        mp.prepare();
    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    mp.start();
}

public void OnMp3Change(String songId, int op) {
// TODO Auto-generated method stub
System.out.println("MusicService:" + songId);
this.songId = songId;
this.op = op;
option();
}
最后在playMusic中的mp.setDataSource(MusicService.this, uri)这里报错。
10-14 07:51:23.515: W/System.err(10908): java.lang.NullPointerException
10-14 07:51:23.519: W/System.err(10908): at android.content.ContextWrapper.getContentResolver(ContextWrapper.java:99)
10-14 07:51:23.522: W/System.err(10908): at android.media.MediaPlayer.setDataSource(MediaPlayer.java:876)
10-14 07:51:23.522: W/System.err(10908): at android.media.MediaPlayer.setDataSource(MediaPlayer.java:854)
10-14 07:51:23.522: W/System.err(10908): at com.example.scanmusic.MusicService.playMusic(MusicService.java:85)
10-14 07:51:23.522: W/System.err(10908): at com.example.scanmusic.MusicService.option(MusicService.java:62)
10-14 07:51:23.525: W/System.err(10908): at com.example.scanmusic.MusicService.OnMp3Change(MusicService.java:113)
10-14 07:51:23.525: W/System.err(10908): at com.example.scanmusic.MainActivity.onListItemClick(MainActivity.java:138)
10-14 07:51:23.525: W/System.err(10908): at android.app.ListActivity$2.onItemClick(ListActivity.java:319)
10-14 07:51:23.525: W/System.err(10908): at android.widget.AdapterView.performItemClick(AdapterView.java:301)
10-14 07:51:23.525: W/System.err(10908): at android.widget.AbsListView.performItemClick(AbsListView.java:1100)
10-14 07:51:23.529: W/System.err(10908): at android.widget.AbsListView$PerformClick.run(AbsListView.java:2749)
10-14 07:51:23.529: W/System.err(10908): at android.widget.AbsListView$1.run(AbsListView.java:3430)
10-14 07:51:23.529: W/System.err(10908): at android.os.Handler.handleCallback(Handler.java:725)
10-14 07:51:23.529: W/System.err(10908): at android.os.Handler.dispatchMessage(Handler.java:92)
10-14 07:51:23.529: W/System.err(10908): at android.os.Looper.loop(Looper.java:137)
10-14 07:51:23.529: W/System.err(10908): at android.app.ActivityThread.main(ActivityThread.java:5106)
10-14 07:51:23.532: W/System.err(10908): at java.lang.reflect.Method.invokeNative(Native Method)
10-14 07:51:23.532: W/System.err(10908): at java.lang.reflect.Method.invoke(Method.java:511)
10-14 07:51:23.532: W/System.err(10908): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:821)
10-14 07:51:23.532: W/System.err(10908): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:588)
10-14 07:51:23.532: W/System.err(10908): at dalvik.system.NativeStart.main(Native Method)

2个回答

u010945409
u010945409   2014.12.09 12:17

在设置dataSource前添加
mp.setAudioStreamType(AudioManager.STREAM_MUSIC);
mp.setDataSource(path);

kimjjkkl
kimjjkkl   2014.10.31 14:58

楼主,你报错那行下面打印的是啥?打印Uri看看路径是否正确。
我记得这个方法获得的路径是SD Card里的,你SD Card里放文件了么?

Csdn user default icon
上传中...
上传图片
插入图片