我将我的mp3文件放在一个文件夹中并将其保存在特定路径中,我无法从我的代码中播放该文件。怎么玩?
String filePath = (File.separator+"storage"+File.separator+"emulated"+File.separator+"0"+File.separator + "sdcard" + File.separator + "Android" + File.separator + "obb" + File.separator + "convertMp3ToDb" + File.separator + "ldoce6_hwd_gb" + File.separator + fileName).toString();
final File file = new File(filePath);
Uri uri_path = Uri.fromFile(file);
if (uri_path!=null) {
try {
mediaPlayer.setDataSource(uri_path.getPath());
mediaPlayer.prepare();
mediaPlayer.start();
} catch (IOException e) {
e.printStackTrace();
}
我的日志猫错误是:
System.err: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.media.MediaPlayer.setDataSource(java.lang.String)' on a null object reference
W/System.err: at com.mobifusion.android.ldoce5.Fragment.DetailPageFragment.playSound(DetailPageFragment.java:523)
07-14 13:03:27.896 28522-30026/com.mobifusion.android.ldoce5 W/System.err: at com.mobifusion.android.ldoce5.Util.JavaScriptInterface.checkUsUkExampleSoundButton(JavaScriptInterface.java:292)
07-14 13:03:27.896 28522-30026/com.mobifusion.android.ldoce5 W/System.err: at org.chromium.base.SystemMessageHandler.nativeDoRunLoopOnce(Native Method)
07-14 13:03:27.896 28522-30026/com.mobifusion.android.ldoce5 W/System.err: at org.chromium.base.SystemMessageHandler.handleMessage(SystemMessageHandler.java:39)
07-14 13:03:27.896 28522-30026/com.mobifusion.android.ldoce5 W/System.err: at android.os.Handler.dispatchMessage(Handler.java:102)
07-14 13:03:27.896 28522-30026/com.mobifusion.android.ldoce5 W/System.err: at android.os.Looper.loop(Looper.java:145)
07-14 13:03:27.896 28522-30026/com.mobifusion.android.ldoce5 W/System.err: at android.os.HandlerThread.run(HandlerThread.java:61)
答案 0 :(得分:1)
像这样创建路径
File path = Environment.getExternalStoragePublicDirectory("/FolderName/SubFolder/xyz.mp3");
你不必创建或解析它到uri
final File file = new File(path);
if (file !=null) {
try {
// make sure mediaPlayer is not null here if(mediaPlayer != null)
// use prepareAsync which will let you know when media is ready to be played
// Also add mediaPlayer Exceptions in catch block
if(mediaPlayer != null){
mediaPlayer.setDataSource(file.getPath());
mediaPlayer.setOnPreparedListener(this);
mediaPlayer.prepareAsync();
}else{
// you can use context in mediaPlayer constructor but if dont have use like below
mediaPlayer = new MediaPlayer(ActivityName.this);
mediaPlayer.setDataSource(file.getPath());
mediaPlayer.setOnPreparedListener(this);
mediaPlayer.prepareAsync();
}
} catch (IOException e) {
e.printStackTrace();
}
public void onPrepared(MediaPlayer player) {
mediaPlayer.start();
}
了解mediaPlayer架构here.
答案 1 :(得分:0)
1.需要在prepare()
之前添加start()
MediaPlayer mp=new MediaPlayer();
try{
mp.setDataSource(uri_path.getPath());
mp.prepare();
mp.start();
}
catch(Exception e){
e.printStackTrace();
}
这可能有助于你
答案 2 :(得分:0)
这就是你需要初始化和启动媒体播放器的方式。
MediaPlayer mediaPlayer = new MediaPlayer();
mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
mediaPlayer.setDataSource(myUri);
mediaPlayer.prepare();
mediaPlayer.start();