0

我正在尝试播放录制的波形文件。播放时,以下语句抛出异常:

Player player = Manager.createPlayer(is, "audio/mpeg");

我播放波形文件的整个代码如下:

if (types[cnt].equals("audio/x-wav")) {
    Class clazz = Class.forName("RecordAudio");
    InputStream is =  
        clazz.getResourceAsStream("file:///SDCard/BlackBerry/original.wav");
    //create an instance of the player from the InputStream
    Player player = Manager.createPlayer(is, "audio/mpeg");
    player.realize();
    player.prefetch();
    //start the player
    player.start();
} 

可能是什么问题呢?

4

2 回答 2

7

getResourceAsStream 函数用于从 JAR/COD 文件中提取资源,而不是从文件系统中提取资源。另外,这比你做的要简单。只需将文件名和路径传递给 createPlayer,如下所示:

try {
    String filename = "file:///SDCard/BlackBerry/original.wav";
    Player player = javax.microedition.media.Manager.Manager.createPlayer( filename );
} catch (IOException e) {
    System.out.println("Error creating player");
} catch (MediaException e) {
    System.out.println("Error media type");
}
于 2009-09-02T12:11:37.937 回答
4

我相信这是因为错误的 MIME 类型。尝试这个:

String fileName = "file:///SDCard/BlackBerry/original.wav";
String mimeType = "audio/x-wav";
String types[] = javax.microedition.media.Manager
        .getSupportedContentTypes(null);
for (int cnt = types.length - 1; cnt >= 0; --cnt) {
    if (types[cnt].equals(mimeType)) {
        InputStream is = null;
        FileConnection fconn = null;
        try {
            fconn = (FileConnection) Connector.open(
            fileName, Connector.READ);
        } catch (IOException e) {
            System.out.println("Error reading file");
        }
        try {
            is = fconn.openInputStream();
        } catch (IOException e) {
            System.out.println("Error opening stream");
        }
        Player player = null;
        try {
            player =                    
            javax.microedition.media.Manager.createPlayer(
            is, mimeType);
        } catch (IOException e) {
            System.out.println("Error creating player");
        } catch (MediaException e) {
            System.out.println("Error media type");
        }
        try {
            player.realize();
        } catch (MediaException e) {
            System.out.println("Player cannot be released");
        }
        try {
            player.prefetch();
        } catch (MediaException e) {
            System.out.println("Player cannot be prefetched");
        }
        // start the player
        try {
            player.start();
        } catch (MediaException e) {
            System.out.println("Player cannot be started");
        }
    }
}

还可以在控制台中查看引发了什么样的异常。

于 2009-09-02T07:14:56.647 回答