Setting data source to an raw ID in MediaPlayer

前端 未结 3 1004
眼角桃花
眼角桃花 2020-12-01 13:53

In MediaPlayer.create method an id to a Raw file can be used but how to use that in setDataSource method?

3条回答
  •  情歌与酒
    2020-12-01 14:30

    Refer to the source android.media.MediaPlayer

    AssetFileDescriptor afd = context.getResources().openRawResourceFd(resid);
    if (afd == null) return;
    mediaPlayer.setDataSource(afd.getFileDescriptor(), afd.getStartOffset(), afd.getLength());
    afd.close();
    

    You may want to add try-catch to the block.

提交回复
热议问题