Android 从内部存储播放资源文件会导致 MediaPlayer.prepare 给出 IOException
我的应用程序播放为我的应用程序指定的内部目录(/data/data/com...)中的音频资源文件。似乎可以将文件下载到该位置,setDataSource(String path) 不会抛出任何异常,但 MediaPlayer.prepare() 会抛出 IOException。相同的代码适用于 SD 卡。为什么会发生这种情况?
编辑:
让我们假设这是代码;它比我的代码更简单,并且抛出相同的异常:
package com.app.MediaPlayerTest;
public class MediaTest extends Activity {
MediaPlayer mp;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
DownloadFiles();
MusicPlay();
}
public void DownloadFiles() {
//Downloads Files
}
public void MusicPlay()
{
try {
mp.setDataSource("/data/data/com.app.pronounce/winds.mp3");
} catch (IllegalArgumentException e1) {
e1.printStackTrace();
} catch (IllegalStateException e1) {
e1.printStackTrace();
} catch (IOException e1) {
e1.printStackTrace();
}
try {
mp.prepare();
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
mp.setLooping(true);
mp.start();
}
}
至于堆栈跟踪:
(mutexes: tll=0 tsl=0 tscl=0 ghl=0 hwl=0 hwll=0)
"main" prio=5 tid=1 NATIVE
| group="main" sCount=1 dsCount=0 obj=0x4001f1a8 self=0xce48
| sysTid=338 nice=0 sched=0/0 cgrp=bg_non_interactive handle=-1345006528
| schedstat=( 151460588 425586896 45 )
at android.os.BinderProxy.transact(Native Method)
at android.app.ActivityManagerProxy.handleApplicationCrash(ActivityManagerNative.java:2547)
at com.android.internal.os.RuntimeInit$UncaughtHandler.uncaughtException(RuntimeInit.java:76)
at java.lang.ThreadGroup.uncaughtException(ThreadGroup.java:854)
at java.lang.ThreadGroup.uncaughtException(ThreadGroup.java:851)
at dalvik.system.NativeStart.main(Native Method)
"Binder Thread #2" prio=5 tid=8 NATIVE
| group="main" sCount=1 dsCount=0 obj=0x40512b30 self=0x156e90
| sysTid=346 nice=0 sched=0/0 cgrp=default handle=1570912
| schedstat=( 4357682 930487 3 )
at dalvik.system.NativeStart.run(Native Method)
"Binder Thread #1" prio=5 tid=7 NATIVE
| group="main" sCount=1 dsCount=0 obj=0x40512a68 self=0x17f578
| sysTid=345 nice=0 sched=0/0 cgrp=bg_non_interactive handle=604904
| schedstat=( 6939806 13372136 3 )
at dalvik.system.NativeStart.run(Native Method)
"Compiler" daemon prio=5 tid=6 VMWAIT
| group="system" sCount=1 dsCount=0 obj=0x4050eba8 self=0x938c8
| sysTid=344 nice=0 sched=0/0 cgrp=bg_non_interactive handle=1099136
| schedstat=( 4770066 33579300 5 )
at dalvik.system.NativeStart.run(Native Method)
"JDWP" daemon prio=5 tid=5 VMWAIT
| group="system" sCount=1 dsCount=0 obj=0x4050eaf8 self=0x10c3c0
| sysTid=343 nice=0 sched=0/0 cgrp=bg_non_interactive handle=1098624
| schedstat=( 14899224 33240040 20 )
at dalvik.system.NativeStart.run(Native Method)
"Signal Catcher" daemon prio=5 tid=4 RUNNABLE
| group="system" sCount=0 dsCount=0 obj=0x4050ea38 self=0x93570
| sysTid=342 nice=0 sched=0/0 cgrp=bg_non_interactive handle=588000
| schedstat=( 24278832 4707632 7 )
at dalvik.system.NativeStart.run(Native Method)
"GC" daemon prio=5 tid=3 VMWAIT
| group="system" sCount=1 dsCount=0 obj=0x4050e990 self=0x8f720
| sysTid=341 nice=0 sched=0/0 cgrp=bg_non_interactive handle=1099336
| schedstat=( 791698 556969 3 )
at dalvik.system.NativeStart.run(Native Method)
"HeapWorker" daemon prio=5 tid=2 VMWAIT
| group="system" sCount=1 dsCount=0 obj=0x4050e8d8 self=0x10c740
| sysTid=340 nice=0 sched=0/0 cgrp=bg_non_interactive handle=1357728
| schedstat=( 211702049 225986921 9 )
at dalvik.system.NativeStart.run(Native Method)
My app plays audio resource files from the internal directory designated for my app (/data/data/com...). It seems to download the files to that location okay, setDataSource(String path) doesn't throw any exceptions, but MediaPlayer.prepare() throws IOException.The same code works on the SD card. Why is this happening?
EDIT:
Let's assume this is the code; it's simpler than my code and it throws the same exception:
package com.app.MediaPlayerTest;
public class MediaTest extends Activity {
MediaPlayer mp;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
DownloadFiles();
MusicPlay();
}
public void DownloadFiles() {
//Downloads Files
}
public void MusicPlay()
{
try {
mp.setDataSource("/data/data/com.app.pronounce/winds.mp3");
} catch (IllegalArgumentException e1) {
e1.printStackTrace();
} catch (IllegalStateException e1) {
e1.printStackTrace();
} catch (IOException e1) {
e1.printStackTrace();
}
try {
mp.prepare();
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
mp.setLooping(true);
mp.start();
}
}
As for the stack trace:
(mutexes: tll=0 tsl=0 tscl=0 ghl=0 hwl=0 hwll=0)
"main" prio=5 tid=1 NATIVE
| group="main" sCount=1 dsCount=0 obj=0x4001f1a8 self=0xce48
| sysTid=338 nice=0 sched=0/0 cgrp=bg_non_interactive handle=-1345006528
| schedstat=( 151460588 425586896 45 )
at android.os.BinderProxy.transact(Native Method)
at android.app.ActivityManagerProxy.handleApplicationCrash(ActivityManagerNative.java:2547)
at com.android.internal.os.RuntimeInit$UncaughtHandler.uncaughtException(RuntimeInit.java:76)
at java.lang.ThreadGroup.uncaughtException(ThreadGroup.java:854)
at java.lang.ThreadGroup.uncaughtException(ThreadGroup.java:851)
at dalvik.system.NativeStart.main(Native Method)
"Binder Thread #2" prio=5 tid=8 NATIVE
| group="main" sCount=1 dsCount=0 obj=0x40512b30 self=0x156e90
| sysTid=346 nice=0 sched=0/0 cgrp=default handle=1570912
| schedstat=( 4357682 930487 3 )
at dalvik.system.NativeStart.run(Native Method)
"Binder Thread #1" prio=5 tid=7 NATIVE
| group="main" sCount=1 dsCount=0 obj=0x40512a68 self=0x17f578
| sysTid=345 nice=0 sched=0/0 cgrp=bg_non_interactive handle=604904
| schedstat=( 6939806 13372136 3 )
at dalvik.system.NativeStart.run(Native Method)
"Compiler" daemon prio=5 tid=6 VMWAIT
| group="system" sCount=1 dsCount=0 obj=0x4050eba8 self=0x938c8
| sysTid=344 nice=0 sched=0/0 cgrp=bg_non_interactive handle=1099136
| schedstat=( 4770066 33579300 5 )
at dalvik.system.NativeStart.run(Native Method)
"JDWP" daemon prio=5 tid=5 VMWAIT
| group="system" sCount=1 dsCount=0 obj=0x4050eaf8 self=0x10c3c0
| sysTid=343 nice=0 sched=0/0 cgrp=bg_non_interactive handle=1098624
| schedstat=( 14899224 33240040 20 )
at dalvik.system.NativeStart.run(Native Method)
"Signal Catcher" daemon prio=5 tid=4 RUNNABLE
| group="system" sCount=0 dsCount=0 obj=0x4050ea38 self=0x93570
| sysTid=342 nice=0 sched=0/0 cgrp=bg_non_interactive handle=588000
| schedstat=( 24278832 4707632 7 )
at dalvik.system.NativeStart.run(Native Method)
"GC" daemon prio=5 tid=3 VMWAIT
| group="system" sCount=1 dsCount=0 obj=0x4050e990 self=0x8f720
| sysTid=341 nice=0 sched=0/0 cgrp=bg_non_interactive handle=1099336
| schedstat=( 791698 556969 3 )
at dalvik.system.NativeStart.run(Native Method)
"HeapWorker" daemon prio=5 tid=2 VMWAIT
| group="system" sCount=1 dsCount=0 obj=0x4050e8d8 self=0x10c740
| sysTid=340 nice=0 sched=0/0 cgrp=bg_non_interactive handle=1357728
| schedstat=( 211702049 225986921 9 )
at dalvik.system.NativeStart.run(Native Method)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
MediaPlayer 要求正在播放的文件具有全局可读权限。您可以在 adb shell 中使用以下命令查看文件的权限:
您可能会看到“-rw-----”,这意味着只有所有者(您的应用程序,而不是 MediaPlayer)具有读/写权限。
注意:您的手机必须已 root 才能使用 ls 命令而不指定文件(在内部存储器中)。
如果您的手机已 root,您可以使用以下命令在 adb shell 中添加世界读取权限:
如果您需要以编程方式修改这些权限(需要 root 手机!),您可以在应用程序代码中使用以下命令
或者
: 基本上是一个linux命令。有关 chmod 的更多信息,请参阅 https://help.ubuntu.com/community/FilePermissions。
编辑:在此处找到了另一种简单的方法(对于那些没有 root 的手机很有用)。由于应用程序拥有该文件,因此它可以创建一个文件描述符并将其传递给 mediaPlayer.setDataSource():
这种方法完全避免了权限问题。
MediaPlayer requires that the file being played has world-readable permissions. You can view the permissions of the file with the following command in adb shell:
You will probably see "-rw------", which means that only the owner (your app, not MediaPlayer) has read/write permissions.
Note: Your phone must be rooted in order to use the ls command without specifying the file (in the internal memory).
If your phone is rooted, you can add world-read permissions in adb shell with the following command:
If you need to modify these permissions programmatically (requires rooted phone!), you can use the following command in your app code:
or
Which is basically a linux command. See https://help.ubuntu.com/community/FilePermissions for more on chmod.
EDIT: Found another simple approach here (useful for those without rooted phones). Since the application owns the file, it can create a file descriptor and pass that to mediaPlayer.setDataSource():
This approach avoids the permission issue completely.
我知道这个问题比泥土更古老,但它帮助我解决了我的问题。以下工作正常:
世界可读标志是这里的重要部分。
I know this question is older than dirt, but it helped me flush out my issue. The following works fine:
The world readable flag is the important part here.