1


我正在尝试开发在黑莓上录制通话的应用程序。我发现很多关于这个和解决方案的问题:

package mypackage;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.OutputStream;

import javax.microedition.io.Connector;
import javax.microedition.io.file.FileConnection;
import javax.microedition.media.Manager;
import javax.microedition.media.Player;
import javax.microedition.media.control.RecordControl;

import net.rim.blackberry.api.phone.Phone;
import net.rim.blackberry.api.phone.PhoneCall;
import net.rim.blackberry.api.phone.PhoneListener;
import net.rim.device.api.ui.component.Dialog;
import net.rim.device.api.ui.container.MainScreen;

/**
 * A class extending the MainScreen class, which provides default standard
 * behavior for BlackBerry GUI applications.
 */
public final class MyScreen extends MainScreen
{
    /**
     * Creates a new MyScreen objects
     * 
 */
Player player;
RecordControl recorder;
private ByteArrayOutputStream output;
byte[] data;
int st;

public MyScreen() {
    // Set the displayed title of the screen
    setTitle("Call Recorder");
    Phone.addPhoneListener(new PhoneListener() {

        public void conferenceCallDisconnected(int callId) {}

        public void callWaiting(int callid) {}

        public void callResumed(int callId) {}

        public void callRemoved(int callId) {}

        public void callInitiated(int callid) {}

        public void callIncoming(int callId) {}

        public void callHeld(int callId) {}

        public void callFailed(int callId, int reason) {}

        public void callEndedByUser(int callId) {}

        public void callDisconnected(int callId) {
            try {
                recorder.commit();
            } catch (IOException e) {
                e.printStackTrace();
            }
            data = output.toByteArray();
            try {
                output.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
            player.close();
            saveRecordedFile(data);
        }

        public void callDirectConnectDisconnected(int callId) {}

        public void callDirectConnectConnected(int callId) {}

        public void callConnected(int callId) {
            PhoneCall phoneCall = Phone.getCall(callId);
            if (phoneCall != null) {
                initPlay();
            }

        }

        public void callConferenceCallEstablished(int callId) {}

        public void callAnswered(int callId) {}

        public void callAdded(int callId) {}
    });

}

private void initPlay() {
    try {
        player = Manager.createPlayer("capture://audio?encoding=audio/amr");
        player.realize();
        recorder = (RecordControl) player.getControl("RecordControl");
        output = new ByteArrayOutputStream();
        recorder.setRecordStream(output);
        recorder.startRecord();
        player.start();
        System.out.println("-----------------------------------------");
        System.out.println("Record started");
        System.out.println("-----------------------------------------");
    } catch (Exception e) {
        Dialog.alert(e + "");
    }

}

public static boolean saveRecordedFile(byte[] data) {
    try {
        String filePath1 = System.getProperty("fileconn.dir.music");
        String fileName = "Call Recorder(";
        boolean existed = true;
        for (int i = 0; i < Integer.MAX_VALUE; i++) {
            try {

                FileConnection fc = (FileConnection) Connector
                        .open(filePath1 + fileName + i + ").amr");

                if (!fc.exists()) {
                    existed = false;
                }
                fc.close();
            } catch (IOException e) {
                Dialog.alert("unable to save");
                return existed;
            }
            if (!existed) {
                fileName += i + ").amr";
                filePath1 += fileName;
                break;
            }
        }
        System.out.println(filePath1);

        FileConnection fconn = (FileConnection) javax.microedition.io.Connector
                .open(filePath1, javax.microedition.io.Connector.READ_WRITE);

        if (fconn.exists())
            fconn.delete();

        fconn.create();

        OutputStream outputStream = fconn.openOutputStream();
        outputStream.write(data);
        outputStream.close();
        fconn.close();
        return true;
    } catch (Exception e) {
    }
    return false;
}

}

但是记录是0kb =(有人可以帮助我吗?我做错了什么?
PS所有权限都是允许
PPS它的问题不是重复如何在黑莓中记录通话?。我的问题是为什么我有空记录?也许我需要在 CallConnected 之前开始记录?

4

0 回答 0