Unity3d for simple voice chat [Android version]

Source: Internet
Author: User

Last time the iOS version of the chat feature unity3d for simple voice chat [iOS version]

This time we're going to go on the Android version for simple voice chat, the process and iOS-based @[email protected]

Here we may encounter a problem:

1.u3d How to communicate between C # and Android Java

2.Android How to call native recording function and playback function

Okay, we'll solve the problem.

1.u3d How to communicate between C # and Android Java

It's a little easier than ios,android here.

In C #, this calls the Android interface

Androidjavaclass Javaclass = new Androidjavaclass ("Com.unity3d.player.UnityPlayer");
Androidjavaobject javaobject = Javaclass. Getstatic<androidjavaobject> ("currentactivity");

Javaobject. Call (MethodName);

So the interface defined in Com.unity3d.player.UnityPlayer can be called by the above method, it is worth noting that if you want to u3d directly to modify the Android interface, not directly in the interface to modify, you can in the Android interface with such a call

This.runonuithread (New Runnable () {

@Override

public void Run () {

Call the Android interface logic

}

});

or with a message

Set Message

Message msg = new Message ();

Msg.what = "MethodName";

Handler MessageHandler = new Handler () {

public void Handlemessage (Message e) {

Call the Android interface logic

}

Send event message

Messagehandler.sendmessage (msg);

Well, in turn, if Android wants to send a message to U3D, you can call the interface provided by Unity-class.jar

Unityplayer.unitysendmessage (String a,string b,string c);

Like iOS, the first parameter is the Gameobject name in the scene, the second parameter is the method name in the component, and the third parameter is any message parameter. In this way, U3d and Android messages are sent to each other to complete.


2.Android How to call native recording function and playback function

Similar to iOS, we need to introduce Android native recording class and audio playback class

Import Android.media.MediaRecorder;

Import Android.media.MediaPlayer;


Mediarecorder

We create Mediarecorder objects to record audio

Mediarecorder Mrecorder = new Mediarecorder (); Mrecorder.setaudiosource (MediaRecorder.AudioSource.MIC);//Set audio source (mic represents microphone)Mrecorder.setoutputformat ( MediaRecorder.OutputFormat.RAW_AMR); Setting the audio output format

Mrecorder.setoutputfile (Voicedatapath);//Set Output file Mrecorder.setaudioencoder (MediaRecorder.AudioEncoder.AMR_NB ); Set audio encoding to AMR

Mrecorder.setmaxduration (20300);//Set recording duration

Mrecorder.setaudioencodingbitrate (4000);

Mrecorder.setaudiosamplingrate (8000);//Sampling rate

Mrecorder.prepare ();

Start recording

Mrecorder.start ();

End recording

Mrecorder.stop ();

Mrecorder.release ();

The Voicedatapath path is the recorded file output path, and unlike iOS, the recording file format can be directly exported to arm format. You can also set callback functions for recording events

Mrecorder.setoninfolistener (New Mediarecorder.oninfolistener () {

Set callback

@Override

public void Oninfo (Mediarecorder arg0, int arg1, int arg2) {

if (arg1 = = mediarecorder.media_recorder_info_max_duration_reached) {//Reach maximum recording time limit

Speakstop ();

}

}

});

MediaPlayer

Playing audio is easier, and passing in the audio file path is possible.

MediaPlayer MPlayer = new MediaPlayer ();

Mplayer.setdatasource (Voicedatapath);

Mplayer.prepare ();

Start playback

Mplayer.start ();

End Play

Mplayer.stop ();

Mplayer.release ();

You can also set the event callback for playback to complete

Mplayer.setoncompletionlistener (New Mediaplayer.oncompletionlistener () {//playback completed callback Listener

@Override

public void Oncompletion (MediaPlayer arg0) {

Stopplay ();

}

});

Unlike iOS, Android does not need to convert arm-formatted audio into the Wav,mediaplayer class to play directly with arm-formatted audio.

OK, the Android version of the voice chat is probably the point here, the process is basically similar to iOS, but the Android API can directly support the ARM audio format recording and playback, it is also quite convenient to use.

Unity3d for simple voice chat [Android version]

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.