1

我有一门课是媒体播放器。我用来mp.setDataSource(path);从 sdcard 获取文件。但是有了这个解决方案,我得到了错误。如何更改将数据设置为播放器工作的方式?我正在阅读有关 FileDescriptor 的信息。如何更改使用 FileDescriptor 的路径?

这是错误:

07-16 15:42:00.827: W/System.err(616): java.io.IOException: setDataSourceFD failed.: status=0x80000000
07-16 15:42:00.827: W/System.err(616):  at android.media.MediaPlayer.setDataSource(Native Method)
07-16 15:42:00.827: W/System.err(616):  at android.media.MediaPlayer.setDataSource(MediaPlayer.java:976)
07-16 15:42:00.827: W/System.err(616):  at VoiceRecorder$7.onClick(VoiceRecorder.java:306)
07-16 15:42:00.827: W/System.err(616):  at android.view.View.performClick(View.java:4202)
07-16 15:42:00.827: W/System.err(616):  at android.view.View$PerformClick.run(View.java:17341)
07-16 15:42:00.827: W/System.err(616):  at android.os.Handler.handleCallback(Handler.java:725)
07-16 15:42:00.827: W/System.err(616):  at android.os.Handler.dispatchMessage(Handler.java:92)
07-16 15:42:00.827: W/System.err(616):  at android.os.Looper.loop(Looper.java:137)
07-16 15:42:00.827: W/System.err(616):  at android.app.ActivityThread.main(ActivityThread.java:5039)
07-16 15:42:00.827: W/System.err(616):  at java.lang.reflect.Method.invokeNative(Native Method)
07-16 15:42:00.827: W/System.err(616):  at java.lang.reflect.Method.invoke(Method.java:511)
07-16 15:42:00.827: W/System.err(616):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
07-16 15:42:00.837: W/System.err(616):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
07-16 15:42:00.837: W/System.err(616):  at dalvik.system.NativeStart.main(Native Method)
07-16 15:42:00.857: W/MessageQueue(616): Handler (android.media.MediaRecorder$EventHandler) {416bcf68} sending message to a Handler on a dead thread
07-16 15:42:00.857: W/MessageQueue(616): java.lang.RuntimeException: Handler (android.media.MediaRecorder$EventHandler) {416bcf68} sending message to a Handler on a dead thread
07-16 15:42:00.857: W/MessageQueue(616):    at android.os.MessageQueue.enqueueMessage(MessageQueue.java:294)
07-16 15:42:00.857: W/MessageQueue(616):    at android.os.Handler.enqueueMessage(Handler.java:618)
07-16 15:42:00.857: W/MessageQueue(616):    at android.os.Handler.sendMessageAtTime(Handler.java:587)
07-16 15:42:00.857: W/MessageQueue(616):    at android.os.Handler.sendMessageDelayed(Handler.java:558)
07-16 15:42:00.857: W/MessageQueue(616):    at android.os.Handler.sendMessage(Handler.java:495)
07-16 15:42:00.857: W/MessageQueue(616):    at android.media.MediaRecorder.postEventFromNative(MediaRecorder.java:977)
07-16 15:42:00.857: W/MessageQueue(616):    at dalvik.system.NativeStart.run(Native Method)
4

3 回答 3

5

例子:

try {
    FileDescriptor fd = null;

    if (isInInternalMemory(audioFilename)) {
        int audioResourceId = mContext.getResources().getIdentifier(audioFilename, "raw", "com.ampirik.audio");
        AssetFileDescriptor afd = mContext.getResources().openRawResourceFd(audioResourceId);
        fd = afd.getFileDescriptor();
    } else if (isInSdCard(audioFilename)) {
        File baseDir = Environment.getExternalStorageDirectory();
        String audioPath = baseDir.getAbsolutePath() + audioFilename + ".mp3";
        FileInputStream fis = new FileInputStream(audioPath);
        fd = fis.getFD();
    }

    if (fd != null) {
        MediaPlayer mediaPlayer = new MediaPlayer();
        mediaPlayer.setDataSource(fd);
        mediaPlayer.prepare();
        mediaPlayer.start();
    }
} catch (Exception e) {
    e.printStackTrace();
}

此示例来自具有相同问题的另一个问题

于 2013-07-16T13:45:11.957 回答
0

要从SdCard播放视频文件,您也可以使用这些:

    VideoView videoView = (VideoView) findViewById(R.id.videoView1);

    String sdcardPath = "/sdcard/smaple.mp4"
    videoView.setVideoPath(sdcardPath);

    /** These one is controlling mediaplayer  */

    MediaController mediaControler = new MediaController(this);
    mediaControler.setAnchorView(videoView);
    videoView.setMediaController(mediaControler);
    videoView.requestFocus();
    videoView.start();

从DDMS/FileExplorer/SdCard/video获取 sdcard 格式

必须记住: VideoView 仅支持.mp4 和 3gp视频格式

于 2015-02-18T10:40:19.750 回答
0

不要忘记在您的项目 AndroidManifest.xml 中添加 android.permission.READ_EXTERNAL_STORAGE

于 2016-08-22T06:28:46.413 回答