MediaPlayer Android

时间:2016-09-25 21:55:52

标签: android listview android-intent mp3 media-player

我的Mp3应用有2个意图。该代码适用于第二个意图。

在我之前的Mp3应用程序的Intent中,所有可用的歌曲都显示在ListView中,它们的路径保存在ArrayList中。之前的Intent搜索一首歌曲,在ArrayList中添加它的路径,然后搜索下一首歌曲,依此类推。然后我在ListView中传递路径的整个ArrayList和歌曲的位置,以将其用作第二个intent中的ArrayList fp的索引。

该程序(即第二个意图)从Previous Intent中提取mp3文件的路径及其在ListView中的位置。现在我面临使用MediaPlayer类播放该歌曲的问题。 setDataSource()方法中发生异常。

public class PlayerScreen extends AppCompatActivity {

private Intent intent;
private Bundle bndl;                                                    
private ArrayList<String> fp = new ArrayList<String>();
private int ps;
private MediaPlayer mp = new MediaPlayer();

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_player_screen);

    intent = this.getIntent();
    bndl = intent.getExtras();
    fp = bndl.getStringArrayList("path");
    ps = bndl.getInt("pos");
    File file = new File(fp.get(ps));
    mp = MediaPlayer.create(this, MediaStore.Audio.Media.getContentUriForPath(fp.get(ps)));
    FileInputStream inputStream = null;
    try {
        inputStream = new FileInputStream(file);
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    }
    try {
        mp.setDataSource(inputStream.getFD());
    } catch (IOException e) {
        e.printStackTrace();
    }
    try {
        mp.prepare();
    } catch (IOException e) {
        e.printStackTrace();
    }
    mp.start();
}
}

---------崩溃的开始 09-26 03:00:07.126 1161-1161 / prakhar.simplemusicplayer E / AndroidRuntime:FATAL EXCEPTION:main                         过程:prakhar.simplemusicplayer,PID:1161                         java.lang.RuntimeException:无法启动活动ComponentInfo {prakhar.simplemusicplayer / prakhar.simplemusicplayer.PlayerScreen}:java.lang.NullPointerException:尝试调用虚方法'void android.media.MediaPlayer.setDataSource(java.io.FileDescriptor) '在空对象引用上                         在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2426)                         在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2490)                         在android.app.ActivityThread.access $ 900(ActivityThread.java:154)                         在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1354)                         在android.os.Handler.dispatchMessage(Handler.java:102)                         在android.os.Looper.loop(Looper.java:148)                         在android.app.ActivityThread.main(ActivityThread.java:5443)                         at java.lang.reflect.Method.invoke(Native Method)                         在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:728)                         在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)   引起:java.lang.NullPointerException:尝试在空对象引用上调用虚方法'void android.media.MediaPlayer.setDataSource(java.io.FileDescriptor)'                         在prakhar.simplemusicplayer.PlayerScreen.onCreate(PlayerScreen.java:42)                         在android.app.Activity.performCreate(Activity.java:6259)                         在android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1130)                         在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2379)                         在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2490)                         在android.app.ActivityThread.access $ 900(ActivityThread.java:154)                         在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1354)                         在android.os.Handler.dispatchMessage(Handler.java:102)                         在android.os.Looper.loop(Looper.java:148)                         在android.app.ActivityThread.main(ActivityThread.java:5443)                         at java.lang.reflect.Method.invoke(Native Method)                         在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:728)                         在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)

0 个答案:

没有答案