0

我正在尝试获取一个 mp3 文件并将其保存在手机内存而不是 SD 卡上,然后立即使用媒体播放器播放。当我以前用它在 SD 卡上做同样的事情时,这段代码运行良好,所以在我保存和读取 mp3 的位置似乎可能有一些错误。

这是我的代码:

try {

            MediaPlayer player = new MediaPlayer();
            URI uri = null;
            try {
                uri = new URI(
                        "http://translate.google.com/translate_tts?tl="
                                + oLanguage + "&q=" + text);
            } catch (URISyntaxException e) {
                e.printStackTrace();
            }

            String FileName = "Audio2.mp3";
            URL u = new URL(uri.toASCIIString());
            HttpURLConnection c = (HttpURLConnection) u.openConnection();
            c.setRequestMethod("GET");
            c.setDoOutput(true);
            c.connect();

//          FileOutputStream f = new FileOutputStream(new File(
//                  Environment.getExternalStorageDirectory()
//                          + "/download/" + FileName));

            FileOutputStream f = openFileOutput(FileName, MODE_PRIVATE);                
            InputStream in = c.getInputStream();
            byte[] buffer = new byte[1024];
            int len1 = 0;
            while ((len1 = in.read(buffer)) > 0) {
                f.write(buffer, 0, len1);
            }
            f.close();

//          player.setDataSource(Environment.getExternalStorageDirectory()
//                      + "/download/" + FileName);

            player.setDataSource("/data/data/com.comm/files/" + FileName);              
            player.prepare();
            player.start()

日志猫:

        07-30 12:23:47.319: W/MediaPlayer(22426): info/warning (1, 26)
    07-30 12:23:47.319: E/MediaPlayer(22426): error (1, -4)
    07-30 12:23:47.319: W/System.err(22426): java.io.IOException: Prepare failed.: status=0x1
    07-30 12:23:47.329: W/System.err(22426):    at android.media.MediaPlayer.prepare(Native Method)
    07-30 12:23:47.329: W/System.err(22426):    at com.comm.Connect.Text(Connect.java:396)
    07-30 12:23:47.329: W/System.err(22426):    at com.comm.Connect$7.onClick(Connect.java:233)
    07-30 12:23:47.329: W/System.err(22426):    at android.view.View.performClick(View.java:2485)
    07-30 12:23:47.329: W/System.err(22426):    at android.view.View$PerformClick.run(View.java:9080)
    07-30 12:23:47.329: W/System.err(22426):    at android.os.Handler.handleCallback(Handler.java:587)
    07-30 12:23:47.329: W/System.err(22426):    at android.os.Handler.dispatchMessage(Handler.java:92)
    07-30 12:23:47.329: W/System.err(22426):    at android.os.Looper.loop(Looper.java:130)
    07-30 12:23:47.329: W/System.err(22426):    at android.app.ActivityThread.main(ActivityThread.java:3687)
    07-30 12:23:47.329: W/System.err(22426):    at java.lang.reflect.Method.invokeNative(Native Method)
    07-30 12:23:47.329: W/System.err(22426):    at java.lang.reflect.Method.invoke(Method.java:507)
    07-30 12:23:47.329: W/System.err(22426):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867)
    07-30 12:23:47.329: W/System.err(22426):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:625)
    07-30 12:23:47.329: W/System.err(22426):    at dalvik.system.NativeStart.main(Native Method)

最后,我是否需要指定任何权限以允许我保存到手机内存中?

4

2 回答 2

0

几件事:

  1. 外挂是什么样的?确保您正在做一些有用的事情,例如捕获异常并吐出堆栈跟踪,这将更加有用,并且应该给您一个更具描述性的错误
  2. 如果您尝试将任何内容写入磁盘,请确保您 android.permission.WRITE_EXTERNAL_STORAGE清单文件中有

试着给我们一个堆栈跟踪,这将帮助我们帮助

于 2012-07-30T11:10:15.477 回答
0

After doing some research it seems I needed to use I found I needed to use an input stream the code below solved the problem

        FileOutputStream f = null;
            try {
                f = openFileOutput(FileName, MODE_PRIVATE);
            } catch (Exception e) {
                e.printStackTrace();
            }

            InputStream in = c.getInputStream();
            byte[] buffer = new byte[1024];
            int len1 = 0;
            while ((len1 = in.read(buffer)) > 0) {
                f.write(buffer, 0, len1);
            }
            f.close();

            player = new MediaPlayer();
            FileInputStream fis = new FileInputStream("/data/data/com.comm/files/"+ FileName);
            player.reset();
            player.setDataSource(fis.getFD());
            player.prepare();
            player.start();
            fis.close();
于 2012-08-02T17:43:14.860 回答