Android上原始文件夹和SD卡之间的MediaPlayer问题 [英] MediaPlayer issue between raw folder and sdcard on android

查看:108
本文介绍了Android上原始文件夹和SD卡之间的MediaPlayer问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在制作一个用于使用搜索栏播放歌曲的应用程序.如果我从原始文件夹中播放,则可以正常播放,但如果我从sdcard中播放歌曲,则显示为空指针异常.

I am making an app for playing songs with a seek bar. If I play from the raw folder it works but if I play a song from the sdcard it shows a null pointer exception.

private MediaPlayer mediaPlayer;
mediaPlayer = MediaPlayer.create(this, R.raw.t1); // it works

//  switch to sdcard
mediaPlayer.setDataSource("/sdcard/t1.mp3"); // null pointer exception.

我不知道这是什么问题.请帮助我.

I do not know what is the problem. Please help me.

推荐答案

您需要确保提供给setDataSource()的路径 是完全正确的最好的方法是使用android.os.Environment.getExternalStorageDirectory()

You need to make sure the path you give to setDataSource() is exactly correct. The best way to do this, instead of hardcoding the reference to '/sdcard/', is to use android.os.Environment.getExternalStorageDirectory()

尝试一下,我认为它将为您提供帮助

Try this, I think it will help you

MediaPlayer mediaPlayer = new MediaPlayer();
File path = android.os.Environment.getExternalStorageDirectory();
mediaPlayer.setDataSource(path + "/t1.mp3");

希望对您有帮助

这篇关于Android上原始文件夹和SD卡之间的MediaPlayer问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆