首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在Android中发送开放声音控制(OSC),套接字错误

在Android中发送开放声音控制(OSC),套接字错误
EN

Stack Overflow用户
提问于 2016-04-22 08:32:20
回答 1查看 2K关注 0票数 1

我尝试在android中使用OSC,下面是http://courses.ideate.cmu.edu/physcomp/f14/16-223/tutorial-android-osc-communication/上的“教程:”。它使用了Illposed的JavaOSC。

然而,当我创建一个新的OSCPortOut时,我得到了一个套接字异常错误。你能分享一下这里发生的事情吗?如何解决这个问题。

Android 1.5.1版本4.4非常感谢

代码语言:javascript
复制
package com.jiajunyang.emosoundcontrol1;

import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Toast;

import java.net.*;
import java.util.*;
import com.illposed.osc.*;

public class MainActivity extends AppCompatActivity {

    // init IP and port
    private String myIP = "192.168.11.93";
    private int myPort = 5000;

    // This is for sending messagign.
    private OSCPortOut oscPortOut;

    // This thread will contain all the code for OSC
    private Thread oscThread = new Thread(){
        @Override
        public void run(){
            try{
                // Connect to IP and port
                oscPortOut = new OSCPortOut(InetAddress.getByName(myIP), myPort);
            } catch(UnknownHostException e) {
                Log.d("OSC2", "OSC Port Out UnknownHoseException");
                //Error handling when your IP is not found
                return;
            } catch (SocketException e){
                Log.d("OSC2", "Socket exception error!");
            }
//            } catch (Exception e){
//                Log.d("OSC2", "OSC Port Out Other error");
//                // Error handling for any other errors.
//            }

            /* 2nd loop infinitely and send messages every x ms.
            * */
            while (true){
                //Log.d("OSC2", "Beginning to while loop");

                if (oscPortOut != null){
                    // Creating the messages
                    Object[] m2send = new Object[3];
                    m2send[0] = "Hello";
                    m2send[1] = 100;
                    m2send[2] = 1.23;

                    OSCMessage message = new OSCMessage(myIP, Arrays.asList(m2send));

                    try{
                        Log.d("OSC2", "Successed to send");
                        // Send messages
                        oscPortOut.send(message);
                        // Pause
                        sleep(500);
                    } catch (Exception e){
                        Log.d("OSC2", "Failed to send.");
                    }
                }
            }
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        oscThread.start();



        FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                        .setAction("Action", null).show();
            }
        });

    }

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

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
}
EN

回答 1

Stack Overflow用户

发布于 2016-09-24 02:21:02

我只是遇到了同样的问题,终于解决了。OSC两次使用术语地址,一次用作INetAddress ( IP地址),另一次在OSCMessage中使用。OSCMessage地址只是接收者要侦听的字符串,但它必须以"/“开头。

改变你的路线:

代码语言:javascript
复制
    OSCMessage message = new OSCMessage(myIP, Arrays.asList(m2send));

至:

代码语言:javascript
复制
    OSCMessage message = new OSCMessage("/msgAddress", Arrays.asList(m2send));

在接收方:

代码语言:javascript
复制
    receiver.addListener("/msgAddress", listener);
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/36788814

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档