gpt4 book ai didi

Android MediaPlayer 文件的完整路径

转载 作者:太空狗 更新时间:2023-10-29 15:34:04 26 4
gpt4 key购买 nike

我需要获取手机某处(任何位置)文件的完整路径并使用 MediaPlayer 播放它。

我听说过使用 Android 文件选择器(通过启动一个 Intent)。

在测试代码中,我只是将一个资源复制到另一个文件,获取路径并将其传递给 AudioVideoEntry(正如我稍后展示的,一个非常简单且薄的 MediaPlayer 包装器)

这是我写的测试代码:

private String ave_path;
private String ave_file_name = "my_media_content";
private InputStream ave_fis;
private OutputStream ave_fos;
public void testAudioVideoEntry()
{
//get the Activity
Module_JournalEntry journalentryactivity = getActivity();
//open an InputStream to a resource file (in this case strokes.mp3)
ave_fis = journalentryactivity.getResources().openRawResource(module.jakway.JournalEntry.R.raw.strokes);

//open an OutputStream to a new file
try {
ave_fos = journalentryactivity.openFileOutput(ave_file_name,
Context.MODE_PRIVATE);
} catch (FileNotFoundException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
assertTrue(false);
}
catch(Exception e)
{
assertTrue(false);
}

//copy the data from the resource into
//the OutputStream
int data;
try {
while((data = ave_fis.read()) != -1)
{
ave_fos.write(data);
}
assertTrue(true);
}
catch(Exception e)
{
assertTrue(false);
}

//get the full path of the file we wrote to
ave_path = journalentryactivity.getFileStreamPath(ave_file_name).toString();

//and construct a new object of AudioVideoEntry with that path
AudioVideoEntry ave = new AudioVideoEntry(ave_path);

//register an error listener via MediaPlayer's setOnErrorListener
ave.setOnErrorListener(new OnErrorListener()
{
@Override
public boolean onError(MediaPlayer mp,
int what, int extra) {
Log.e("MEDIAPLAYER ERRORS",
"what: " + what + " extra: " + extra);
assertTrue(false);
// TODO Auto-generated method stub
return false;
}
});
ave.prepareMedia();
ave.playMedia();
try {
ave_fis.close();
ave_fos.close();
}
catch(Exception e)
{
assertTrue(false);
e.printStackTrace();
}

AudioVideoEntry 基本上是 MediaPlayer 的薄包装,可以拥有自己的路径:

public class AudioVideoEntry
{
private String path_to_audio_file;
private MediaPlayer mediaplayer;

/**
* Initialize the internal MediaPlayer
* from the String parameter
* @param set_path_to_audio_file
*/
public AudioVideoEntry(String set_path_to_audio_file)
{
path_to_audio_file = set_path_to_audio_file;
mediaplayer = new MediaPlayer();
try {
mediaplayer.setDataSource(path_to_audio_file);
mediaplayer.prepare();
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

public AudioVideoEntry(FileDescriptor fd)
{

mediaplayer = new MediaPlayer();
try {
mediaplayer.setDataSource(fd);
mediaplayer.prepare();
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}



/**
* Begin playing media
*/
public void prepareMedia()
{
try {
mediaplayer.prepare();
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

/**
* play media
* don't forget to prepare() if necessary
*/
public void playMedia()
{
mediaplayer.start();
}

/**
* pause the media
* can be played later
*/
public void pauseMedia()
{
mediaplayer.pause();
}

/**
* stop media
*/
public void stopMedia()
{
mediaplayer.stop();
}

public void setOnErrorListener(OnErrorListener listener)
{
mediaplayer.setOnErrorListener(listener);
}
}

这是 JUnit 测试的 logcat 输出(测试成功,实际结果 - 如 logat 所示 - 不成功)

02-07 09:40:23.129: ERROR/MediaPlayer(1209): error (1, -2147483648)
02-07 09:40:23.139: WARN/System.err(1209): java.io.IOException: Prepare failed.: status=0x1
02-07 09:40:23.149: WARN/System.err(1209): at android.media.MediaPlayer.prepare(Native Method)
02-07 09:40:23.149: WARN/System.err(1209): at module.jakway.JournalEntry.AudioVideoEntry.<init>(AudioVideoEntry.java:39)
02-07 09:40:23.149: WARN/System.err(1209): at module.jakway.JournalEntry.test.Module_JournalEntryTest.testAudioVideoEntry(Module_JournalEntryTest.java:182)
02-07 09:40:23.149: WARN/System.err(1209): at java.lang.reflect.Method.invokeNative(Native Method)
02-07 09:40:23.149: WARN/System.err(1209): at java.lang.reflect.Method.invoke(Method.java:507)
02-07 09:40:23.159: WARN/System.err(1209): at android.test.InstrumentationTestCase.runMethod(InstrumentationTestCase.java:204)
02-07 09:40:23.159: WARN/System.err(1209): at android.test.InstrumentationTestCase.runTest(InstrumentationTestCase.java:194)
02-07 09:40:23.159: WARN/System.err(1209): at android.test.ActivityInstrumentationTestCase2.runTest(ActivityInstrumentationTestCase2.java:186)
02-07 09:40:23.159: WARN/System.err(1209): at junit.framework.TestCase.runBare(TestCase.java:127)
02-07 09:40:23.169: WARN/System.err(1209): at junit.framework.TestResult$1.protect(TestResult.java:106)
02-07 09:40:23.169: WARN/System.err(1209): at junit.framework.TestResult.runProtected(TestResult.java:124)
02-07 09:40:23.169: WARN/System.err(1209): at junit.framework.TestResult.run(TestResult.java:109)
02-07 09:40:23.179: WARN/System.err(1209): at junit.framework.TestCase.run(TestCase.java:118)
02-07 09:40:23.179: WARN/System.err(1209): at android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:169)
02-07 09:40:23.179: WARN/System.err(1209): at android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:154)
02-07 09:40:23.179: WARN/System.err(1209): at android.test.InstrumentationTestRunner.onStart(InstrumentationTestRunner.java:529)
02-07 09:40:23.189: WARN/System.err(1209): at android.app.Instrumentation$InstrumentationThread.run(Instrumentation.java:1447)
02-07 09:40:23.189: ERROR/MediaPlayer(1209): prepareAsync called in state 0
02-07 09:40:23.189: WARN/System.err(1209): java.lang.IllegalStateException
02-07 09:40:23.189: WARN/System.err(1209): at android.media.MediaPlayer.prepare(Native Method)
02-07 09:40:23.189: WARN/System.err(1209): at module.jakway.JournalEntry.AudioVideoEntry.prepareMedia(AudioVideoEntry.java:79)
02-07 09:40:23.199: WARN/System.err(1209): at module.jakway.JournalEntry.test.Module_JournalEntryTest.testAudioVideoEntry(Module_JournalEntryTest.java:197)
02-07 09:40:23.199: WARN/System.err(1209): at java.lang.reflect.Method.invokeNative(Native Method)
02-07 09:40:23.199: WARN/System.err(1209): at java.lang.reflect.Method.invoke(Method.java:507)
02-07 09:40:23.199: WARN/System.err(1209): at android.test.InstrumentationTestCase.runMethod(InstrumentationTestCase.java:204)
02-07 09:40:23.199: WARN/System.err(1209): at android.test.InstrumentationTestCase.runTest(InstrumentationTestCase.java:194)
02-07 09:40:23.199: WARN/System.err(1209): at android.test.ActivityInstrumentationTestCase2.runTest(ActivityInstrumentationTestCase2.java:186)
02-07 09:40:23.199: WARN/System.err(1209): at junit.framework.TestCase.runBare(TestCase.java:127)
02-07 09:40:23.199: WARN/System.err(1209): at junit.framework.TestResult$1.protect(TestResult.java:106)
02-07 09:40:23.199: WARN/System.err(1209): at junit.framework.TestResult.runProtected(TestResult.java:124)
02-07 09:40:23.199: WARN/System.err(1209): at junit.framework.TestResult.run(TestResult.java:109)
02-07 09:40:23.199: WARN/System.err(1209): at junit.framework.TestCase.run(TestCase.java:118)
02-07 09:40:23.199: WARN/System.err(1209): at android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:169)
02-07 09:40:23.199: WARN/System.err(1209): at android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:154)
02-07 09:40:23.199: WARN/System.err(1209): at android.test.InstrumentationTestRunner.onStart(InstrumentationTestRunner.java:529)
02-07 09:40:23.199: WARN/System.err(1209): at android.app.Instrumentation$InstrumentationThread.run(Instrumentation.java:1447)
02-07 09:40:23.199: ERROR/MediaPlayer(1209): start called in state 0
02-07 09:40:23.199: ERROR/MediaPlayer(1209): error (-38, 0)

编辑:为什么 MediaPlayer 会失败?

谢谢!龙鸦

最佳答案

AudioVideoEntry.prepareMedia() 方法中对 MediaPlayer.prepare() 的第二次调用(已在 AudioVideoEntry 构造函数中调用过一次)很容易发现,其他人已经注意到了。

第一个错误更难发现。

我使用 Ogg 文件进行测试。

第一个线索来自 davidsparks 在 android-platform - Ogg on G1 中的回复(最后一个)

As long as the files have a .ogg extension, they should play with the built-in music player. We rely on the file extensions because there is no centralized file recognizer for the media scanner.

第二条线索来自[android-developers] Re: File permission about MediaPlayer

Due to the Android security model, MediaPlayer does not have root access rights. It can access the sdcard, but it can't access private app directories.

Your app can explicitly grant MediaPlayer temporary access to secure files by opening the file and passing the file descriptor to MediaPlayer using the setDataSource(FileDescriptor fd) method.

如果查看输出流的绝对路径,您会发现它位于应用程序包名称目录下的 /data/data 目录中。

请原谅时间戳 - 我向后工作以获取要在 OS2.1update1 (API7) 模拟器上显示的数据。

您的代码有:

String ave_file_name = "my_media_content";

ave_fos = activity.openFileOutput(ave_file_name, Context.MODE_PRIVATE);

DDMS 显示:

02-10 05:10:28.253: WARN/MediaPlayer(1992): info/warning (1, 26)
02-10 05:10:28.253: ERROR/PlayerDriver(31): Command PLAYER_SET_DATA_SOURCE completed with an error or info PVMFErrNotSupported
02-10 05:10:28.253: ERROR/MediaPlayer(1992): error (1, -4)
02-10 05:10:28.274: WARN/PlayerDriver(31): PVMFInfoErrorHandlingComplete

如果我们将文件JUST更改为MODE_WORLD_READABLE:

String ave_file_name = "my_media_content";

ave_fos = activity.openFileOutput(ave_file_name, Context.MODE_WORLD_READABLE);

DDMS 没有改善:

02-10 05:08:28.543: WARN/MediaPlayer(1900): info/warning (1, 26)
02-10 05:08:28.553: ERROR/PlayerDriver(31): Command PLAYER_SET_DATA_SOURCE completed with an error or info PVMFErrNotSupported
02-10 05:08:28.553: ERROR/MediaPlayer(1900): error (1, -4)
02-10 05:08:28.563: WARN/PlayerDriver(31): PVMFInfoErrorHandlingComplete

如果我们将文件扩展名JUST更改为ogg:

String ave_file_name = "my_media_content.ogg";

ave_fos = activity.openFileOutput(ave_file_name, Context.MODE_PRIVATE);

我们得到 DDMS 输出的变化:

02-10 04:59:30.153: ERROR/MediaPlayerService(31):   error: -2
02-10 04:59:30.163: ERROR/MediaPlayer(1603): Unable to to create media player

但是当我们将两者结合时:

String ave_file_name = "my_media_content.ogg";

ave_fos = activity.openFileOutput(ave_file_name, Context.MODE_WORLD_READABLE);

DDMS 显示没有错误。

关于Android MediaPlayer 文件的完整路径,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4924600/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com