0

我的代码如下 - 我尝试了黑莓网站的文档。它没有创建音频文件。如何解决这个问题呢 ?。提前致谢。

import net.rim.device.api.ui.*;
import net.rim.device.api.ui.container.*;
import net.rim.device.api.ui.component.*;
import java.lang.*;

import javax.microedition.io.Connector;
import javax.microedition.io.file.FileConnection;
import javax.microedition.media.*;
import javax.microedition.media.Manager;

import java.io.*;
import javax.microedition.media.control.*;

public class AudioRecording extends UiApplication
{
public static void main(String[] args)
{
     AudioRecording app = new AudioRecording();
     app.enterEventDispatcher();
}

public AudioRecording()
{
     pushScreen(new AudioRecordingDemoScreen());
}

private class AudioRecordingDemoScreen extends MainScreen  
{   
    private AudioRecorderThread _recorderThread;
    ByteArrayOutputStream  bt;
    DataOutputStream  ot;
    public AudioRecordingDemoScreen()
    {
        //setTitle("Audio recording demo");

        addMenuItem(new StartRecording());
        addMenuItem(new StopRecording());
    }

    private class StartRecording extends MenuItem 
    {
        public StartRecording() 
        {
            super("Start recording", 0, 100);
        }

        public void run() 
        {
            try 
            {
                AudioRecorderThread newRecorderThread = new AudioRecorderThread();
                newRecorderThread.start();
                _recorderThread = newRecorderThread;
            }  
            catch (Exception e) 
            {
                Dialog.alert(e.toString());
            }
        }
    }

    private class StopRecording extends MenuItem 
    {
        public StopRecording() 
        {
            super("Stop recording", 0, 100);
        }

        public void run() 
        {
            try 
            {
                if (_recorderThread != null) 
                { 
                    _recorderThread.stop();
                }
            } 
            catch (Exception e) 
            {
                Dialog.alert(e.toString());
            }
        }
    }

    private class AudioRecorderThread extends Thread implements javax.microedition.media.PlayerListener
    {
        private Player _player;
        private RecordControl _recordControl;

        AudioRecorderThread()
        {
        }

        public void run() 
        {
            try 
            {
                _player = javax.microedition.media.Manager.createPlayer("capture://audio?encoding=amr");

                _player.addPlayerListener(this);

                _player.realize();
                _recordControl = (RecordControl) _player.getControl( "RecordControl" );

                FileConnection  fc = (FileConnection)Connector.open("file:///Device Memory/home/user/music/recordingFile.amr", Connector.READ_WRITE );
                if(!fc.exists()){
                    fc.create();
                }
                 ot = fc.openDataOutputStream();
                _recordControl.setRecordStream(ot);


                _recordControl.startRecord(); 
                _player.start();

            }
            catch( IOException e ) 
            {
                Dialog.alert(e.toString());
            }
            catch( MediaException e ) 
            {
                Dialog.alert(e.toString());
            }
        }
        public void stop() 
        {
        /*    if (_player != null) 
            {
                 _player.close();
                 _player = null;
            }

            if (_recordControl != null) 
            {
                _recordControl.stopRecord();*/

                try 
                {

                    _recordControl.commit();


                } 
                catch (Exception e) 
                {
                    Dialog.alert(e.toString());
                }
               /* _recordControl = null;
            } */



        }

        public void playerUpdate(Player player, String event, Object eventData) 
        {
            Dialog.alert("Player " + player.hashCode() + " got event " + event + ": " + eventData);
        }
    }
}
}

它没有创建文件。如何解决这个问题呢

4

1 回答 1

0

试试下面的代码 -

import net.rim.device.api.ui.*;
import net.rim.device.api.ui.container.*;
import net.rim.device.api.ui.component.*;
import java.lang.*;

import javax.microedition.io.Connector;
import javax.microedition.io.file.FileConnection;
import javax.microedition.media.*;
import javax.microedition.media.Manager;

import java.io.*;
import javax.microedition.media.control.*;

public class AudioRecording extends UiApplication
{
public static void main(String[] args)
{
     AudioRecording app = new AudioRecording();
     app.enterEventDispatcher();
}

public AudioRecording()
{
     pushScreen(new AudioRecordingDemoScreen());
}

private class AudioRecordingDemoScreen extends MainScreen  
{   
    private AudioRecorderThread _recorderThread;
    ByteArrayOutputStream  bt;
    DataOutputStream  ot;
    public AudioRecordingDemoScreen()
    {
        //setTitle("Audio recording demo");

        addMenuItem(new StartRecording());
        addMenuItem(new StopRecording());
    }

    private class StartRecording extends MenuItem 
    {
        public StartRecording() 
        {
            super("Start recording", 0, 100);
        }

        public void run() 
        {
            try 
            {
                AudioRecorderThread newRecorderThread = new AudioRecorderThread();
                newRecorderThread.start();
                _recorderThread = newRecorderThread;
            }  
            catch (Exception e) 
            {
                Dialog.alert(e.toString());
            }
        }
    }

    private class StopRecording extends MenuItem 
    {
        public StopRecording() 
        {
            super("Stop recording", 0, 100);
        }

        public void run() 
        {
            try 
            {
                if (_recorderThread != null) 
                { 
                    _recorderThread.stop();
                }
            } 
            catch (Exception e) 
            {
                Dialog.alert(e.toString());
            }
        }
    }

    private class AudioRecorderThread extends Thread implements javax.microedition.media.PlayerListener
    {
         private Player _player;
           private RecordControl _rcontrol;
           private ByteArrayOutputStream _output;
           private byte _data[];

           AudioRecorderThread() {}

           private int getSize()
           {
               return (_output != null ? _output.size() : 0);
           }

           private byte[] getVoiceNote()
           {
              return _data;
           }

           public void run() {
              try {
                  // Create a Player that captures live audio.
                  _player = Manager.createPlayer("capture://audio");
                  _player.realize();

                  // Get the RecordControl, set the record stream,
                  _rcontrol = (RecordControl)_player.getControl("RecordControl");

                  //Create a ByteArrayOutputStream to capture the audio stream.
                  _output = new ByteArrayOutputStream();
                  _rcontrol.setRecordStream(_output);
                  _rcontrol.startRecord();
                  _player.start();

              } catch (final Exception e) {
                 UiApplication.getUiApplication().invokeAndWait(new Runnable() {
                    public void run() {
                       Dialog.inform(e.toString());
                    }
                 });
              }
           }

           public void stop() {
              try {
                   //Stop recording, capture data from the OutputStream,
                   //close the OutputStream and player.
                   _rcontrol.commit();
                   _data = _output.toByteArray();

                   saveRecordedFile(_data);

                   _output.close();
                   _player.close();

              } catch (Exception e) {
                 synchronized (UiApplication.getEventLock()) {
                    Dialog.inform(e.toString());
                 }
              }
           }

           public void playerUpdate(Player player, String event, Object eventData) 
           {
               Dialog.alert("Player " + player.hashCode() + " got event " + event + ": " + eventData);
           }
        }
}



public static boolean saveRecordedFile(byte[] data) {
    try {
        String filePath1 = System.getProperty("fileconn.dir.music");
        String fileName = "sample";
        boolean existed = true;
        for (int i = 0; i < Integer.MAX_VALUE; i++) {
            try {
                FileConnection fc = (FileConnection) Connector.open(filePath1 + fileName+ ".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);
        System.out.println("");
        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;
}


 }
于 2012-04-20T12:08:36.727 回答