0

I am using Android Studio with MacBook, and copied and pasted an audio file in raw folder.

The audio file(mp3) which I want to use is 10.3MB, so Android Studio says, "The file size exceeds configured limit. Code insight features are not available." And, I resized the audio file with a random resizer online. However, when I resize the file, Android Studio says, "File was loaded in the wrong encoding: 'UTF-8'".

Now, I tried everything that I could find in stack overflow. I turned off bluetooth and stuff like that... Also, I deleted cache with:

~/Library/Preferences/AndroidStudio[Preview]_X.Y_
~/Library/Caches/AndroidStudio[Preview]_X.Y_
~/Library/Logs/AndroidStudio[Preview]_X.Y_
~/Library/Application Support/AndroidStudio[Preview]_X.Y_

Are there any advises? It seems like it would work on windows to just copy and paste an audio file in raw folder, but I am using Mac, so it's not working. This is how I thought.

1 Answers1

0

Have you tried playing the audio using something similar to this code as recommended by the Android Developers page?

// am1n
import androidx.appcompat.app.AppCompatActivity;
import android.media.AudioAttributes;
import android.media.MediaMetadataRetriever;
import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import java.io.File;
import java.io.IOException;

public class TestAudioActivity extends AppCompatActivity
{
    private String filepath;
    private File audioFile;
    private Uri audioUri;
    private MediaMetadataRetriever mediaMetadataRetriever;
    int audioDuration;
    private MediaPlayer mediaPlayer;
    private TextView audioTitle;
    private ImageView audioCloseButton;

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

        filepath = getIntent().getStringExtra("ITEMPATH");
        audioFile = new File(filepath);
        audioUri = Uri.fromFile(audioFile);

        // Getting audio duration in milliseconds.
        mediaMetadataRetriever = new MediaMetadataRetriever();
        mediaMetadataRetriever.setDataSource(getApplicationContext(), audioUri);
        audioDuration = Integer.parseInt(mediaMetadataRetriever.extractMetadata(mediaMetadataRetriever.METADATA_KEY_DURATION));
        Toast.makeText(getApplicationContext(), audioDuration + "", Toast.LENGTH_SHORT).show();

        mediaPlayer = new MediaPlayer();
        mediaPlayer.setAudioAttributes(new AudioAttributes.Builder().setContentType(AudioAttributes.CONTENT_TYPE_MUSIC).setUsage(AudioAttributes.USAGE_MEDIA).build());
        try
        {
            mediaPlayer.setDataSource(getApplicationContext(), audioUri);
            mediaPlayer.prepare();
            mediaPlayer.start();
        }
        catch(IOException e)
        {
            finish();
            e.printStackTrace();
        }

        audioTitle = findViewById(R.id.audio_title);
        audioCloseButton = findViewById(R.id.audio_close_button);
        audioCloseButton.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View v)
            {
                closeAudio();
            }
        });
    }

    // Audio file must be closed after job is done.
    public void closeAudio()
    {
        mediaPlayer.release();
        mediaPlayer = null;
        finish();
    }

    @Override
    public void onBackPressed()
    {
        closeAudio();
    }
}
mindoverflow
  • 730
  • 4
  • 13
  • No, I haven't. Can I have the xml file? Or URL? – HarukiMiya Dec 18 '21 at 04:04
  • You can create an arbitrary XML. That is not a problem. Just name the variables appropriately or remove the extra nodes from the code. The other thing of utmost importance is that since android 11 storage access has changed. Please refer to the answer here: – mindoverflow Dec 18 '21 at 09:44
  • https://stackoverflow.com/questions/66642726/java-io-filenotfoundexception-open-failed-eexist-file-exists-android-11/66645800#66645800 – mindoverflow Dec 18 '21 at 09:44
  • Im assuming here that youre trying to play audio on your android app, that you rin on the phone emulator. im guessing youre not trying to play audio on the android studio ide. For playing audio on the ide, which i dont know serves what purpose, you would possibly need to install a plugin for the ide. – mindoverflow Dec 18 '21 at 09:48
  • I somehow made it by restoring and restarting (File -> Manage IDE settings -> Restore default settings). Thanks anyways. – HarukiMiya Dec 19 '21 at 05:34
  • Great. Just in case, for future reference, this is how you'd program an Android app to play audio. Good luck. – mindoverflow Dec 20 '21 at 19:20