如何集成我的 Android 录音机代码来显示用户的当前位置?

发布于 2024-12-15 06:50:15 字数 4744 浏览 3 评论 0原文

为了制作一个简单的录音机应用程序,我阅读了大量代码。它工作得很好,而且非常简单,但现在我想将它与一些额外的功能集成,以使用简单的实用程序(例如文本框)显示用户的当前位置。我非常了解 Java 编程,但我仍在学习 Android,所以我对如何做事情有点犹豫。我已经阅读了许多教程和示例,也尝试了许多不同的方法,但似乎没有任何效果。我正在使用 Eclipse 和 Android SDK 进行版本 8 的开发。我还有一部 Android 手机来测试该应用程序,事实证明该应用程序非常有帮助。

这是我的代码,它可以工作,它是一个用作录音机的应用程序。任何帮助将不胜感激:

import android.app.Activity;
import android.widget.LinearLayout;
import android.os.Bundle;
import android.os.Environment;
import android.view.ViewGroup;
import android.widget.Button;
import android.view.View;
import android.view.View.OnClickListener;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.media.MediaRecorder;
import android.media.MediaPlayer;
import java.io.IOException;


public class AndroidVoiceActivity extends Activity{
private static final String LOG_TAG = "AudioRecordTest";
private static String mFileName = null;

private RecordButton mRecordButton = null;
private MediaRecorder mRecorder = null;

private PlayButton   mPlayButton = null;
private MediaPlayer   mPlayer = null;

/***************** Record Button ********************/
private void onRecord(boolean start) {
    if (start) {
        startRecording();
    } else {
        stopRecording();
    }
}

private void startRecording() {
    mRecorder = new MediaRecorder();
    mRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
    mRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
    mRecorder.setOutputFile(mFileName);
    mRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);

    try {
        mRecorder.prepare();
    } catch (IOException e) {
        Log.e(LOG_TAG, "prepare() failed");
    }

    mRecorder.start();
}

private void stopRecording() {
    mRecorder.stop();
    mRecorder.release();
    mRecorder = null;
}

class RecordButton extends Button {
    boolean mStartRecording = true;

    OnClickListener clicker = new OnClickListener() {
        public void onClick(View v) {
            onRecord(mStartRecording);
            if (mStartRecording) {
                setText("Stop recording");
            } else {
                setText("Start recording");
            }
            mStartRecording = !mStartRecording;
        }
    };

    public RecordButton(Context ctx) {
        super(ctx);
        setText("Start recording");
        setOnClickListener(clicker);
    }
}

/***************** Play Button ********************/
private void startPlaying() {
    mPlayer = new MediaPlayer();
    try {
        mPlayer.setDataSource(mFileName);
        mPlayer.prepare();
        mPlayer.start();
    } catch (IOException e) {
        Log.e(LOG_TAG, "prepare() failed");
    }
}

private void stopPlaying() {
    mPlayer.release();
    mPlayer = null;
}

private void onPlay(boolean start) {
    if (start) {
        startPlaying();
    } else {
        stopPlaying();
    }
}

class PlayButton extends Button {
    boolean mStartPlaying = true;

    OnClickListener clicker = new OnClickListener() {
        public void onClick(View v) {
            onPlay(mStartPlaying);
            if (mStartPlaying) {
                setText("Stop playing");
            } else {
                setText("Start playing");
            }
            mStartPlaying = !mStartPlaying;
        }
    };

    public PlayButton(Context ctx) {
        super(ctx);
        setText("Start playing");
        setOnClickListener(clicker);
    }
}
/***************** File Saver ********************/

public AndroidVoiceActivity() {
    mFileName = Environment.getExternalStorageDirectory().getAbsolutePath();
    mFileName += "/audiorecordtest.3gp";
}

/***************** On Create ********************/
@Override
public void onCreate(Bundle icicle) {
    super.onCreate(icicle);
    //preparing linear layout
    LinearLayout ll = new LinearLayout(this);

    //record button
    mRecordButton = new RecordButton(this);
    ll.addView(mRecordButton,
        new LinearLayout.LayoutParams(
            ViewGroup.LayoutParams.WRAP_CONTENT,
            ViewGroup.LayoutParams.WRAP_CONTENT,
            0));

    //play button
    mPlayButton = new PlayButton(this);
    ll.addView(mPlayButton,
        new LinearLayout.LayoutParams(
            ViewGroup.LayoutParams.WRAP_CONTENT,
            ViewGroup.LayoutParams.WRAP_CONTENT,
            0));

    //setting linear layout
    setContentView(ll);
}

@Override
public void onPause() {
    super.onPause();
    if (mRecorder != null) {
        mRecorder.release();
        mRecorder = null;
    }

    if (mPlayer != null) {
        mPlayer.release();
        mPlayer = null;
    }
}

}

I read through a lot of code in order to make a simple voice recorder application. It works very well and it is pretty straight forward but now and I want to integrate it with some extra feature to display the user's current location using a simple utility, something like a text box. I understand java programming really well but I am still learning Android so I am a bit shaky on how to do things. I have read through many tutorials and examples, I have also tried many different approaches but nothing seems to work. I am developing for version 8 using Eclipse and Android SDK. I also have an android phone to test the app in which has proven to be very helpful.

Here is my code, this works and its an app that works as a voice recorder. Any help would be greatly appreciated:

import android.app.Activity;
import android.widget.LinearLayout;
import android.os.Bundle;
import android.os.Environment;
import android.view.ViewGroup;
import android.widget.Button;
import android.view.View;
import android.view.View.OnClickListener;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.media.MediaRecorder;
import android.media.MediaPlayer;
import java.io.IOException;


public class AndroidVoiceActivity extends Activity{
private static final String LOG_TAG = "AudioRecordTest";
private static String mFileName = null;

private RecordButton mRecordButton = null;
private MediaRecorder mRecorder = null;

private PlayButton   mPlayButton = null;
private MediaPlayer   mPlayer = null;

/***************** Record Button ********************/
private void onRecord(boolean start) {
    if (start) {
        startRecording();
    } else {
        stopRecording();
    }
}

private void startRecording() {
    mRecorder = new MediaRecorder();
    mRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
    mRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
    mRecorder.setOutputFile(mFileName);
    mRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);

    try {
        mRecorder.prepare();
    } catch (IOException e) {
        Log.e(LOG_TAG, "prepare() failed");
    }

    mRecorder.start();
}

private void stopRecording() {
    mRecorder.stop();
    mRecorder.release();
    mRecorder = null;
}

class RecordButton extends Button {
    boolean mStartRecording = true;

    OnClickListener clicker = new OnClickListener() {
        public void onClick(View v) {
            onRecord(mStartRecording);
            if (mStartRecording) {
                setText("Stop recording");
            } else {
                setText("Start recording");
            }
            mStartRecording = !mStartRecording;
        }
    };

    public RecordButton(Context ctx) {
        super(ctx);
        setText("Start recording");
        setOnClickListener(clicker);
    }
}

/***************** Play Button ********************/
private void startPlaying() {
    mPlayer = new MediaPlayer();
    try {
        mPlayer.setDataSource(mFileName);
        mPlayer.prepare();
        mPlayer.start();
    } catch (IOException e) {
        Log.e(LOG_TAG, "prepare() failed");
    }
}

private void stopPlaying() {
    mPlayer.release();
    mPlayer = null;
}

private void onPlay(boolean start) {
    if (start) {
        startPlaying();
    } else {
        stopPlaying();
    }
}

class PlayButton extends Button {
    boolean mStartPlaying = true;

    OnClickListener clicker = new OnClickListener() {
        public void onClick(View v) {
            onPlay(mStartPlaying);
            if (mStartPlaying) {
                setText("Stop playing");
            } else {
                setText("Start playing");
            }
            mStartPlaying = !mStartPlaying;
        }
    };

    public PlayButton(Context ctx) {
        super(ctx);
        setText("Start playing");
        setOnClickListener(clicker);
    }
}
/***************** File Saver ********************/

public AndroidVoiceActivity() {
    mFileName = Environment.getExternalStorageDirectory().getAbsolutePath();
    mFileName += "/audiorecordtest.3gp";
}

/***************** On Create ********************/
@Override
public void onCreate(Bundle icicle) {
    super.onCreate(icicle);
    //preparing linear layout
    LinearLayout ll = new LinearLayout(this);

    //record button
    mRecordButton = new RecordButton(this);
    ll.addView(mRecordButton,
        new LinearLayout.LayoutParams(
            ViewGroup.LayoutParams.WRAP_CONTENT,
            ViewGroup.LayoutParams.WRAP_CONTENT,
            0));

    //play button
    mPlayButton = new PlayButton(this);
    ll.addView(mPlayButton,
        new LinearLayout.LayoutParams(
            ViewGroup.LayoutParams.WRAP_CONTENT,
            ViewGroup.LayoutParams.WRAP_CONTENT,
            0));

    //setting linear layout
    setContentView(ll);
}

@Override
public void onPause() {
    super.onPause();
    if (mRecorder != null) {
        mRecorder.release();
        mRecorder = null;
    }

    if (mPlayer != null) {
        mPlayer.release();
        mPlayer = null;
    }
}

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

远山浅 2024-12-22 06:50:16

You should start out by taking a look at the Android developer docs

http://developer.android.com/guide/topics/location/obtaining-user-location.html

If you want to display the current address, look into Geocoder:

http://developer.android.com/reference/android/location/Geocoder.html

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文