gpt4 book ai didi

java - 使用 JavaOSC 向 Android 发送/接收 OSC 消息

转载 作者:行者123 更新时间:2023-12-01 04:24:58 25 4
gpt4 key购买 nike

我正在尝试编写示例 Android 应用程序,允许使用 JavaOSC 库发送和接收 OSC 消息。在示例中,我们使用 OSC 消息发送简单 AndroideekBar 的当前值。

LogCat 说:

09-03 04:56:25.695: I/System.out(1235): can not send

这是我的示例代码,不幸的是它不起作用(似乎我在尝试发送消息时遇到了异常)。也许有人知道这里出了什么问题?

我是 Android 开发新手,感谢您的所有建议...

 package com.remote.secondtest;

import java.io.IOException;
import java.net.InetAddress;
import java.net.SocketException;
import java.net.UnknownHostException;
import java.util.Date;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;
import android.widget.TextView;





public class MainActivity extends Activity implements OnSeekBarChangeListener{

private SeekBar myBar;
private TextView textProgress, textAction;
public OSCPortOut sender = null;
private OSCPortIn receiver;
public InetAddress targetIP;
int port = 5001;
protected int sliderValue;


@Override
public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);
setContentView(R.layout.myscreen);

myBar = (SeekBar)findViewById(R.id.seekBar1);
myBar.setOnSeekBarChangeListener(this);
textProgress = (TextView)findViewById(R.id.textViewProgress);
textAction = (TextView)findViewById(R.id.textViewAction);

}

public void setConnection(){
try {
targetIP = InetAddress.getByName("192.168.0.104");
//targetIP = InetAddress.getLocalHost();
} catch (UnknownHostException e) {
e.printStackTrace(); }

try {
sender = new OSCPortOut(targetIP, 4444); //------set up outgoing ------
} catch (SocketException e) {
e.printStackTrace();
}

/*try { //------set up incoming-------
receiver = new OSCPortIn(4444);
} catch (SocketException e) {
e.printStackTrace();
} */

}


@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}


@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
textProgress.setText("The value is: " + progress);
textAction.setText("changing");

sliderValue = progress;
sendMyOscMessage();
//receiveMyOscMessage();
}

@Override
public void onStartTrackingTouch(SeekBar seekBar) {
textAction.setText("starting to track touch");

}

@Override
public void onStopTrackingTouch(SeekBar seekBar) {
seekBar.setSecondaryProgress(seekBar.getProgress());
textAction.setText("ended tracking touch");

}

public void sendMyOscMessage(){
Object args [] = new Object[2];
args[0] = "Current_Value_is: ";
args[1] = sliderValue;
OSCMessage msg = new OSCMessage("/test_from_device", args);

try {
sender.send(msg);
System.out.println("OSC message sent!");
System.out.println(args[0].toString() + args[1]);
} catch (Exception e) {
System.out.println("can not send");
//e.printStackTrace();
}

}

/*public void receiveMyOscMessage(){
OSCListener myListener = new OSCListener() {

@Override
public void acceptMessage(Date time, OSCMessage message) {

System.out.println("myMessage_received!");
//Object [] args = message.getArguments();
//String myTitle = args[0].toString();
//int currentValue = (Integer)args[1];
}
};

receiver.addListener("/test_to_device", myListener);
receiver.startListening();

}*/


}

UPD:OSCMessage 构造函数已被弃用,但在我将其修改为有效构造函数后,没有任何更改...

OSCMessage msg = new OSCMessage();
msg.setAddress("192.168.0.104");
msg.addArgument(args);

很多 android 错误日志显示:

09-03 07:56:00.794: E/AndroidRuntime(2408): FATAL EXCEPTION: main 09-03 07:56:00.794: E/AndroidRuntime(2408): java.lang.NullPointerException 09-03 07:56:00.794: E/AndroidRuntime(2408): at com.remote.secondtest.MainActivity.sendMyOscMessage(MainActivity.java:132) 09-03 07:56:00.794: E/AndroidRuntime(2408): at com.remote.secondtest.MainActivity.onProgressChanged(MainActivity.java:93) 09-03 07:56:00.794: E/AndroidRuntime(2408): at android.widget.SeekBar.onProgressRefresh(SeekBar.java:91)

最佳答案

您需要访问互联网的权限。

由于行号与您的错误不匹配,我无法确定,但我猜测这是最常见的错误,导致“无法发送”。

关于java - 使用 JavaOSC 向 Android 发送/接收 OSC 消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18588064/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com