Search code examples
androidudpaudio-streamingrtp

Android Use AudioStream To receive audio RTP


I have Problem when trying to receive my audio stream on the other android device

-I have two android phone one of them should send voice via rtp the other should receive it

here is the sender Code

    package com.mywork.johnathan.zoomsenderapk;

import android.content.Intent;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.provider.MediaStore;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

import java.io.IOException;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.util.Enumeration;

import android.app.Activity;
import android.content.Context;
import android.media.AudioManager;
import android.net.rtp.AudioCodec;
import android.net.rtp.AudioGroup;
import android.net.rtp.AudioStream;
import android.net.rtp.RtpStream;
import android.os.StrictMode;
import android.text.format.Formatter;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import  android.media.MediaPlayer;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
        StrictMode.setThreadPolicy(policy);
        try {

            AudioManager audio =  (AudioManager) getSystemService(Context.AUDIO_SERVICE);

            audio.setMode(AudioManager.MODE_NORMAL);

            AudioGroup audioGroup = new AudioGroup();
            audioGroup.setMode(AudioGroup.MODE_NORMAL);

            AudioStream AudioStream = new AudioStream(InetAddress.getByName(getLocalIpAddress()));

            AudioStream.setCodec(AudioCodec.PCMU);

            AudioStream.setMode(RtpStream.MODE_SEND_ONLY);
            //set receiver(vlc player) machine ip address(please update with your machine ip)

            AudioStream.associate(InetAddress.getByName("192.168.1.3"),123);

            AudioStream.join(audioGroup);

            Log.e("pass",getLocalIpAddress().toString());

        } catch (Exception e) {
            Log.e("----------------------", e.toString());
            e.printStackTrace();
        }
    }
    public String getLocalIpAddress()
    {
        WifiManager wifiMgr = (WifiManager)getApplicationContext().getSystemService(WIFI_SERVICE);
        WifiInfo wifiInfo = wifiMgr.getConnectionInfo();

        int ip = wifiInfo.getIpAddress();
        Log.e("-----------ip", Formatter.formatIpAddress(ip));
        return Formatter.formatIpAddress(ip);
    }

    }

But I have no idea how my receiver app code should be

I guess the trick would be in this line

AudioStream.setMode(RtpStream.MODE_SEND_ONLY);

Thanks In Advance ♥


Solution

  • did you try?

    RtpStream.MODE_RECEIVE_ONLY
    

    browse the definition of class RtpStream, you can find all the options there.