MediaPlayer给出了空异常

MediaPlayer给出了空异常

本文介绍了Android MediaPlayer给出了空异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人可以帮我解决这个奇怪的问题吗,我想播放SDCard的音频文件,这是代码,我知道这是正确的文件路径,但是MediaPlayer给了我一个空异常.任何帮助将不胜感激!

Can someone help me with this strange issue, I want to play an audio file from SDCard , this is the code,I know that this is the right file path, but MediaPlayer gives me a null exception. Any help will be appreciated!

File file = new File(android.os.Environment.getExternalStorageDirectory()+"/rockstar.mp3");
MediaPlayer m = MediaPlayer.create(getApplicationContext(), Uri.fromFile(file));
m.start();

这是stacktarce

This is stacktarce

java.lang.NullPointerException

java.lang.NullPointerException

推荐答案

您是否不缺少.getAbsolutePath()?

File file = new File(android.os.Environment.getExternalStorageDirectory().getAbsolutePath()+"/rockstar.mp3");

这篇关于Android MediaPlayer给出了空异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-30 23:16