Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //MainAvtivity
- package com.example.media;
- import android.Manifest;
- import android.content.pm.PackageManager;
- import android.media.AudioManager;
- import android.media.MediaPlayer;
- import android.media.MediaRecorder;
- import android.media.audiofx.Visualizer;
- import android.net.Uri;
- import android.os.Handler;
- import android.os.Message;
- import android.os.Bundle;
- import android.util.Log;
- import android.view.View;
- import android.widget.AdapterView;
- import android.widget.ArrayAdapter;
- import android.widget.Button;
- import android.widget.SeekBar;
- import android.widget.Spinner;
- import android.widget.TextView;
- import android.widget.Toast;
- import androidx.appcompat.app.AppCompatActivity;
- import androidx.core.app.ActivityCompat;
- import com.example.media.R;
- import com.example.media.VisualizerView;
- import java.io.IOException;
- public class MainActivity extends AppCompatActivity {
- Button playBtn;
- SeekBar positionBar;
- SeekBar volumeBar;
- TextView elapsedTimeLabel;
- TextView remainingTimeLabel;
- MediaPlayer mp;
- int totalTime;
- Spinner dropdown;
- final String[] items = {"music_mp3", "music_amr", "music_flac", "music_m4a", "music_m4r", "music_mp2", "music_ogg", "music_wav"};
- private Button play, stop, record;
- private MediaRecorder myAudioRecorder;
- private String outputFile;
- VisualizerView mVisualizerView;
- private Visualizer mVisualizer;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- playBtn = (Button) findViewById(R.id.playBtn);
- elapsedTimeLabel = (TextView) findViewById(R.id.elapsedTimeLabel);
- remainingTimeLabel = (TextView) findViewById(R.id.remainingTimeLabel);
- positionBar = (SeekBar) findViewById(R.id.positionBar);
- volumeBar = (SeekBar) findViewById(R.id.volumeBar);
- // Media Player
- mp = MediaPlayer.create(this, Uri.parse("android.resource://com.example.media/raw/" + items[0]));
- mp.setLooping(true);
- mp.seekTo(0);
- mp.setVolume(0.5f, 0.5f);
- totalTime = mp.getDuration();
- // Position Bar
- positionBar.setMax(totalTime);
- positionBar.setOnSeekBarChangeListener(
- new SeekBar.OnSeekBarChangeListener() {
- @Override
- public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
- if (fromUser) {
- mp.seekTo(progress);
- positionBar.setProgress(progress);
- }
- }
- @Override
- public void onStartTrackingTouch(SeekBar seekBar) {
- }
- @Override
- public void onStopTrackingTouch(SeekBar seekBar) {
- }
- }
- );
- // Volume Bar
- volumeBar.setOnSeekBarChangeListener(
- new SeekBar.OnSeekBarChangeListener() {
- @Override
- public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
- float volumeNum = progress / 100f;
- mp.setVolume(volumeNum, volumeNum);
- }
- @Override
- public void onStartTrackingTouch(SeekBar seekBar) {
- }
- @Override
- public void onStopTrackingTouch(SeekBar seekBar) {
- }
- }
- );
- // Thread (Update positionBar & timeLabel)
- new Thread(new Runnable() {
- @Override
- public void run() {
- while (mp != null) {
- try {
- Message msg = new Message();
- msg.what = mp.getCurrentPosition();
- handler.sendMessage(msg);
- Thread.sleep(1000);
- } catch (InterruptedException e) {
- }
- }
- }
- }).start();
- //spinner
- dropdown = (Spinner) findViewById(R.id.spinner1);
- ArrayAdapter<String> adapter = new ArrayAdapter<>(this, android.R.layout.simple_spinner_dropdown_item, items);
- dropdown.setAdapter(adapter);
- dropdown.setSelection(0, false);
- dropdown.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
- @Override
- public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
- Log.d("listener", "wybrano obiekt " + id);
- // Media Player
- mp.stop();
- mp.release();
- mp = null;
- playBtn.setBackgroundResource(R.drawable.play);
- mp = MediaPlayer.create(getApplicationContext(), Uri.parse("android.resource://com.example.lab6/raw/" + items[(int) id]));
- mp.setLooping(true);
- mp.seekTo(0);
- mp.setVolume(0.5f, 0.5f);
- totalTime = mp.getDuration();
- // Position Bar
- positionBar.setMax(totalTime);
- mVisualizer.release();
- mVisualizer = null;
- mVisualizerView = null;
- mVisualizerView = (VisualizerView) findViewById(R.id.myvisualizerview);
- initAudio();
- }
- @Override
- public void onNothingSelected(AdapterView<?> parent) {
- }
- });
- mVisualizerView = (VisualizerView) findViewById(R.id.myvisualizerview);
- initAudio();
- //Recorder
- play = (Button) findViewById(R.id.play);
- stop = (Button) findViewById(R.id.stop);
- record = (Button) findViewById(R.id.record);
- stop.setEnabled(false);
- play.setEnabled(false);
- outputFile = getExternalFilesDir(null) + "/recording.mp3";
- myAudioRecorder = new MediaRecorder();
- if (ActivityCompat.checkSelfPermission(this, Manifest.permission.RECORD_AUDIO) != PackageManager.PERMISSION_GRANTED) {
- if (ActivityCompat.checkSelfPermission(this, Manifest.permission.RECORD_AUDIO)
- != PackageManager.PERMISSION_GRANTED) {
- ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.RECORD_AUDIO},
- 10);
- }
- }
- myAudioRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
- myAudioRecorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
- myAudioRecorder.setAudioEncoder(MediaRecorder.OutputFormat.MPEG_4);
- myAudioRecorder.setOutputFile(outputFile);
- record.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- if (myAudioRecorder == null) {
- myAudioRecorder = new MediaRecorder();
- myAudioRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
- myAudioRecorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
- myAudioRecorder.setAudioEncoder(MediaRecorder.OutputFormat.MPEG_4);
- myAudioRecorder.setOutputFile(outputFile);
- }
- try {
- myAudioRecorder.prepare();
- myAudioRecorder.start();
- } catch (IllegalStateException ise) {
- Log.e("Record ERROR ise", "error");
- } catch (IOException ioe) {
- Log.e("Record ERROR ioe", "error");
- }
- record.setEnabled(false);
- stop.setEnabled(true);
- Toast.makeText(getApplicationContext(), "Recording started", Toast.LENGTH_LONG).show();
- }
- });
- stop.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- try {
- myAudioRecorder.stop();
- } catch (RuntimeException e) {
- Log.e("Stop recording ERROR", "error");
- } finally {
- myAudioRecorder.release();
- myAudioRecorder = null;
- }
- record.setEnabled(true);
- stop.setEnabled(false);
- play.setEnabled(true);
- Toast.makeText(getApplicationContext(), "Audio Recorder successfully", Toast.LENGTH_LONG).show();
- }
- });
- play.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- MediaPlayer mediaPlayer = new MediaPlayer();
- try {
- mediaPlayer.setDataSource(outputFile);
- mediaPlayer.prepare();
- mediaPlayer.start();
- Toast.makeText(getApplicationContext(), "Playing Audio", Toast.LENGTH_LONG).show();
- } catch (Exception e) {
- Log.e("Play ERROR", "error playing recorded song");
- }
- }
- });
- Log.d("Path to output file:", outputFile);
- }
- @Override
- protected void onPause() {
- super.onPause();
- if (isFinishing() && mp != null) {
- mVisualizer.release();
- mp.release();
- mp = null;
- myAudioRecorder.release();
- myAudioRecorder = null;
- }
- }
- private Handler handler = new Handler() {
- @Override
- public void handleMessage(Message msg) {
- int currentPosition = msg.what;
- // Update positionBar.
- positionBar.setProgress(currentPosition);
- // Update Labels.
- String elapsedTime = createTimeLabel(currentPosition);
- elapsedTimeLabel.setText(elapsedTime);
- String remainingTime = createTimeLabel(totalTime - currentPosition);
- remainingTimeLabel.setText("- " + remainingTime);
- }
- };
- public String createTimeLabel(int time) {
- String timeLabel = "";
- int min = time / 1000 / 60;
- int sec = time / 1000 % 60;
- timeLabel = min + ":";
- if (sec < 10) timeLabel += "0";
- timeLabel += sec;
- return timeLabel;
- }
- public void playBtnClick(View view) {
- if (!mp.isPlaying()) {
- // Stopping
- mp.start();
- playBtn.setBackgroundResource(R.drawable.stop);
- } else {
- // Playing
- mp.pause();
- playBtn.setBackgroundResource(R.drawable.play);
- }
- }
- private void initAudio() {
- setVolumeControlStream(AudioManager.STREAM_MUSIC);
- setupVisualizerFxAndUI();
- // Make sure the visualizer is enabled only when you actually want to
- // receive data, and
- // when it makes sense to receive data.
- mVisualizer.setEnabled(true);
- // When the stream ends, we don't need to collect any more data. We
- // don't do this in
- // setupVisualizerFxAndUI because we likely want to have more,
- // non-Visualizer related code
- // in this callback.
- mp
- .setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
- public void onCompletion(MediaPlayer mediaPlayer) {
- mVisualizer.setEnabled(false);
- }
- });
- }
- private void setupVisualizerFxAndUI() {
- // Create the Visualizer object and attach it to our media player.
- mVisualizer = new Visualizer(mp.getAudioSessionId());
- mVisualizer.setCaptureSize(Visualizer.getCaptureSizeRange()[1]);
- mVisualizer.setDataCaptureListener(
- new Visualizer.OnDataCaptureListener() {
- public void onWaveFormDataCapture(Visualizer visualizer,
- byte[] bytes, int samplingRate) {
- mVisualizerView.updateVisualizer(bytes);
- }
- public void onFftDataCapture(Visualizer visualizer,
- byte[] bytes, int samplingRate) {
- }
- }, Visualizer.getMaxCaptureRate() / 2, true, false);
- }
- }
- -------------------------------------------------------
- //VisualView
- package com.example.media;
- import android.content.Context;
- import android.graphics.Canvas;
- import android.graphics.Color;
- import android.graphics.Paint;
- import android.graphics.Rect;
- import android.util.AttributeSet;
- import android.view.View;
- public class VisualizerView extends View {
- private byte[] mBytes;
- private float[] mPoints;
- private Rect mRect = new Rect();
- private Paint mForePaint = new Paint();
- public VisualizerView(Context context) {
- super(context);
- init();
- }
- public VisualizerView(Context context, AttributeSet attrs) {
- super(context, attrs);
- init();
- }
- public VisualizerView(Context context, AttributeSet attrs, int defStyleAttr) {
- super(context, attrs, defStyleAttr);
- init();
- }
- private void init() {
- mBytes = null;
- mForePaint.setStrokeWidth(1f);
- mForePaint.setAntiAlias(true);
- mForePaint.setColor(Color.rgb(0, 128, 255));
- }
- public void updateVisualizer(byte[] bytes) {
- mBytes = bytes;
- invalidate();
- }
- @Override
- protected void onDraw(Canvas canvas) {
- super.onDraw(canvas);
- if (mBytes == null) {
- return;
- }
- if (mPoints == null || mPoints.length < mBytes.length * 4) {
- mPoints = new float[mBytes.length * 4];
- }
- mRect.set(0, 0, getWidth(), getHeight());
- for (int i = 0; i < mBytes.length - 1; i++) {
- mPoints[i * 4] = mRect.width() * i / (mBytes.length - 1);
- mPoints[i * 4 + 1] = mRect.height() / 2
- + ((byte) (mBytes[i] + 128)) * (mRect.height() / 2) / 128;
- mPoints[i * 4 + 2] = mRect.width() * (i + 1) / (mBytes.length - 1);
- mPoints[i * 4 + 3] = mRect.height() / 2
- + ((byte) (mBytes[i + 1] + 128)) * (mRect.height() / 2)
- / 128;
- }
- canvas.drawLines(mPoints, mForePaint);
- }
- }
- -----------------------------------------------------------------------------------------------
- // Manifest
- <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
- <uses-permission android:name="android.permission.RECORD_AUDIO" />
- -----------------------------------------------------------------------------------------------------
- XML layout
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="com.example.lab6.MainActivity" android:orientation="vertical" android:gravity="center_horizontal">
- <Spinner android:id="@+id/spinner1" android:layout_width="match_parent" android:layout_height="wrap_content"/>
- <SeekBar android:id="@+id/positionBar" android:layout_width="300dp" android:layout_height="wrap_content" android:layout_marginTop="30dp"/>
- <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content">
- <TextView android:id="@+id/elapsedTimeLabel" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="0:11" android:layout_marginLeft="40dp"/>
- <TextView android:id="@+id/remainingTimeLabel" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="-1:49" android:layout_marginLeft="240dp"/>
- </LinearLayout>
- <Button android:id="@+id/playBtn" android:layout_width="30dp" android:layout_height="30dp" android:background="@drawable/play" android:layout_marginTop="40dp" android:onClick="playBtnClick"/>
- <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" android:layout_marginTop="40dp" android:gravity="center">
- <ImageView android:layout_width="18dp" android:layout_height="18dp" android:src="@drawable/sound1"/>
- <SeekBar android:id="@+id/volumeBar" android:layout_width="300dp" android:layout_height="wrap_content" android:progress="50" android:max="100"/>
- <ImageView android:layout_width="26dp" android:layout_height="26dp" android:src="@drawable/sound2"/>
- </LinearLayout>
- <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Record" android:textSize="20sp" android:id="@+id/record" android:layout_centerHorizontal="true" android:layout_marginTop="15dp"/>
- <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Stop" android:textSize="20sp" android:id="@+id/stop" android:layout_below="@id/record" android:layout_marginTop="10dp" android:layout_centerHorizontal="true"/>
- <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Play" android:textSize="20sp" android:id="@+id/play" android:layout_below="@id/stop" android:layout_marginTop="10dp" android:layout_centerHorizontal="true"/>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context="com.example.androidaudiovisualizer.MainActivity">
- <com.example.lab6.VisualizerView android:id="@+id/myvisualizerview" android:layout_width="match_parent" android:layout_height="match_parent"/>
- </LinearLayout>
- </LinearLayout>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement