Java ME Abbruch in Simulation mit

Enigma228

Bekanntes Mitglied
Hi Leute..
Bin immer noch an meinem einfachen Diktiergerät dran.. nun habe ich es ein wenig erweitert..
und es kommt folgender Fehler wenn ich es im Simulator von Sony probiere:

/*Fehler:
Running with storage root temp.SonyEricsson_W950_Emu53
Running with locale: German_Germany.1252
Warning: To avoid potential deadlock, operations that may block, such as
networking, should be performed in a different thread than the
commandAction() handler.
*/

In der Simulation bzw. auf dem Handy gehe ich so vor:
1. "Aufnahme" anklicken
2. Dateinamen in eingeben und mit OK bestätigen.
3. Im Simulator stellt er jetzt noch die Frage ob er schreiben darf, aber die kann ich schon nicht mehr beantworten weil die Simu abgebrochen wurde mit dem oben genannten Fehler
Auf dem Handy kann ich ich noch die Schreibrechte abfragen und dann stürzt das Programm ab

Wo liegt mein Fehler und wie kann ich ihn korriegieren??

Danke
Thomas
Java:
package RECORDER;

import java.io.ByteArrayOutputStream;
import java.io.DataOutputStream;
import java.io.IOException;

import javax.microedition.io.Connector;
import javax.microedition.io.file.FileConnection;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Form;
import javax.microedition.lcdui.TextField;
import javax.microedition.media.Manager;
import javax.microedition.media.MediaException;
import javax.microedition.media.Player;
import javax.microedition.media.control.RecordControl;
import javax.microedition.midlet.MIDlet;
import javax.microedition.midlet.MIDletStateChangeException;

public class Main extends MIDlet {

	MainForm mainform;
	Display display;
	RecordForm recordform;
	
	public Main() {
		mainform = new MainForm("Media");
		recordform = new RecordForm("Recorder");
	}

	protected void destroyApp(boolean arg0) throws MIDletStateChangeException {
		// TODO Auto-generated method stub

	}

	protected void pauseApp() {
		// TODO Auto-generated method stub

	}

	protected void startApp() throws MIDletStateChangeException {
		display = Display.getDisplay(this);
		display.setCurrent(mainform);
	}

	private class MainForm extends Form implements CommandListener{
		
		Command EXIT, PLAYER, RECORDER;
		
		public MainForm(String title){
			super(title);
			EXIT = new Command("Exit", Command.EXIT, 1);
			PLAYER = new Command("Player", Command.SCREEN, 1);
			RECORDER = new Command("Aufnahme", Command.SCREEN, 1);
			this.addCommand(RECORDER);
			this.addCommand(EXIT);
			//this.addCommand(PLAYER);
			this.setCommandListener(this);
		}

		public void commandAction(Command c, Displayable d) {
			if(c==EXIT){
				try {
					destroyApp(false);
				} catch (MIDletStateChangeException e) {
					e.printStackTrace();
				}
			}
			if(c==PLAYER){
				//display.setCurrent(new PlayerForm("MediaPlayer"));
			}
			if(c==RECORDER){
				display.setCurrent(recordform);
			}
		}
	}
	
	private class RecordForm extends Form implements CommandListener{
		
		private TextField textfield;
		private Command BACK, OK, REC_START, REC_STOP;
		private String path="";
		private boolean breaker;
		private Player captureplayer;
		private RecordControl rc;
		private ByteArrayOutputStream output = new ByteArrayOutputStream();
		private DataOutputStream dos;
		
		public RecordForm(String title){
			super(title);
			BACK = new Command("Zurück", Command.CANCEL, 1);
			OK = new Command("Ok", Command.OK, 1);
			textfield = new TextField("Dateinamen eingeben:", "", 16, TextField.ANY);
			this.addCommand(OK);
			this.addCommand(BACK);
			this.append(textfield);
			this.setCommandListener(this);
		}

		public void commandAction(Command c, Displayable d) {
			if(c==BACK){
				display.setCurrent(mainform);
			}
			if(c==OK){
				path = "file:///e:/"+textfield.getString()+".amr";
				try {
					FileConnection fc = (FileConnection)Connector.open(path,Connector.READ_WRITE);
					if(!fc.exists()){
						fc.create();
					}
					fc.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
				this.deleteAll();
				this.append("Ziel-Datei:\n"+path);
				this.removeCommand(BACK);
				this.removeCommand(OK);
				this.addCommand(REC_START);
			}
			if(c==REC_START){
				this.removeCommand(REC_START);
				this.addCommand(REC_STOP);
				breaker=true;
				this.append("Aufnahme gestartet");
				this.record();
				
			}
			if(c==REC_STOP){
				breaker=false;
				this.append("Aufnahme beendet und in Datei "+path+"abgespeichert!");
				this.removeCommand(REC_STOP);
				this.addCommand(BACK);
			}
		}
		private void record(){
			Thread record = new Thread(){
				public void run(){
					try {
						captureplayer = Manager.createPlayer("capture://audio?encoding=amr");
						captureplayer.realize();
						rc = (RecordControl)captureplayer.getControl("RecordControl");
						rc.setRecordStream(output);
						rc.startRecord();
						captureplayer.start();
						while(breaker){};
						rc.stopRecord();
						rc.commit();
						captureplayer.stop();
						captureplayer.close();
						FileConnection fc2 = (FileConnection)Connector.open(path,Connector.READ_WRITE);
						dos = fc2.openDataOutputStream();
						dos.write(output.toByteArray());
						dos.close();
						output.close();
					} catch (IOException e) {
						e.printStackTrace();
					} catch (MediaException e) {
						e.printStackTrace();
					}
				}
			};
			record.start();
		}
	}
}
 

theodosis

Mitglied
Dann vielleicht hat der Simulator Recht, weil MIDlet ist schon ein spezieles System-Thread, das du nicht blockieren darfst.

Ich würde vorschalgen, das Thread nicht innerhalb der Funktion record(...) zu erstellen, sondern in einer andere Klasse, die von Thread-Klasse vererbt:

Java:
class Record extends Thread
{
  public Record( )
  {
    start();
  }

  public void run()
  {
     try {
                        captureplayer = Manager. ....uzw 
                        ...
  }
}

und ersetze das

Java:
if(c==REC_START)
{
                ....
                this.record();
                
}

mit

Java:
if(c==REC_START)
{
                ....
  new Record()
}

... so grob gesagt !!
 

Enigma228

Bekanntes Mitglied
Habe die Lösung für das angegebene Problem schon gefunden...
Ich habe die Commands-Objekte REC_START und REC_STOP noch nicht erstellt..

der Aufnahmeteil funktioniert, aber..

die Aufnahme mit amr- Codec schluckt ganze Wortteile weg...
mit wav habe ich das problem welches ich in einem vorherigen Thread beschrieben habe..

http://www.java-forum.org/mobile-geraete/115562-suche-aufgenommene-datei.html

um es kurz zu machen, mir gelingt keine saubere Aufnahme.. hat jemand eine Idee?

kurzes kompilierbares Beispiel:
funktionierender Quellcode mit AMR Codec (schluckt aber ganze Wortteile weg)
Java:
import java.io.ByteArrayOutputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.OutputStream;

import javax.microedition.io.Connection;
import javax.microedition.io.Connector;
import javax.microedition.io.file.FileConnection;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Form;
import javax.microedition.media.Manager;
import javax.microedition.media.MediaException;
import javax.microedition.media.Player;
import javax.microedition.media.control.RecordControl;
import javax.microedition.midlet.MIDlet;
import javax.microedition.midlet.MIDletStateChangeException;
import javax.microedition.rms.RecordStore;
import javax.microedition.rms.RecordStoreException;
import javax.microedition.rms.RecordStoreFullException;
import javax.microedition.rms.RecordStoreNotFoundException;

import com.sun.midp.io.j2me.storage.File;


public class FirstCapture extends MIDlet implements CommandListener {

	private Command b_exit, b_record, b_stop;
	private Form form;
	private Display display;
	private Player capturePlayer, playbackPlayer;
	private byte[] recordedAudioArray = null;
	private RecordStore rs;
	private RecordControl rc;
	private ByteArrayOutputStream output = new ByteArrayOutputStream();
	private Thread t;
	private boolean breaker=true;
	
	public FirstCapture() {
		display = Display.getDisplay(this);
		b_exit = new Command("Exit", Command.EXIT, 0);
		b_record = new Command("Record", Command.SCREEN, 1);
		b_stop = new Command("Stop", Command.SCREEN, 2);
		try {
			rs = RecordStore.openRecordStore("MyAudioCaptures", true);
		} catch (RecordStoreFullException e) {
			e.printStackTrace();
		} catch (RecordStoreNotFoundException e) {
			e.printStackTrace();
		} catch (RecordStoreException e) {
			e.printStackTrace();
		}
		form = new Form("FirstCapture");
		form.addCommand(b_exit);
		form.addCommand(b_record);
		//form.addCommand(b_stop);
		form.setCommandListener(this);
		display.setCurrent(form);
	}

	public void commandAction(Command c, Displayable d) {
		if(c==b_exit){
			try {
				this.destroyApp(false);
			} catch (MIDletStateChangeException e) {
				e.printStackTrace();
			}
		}
		if(c==b_record){
			form.deleteAll();
			form.removeCommand(b_exit);
			form.removeCommand(b_record);
			form.addCommand(b_stop);
			breaker=true;
			t  = new Thread(){
				public void run(){
					try {
						form.append("Player erstellen");
						capturePlayer = Manager.createPlayer("capture://audio?encoding=amr");
						form.append("Player.realize()");
						capturePlayer.realize();
						form.append("RecordControl eerstellen");
						rc = (RecordControl)capturePlayer.getControl("RecordControl");
						form.append("RecordLocation festgelegt");
						FileConnection fc = (FileConnection)Connector.open("file:///e:/test.amr",Connector.READ_WRITE);
						if(!fc.exists()){
							fc.create();
							fc.setWritable(true);
						}
						fc.close();
						//rc.setRecordLocation("file:///e:/test.wav");
						form.append("RecordControl.setRecordStream()");
						rc.setRecordStream(output);
						form.append("RecordControl.startRecord()");
						rc.startRecord();
						form.append("Player starten");
						capturePlayer.start();
						form.append("Player gestartet");
						while(breaker){};
						form.append("RecordControl.stopRecord()");
						rc.stopRecord();
						form.append("RecordControl.stopRecord()");
						rc.commit();
						form.append("Player stoppen");
						capturePlayer.stop();
						form.append("Player schliessen");
						capturePlayer.close();
						recordedAudioArray = output.toByteArray();
						form.append("Datenstrom: "+recordedAudioArray.length);
						FileConnection fc2 = (FileConnection)Connector.open("file:///e:/test.amr",Connector.READ_WRITE);
						fc2.setWritable(true);
						form.append("fc2 eingerichtet");
						/*OutputStream bous = fc2.openOutputStream();
						form.append("Outputstream auf Datei geholt");
						bous.write(recordedAudioArray);
						form.append("ByteArray wurde geschrieben");
						bous.close();*/
						DataOutputStream dos = fc2.openDataOutputStream();
						dos.write(output.toByteArray());
						dos.close();
						output.close();
						fc2.close();
						//rs.addRecord(recordedAudioArray, 0, recordedAudioArray.length);
						
					} catch (Exception e) {
						form.append(e.getMessage());
					}
				}
			};
			t.start();
		}
		if(c==b_stop){
			breaker=false;
			form.removeCommand(b_stop);
			form.addCommand(b_record);
			form.addCommand(b_exit);
		}
		
	}

	protected void destroyApp(boolean arg0) throws MIDletStateChangeException {
		notifyDestroyed();

	}

	protected void pauseApp() {
		// TODO Auto-generated method stub

	}

	protected void startApp() throws MIDletStateChangeException {
		
	}

}
 

Ähnliche Java Themen

Neue Themen


Oben