简体   繁体   English

使用Android从jar资源播放音频文件

[英]Play an audio file from jar resource with Android

If I have this method in a jar library: 如果我在jar库中有此方法:

public void playAudioFile()
{
  ClassLoader cLoader = getClass().getClassLoader();
  URL url = cLoader.getResource("audio.mp3"); // in the resource of the jar library
  System.out.println(url.toString());

  MediaPlayer mediaPlayer = new MediaPlayer();
  mediaPlayer.setDataSource(url);
}

After compiling the library, I have: MySDK.jar 编译库后,我有:MySDK.jar

When I add the MySDK.jar into my android application and try to play an audio file: 当我将MySDK.jar添加到我的android应用程序中并尝试播放音频文件时:

private void playAudio()
{
   MySDK.Audio audio = new MySDK.Audio();
   audio.playAudioFile();
}

After compiling the android application, I have: mysampleapp.apk 编译android应用程序后,我有:mysampleapp.apk

When debugging the application, the audio file can't be played because: 调试应用程序时,由于以下原因,无法播放音频文件:

System.out.println(url.toString());

gives: 得到:

jar:file:/data/app/com.mysampleapp.apk!/audio.mp3

The path of the audio should be from the jar file not from the sample app. 音频的路径应来自jar文件,而不应来自示例应用程序。 Therefore, the audio file can not be played. 因此,无法播放音频文件。

What did I do wrong, and how can I play an audio file as a resource file from the jar library? 我做错了什么,如何从jar库中将音频文件作为资源文件播放?

I don't think it's possible to start playback of an mp3 file using a method from within a .jar in Android. 我认为使用Android .jar中的方法无法开始播放mp3文件。 If you need to keep the .jar and play the mp3 that lies within it, I'd temporarily unzip the mp3 to the SD card and play it from there. 如果您需要保留.jar并播放其中的mp3,我会暂时将mp3解压缩到SD卡并从那里播放。

I see only two solutions: 我只看到两种解决方案:

  • The jar file must contains the mp3. jar文件必须包含mp3。
  • Add the base url as parameters. 添加基本​​URL作为参数。

Dalvik doesn't know about your library, it only knows your app. Dalvik不了解您的资料库,仅了解您的应用程序。 One way I think you could work around this is by passing an app-specific resource as a parameter to the playAudioFile() method (or, perhaps, you could initialize a property of its class). 我认为您可以解决此问题的一种方法是,将特定于应用程序的资源作为参数传递给playAudioFile()方法(或者,也许可以初始化其类的属性)。 That way you could copy the file to a proper directory that belongs to your app and still have reusability for your class. 这样,您可以将文件复制到属于您的应用程序的正确目录中,并且仍然可以重用您的类。

Hi try these code in your playAudioFile() method 嗨,请在您的playAudioFile()方法中尝试这些代码

    URL defaultSound = getClass().getResource("/folder/folder/audio.wav");
    File soundFile = new File(defaultSound.toURI());
    System.out.println("defaultSound " + defaultSound);  // check the URL!
    AudioInputStream audioInputStream = AudioSystem.getAudioInputStream(defaultSound);
    Clip clip = AudioSystem.getClip();
    clip.open(audioInputStream);
    clip.start( );

I suspect that this question is very similar to another one on SO (but I don't know how to flag that). 我怀疑这个问题与SO上的另一个问题非常相似(但我不知道如何标记该问题)。 The answer I provided to that one is at: Convert a string to a resource Uri for mp3 playback 我对此提供的答案是: 将字符串转换为资源Uri以便进行mp3播放

In essence you need to get the URI correct, as the above link explains. 本质上,如上面的链接所述,您需要正确获取URI。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM