2
Hello. I’m learning android and I’m trying to play a file. MID in my app.
On Activity Main has this code:
package com.micsgsoftwares.exemploplayermusic;
import androidx.appcompat.app.AppCompatActivity;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.View;
public class MainActivity extends AppCompatActivity {
public MediaPlayer mediaPlayer;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mediaPlayer = MediaPlayer.create(this, R.raw.h001);
}
public void CliqueBotao(View v){
mediaPlayer.start(); // no need to call prepare(); create() does that for you
}
}
The h001 file is in the res raw folder. I already put other files, only mp3, and did not show any error and played the file.
The error shown is as follows::
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.examplesoftware.exemploplayermusic, PID: 4890
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.examplesoftware.exemploplayermusic/com.examplesoftware.exemploplayermusic.MainActivity}: android.content.res.Resources$NotFoundException: File res/raw/h001.MID from drawable resource ID #0x7f0e0000
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2416)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
at android.app.ActivityThread.-wrap11(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
Caused by: android.content.res.Resources$NotFoundException: File res/raw/h001.MID from drawable resource ID #0x7f0e0000
at android.content.res.Resources.openRawResourceFd(Resources.java:1315)
at android.media.MediaPlayer.create(MediaPlayer.java:937)
at android.media.MediaPlayer.create(MediaPlayer.java:920)
at com.examplesoftware.exemploplayermusic.MainActivity.onCreate(MainActivity.java:18)
at android.app.Activity.performCreate(Activity.java:6237)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
at android.app.ActivityThread.-wrap11(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
Caused by: java.io.FileNotFoundException: This file can not be opened as a file descriptor; it is probably compressed
at android.content.res.AssetManager.openNonAssetFdNative(Native Method)
at android.content.res.AssetManager.openNonAssetFd(AssetManager.java:426)
at android.content.res.Resources.openRawResourceFd(Resources.java:1312)
at android.media.MediaPlayer.create(MediaPlayer.java:937)
at android.media.MediaPlayer.create(MediaPlayer.java:920)
at com.micsgsoftwares.exemploplayermusic.MainActivity.onCreate(MainActivity.java:18)
at android.app.Activity.performCreate(Activity.java:6237)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
at android.app.ActivityThread.-wrap11(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
I would like you to help me correct these mistakes.
EDIT
- Android usage 6.0 api 23.
I partially solved the problem by converting the MID files to mp3. Still more I wonder why I can’t play MID files on android if in the documentation of android studio it says you can play MID files: link: https://developer.android.com/guide/topics/media/mediaplayer?hl=pt-br#mediaplayer
One of the most important components of the media framework is the class Mediaplayer. An object of this class can search, decode and play audio and video with a minimum amount of setup. It is compatible with several different media sources such as:
- Local resources
- Internal Uris, as one you can get from a content solver
- External Urls (streaming)
To view a list of media formats compatible with Android, see the page Supported media formats.
I consulted the page Compatible media formats:
link: https://developer.android.com/guide/topics/media/formats?hl=pt-br#audio-codecs