媒体播放器android中的SetDataSource

时间:2013-07-16 13:37:40

标签: android

我有一个媒体播放器课程。我使用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)

3 个答案:

答案 0 :(得分: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();
}

此示例来自另一个question,但问题相同。

答案 1 :(得分: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();

SD卡格式来自 DDMS / FileExplorer / SdCard / video

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

答案 2 :(得分:0)

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