简体   繁体   中英

Android MediaPlayer unknown error

I am creating a MediaPlayer instance in MainActivity's onCreate() method like this

 MediaPlayer mPlayer = MediaPlayer.create(this, Uri.fromFile(new File("/storage/emulated/0/soundrecorder/My recording #26.wav")));

It is created successfully but I get this error:

07-06 18:33:44.266 18366-18366/com.audiorecorder.wel.voicerecorder E/MediaPlayer-JNI: JNIMediaPlayerFactory: bIsQCMediaPlayerPresent 0
07-06 18:33:44.267 18366-18366/com.audiorecorder.wel.voicerecorder E/MediaPlayer-JNI: JNIMediaPlayerFactory: bIsQCMediaPlayerPresent 0

Also tried this but the same error on logcat:

MediaPlayer mp = new MediaPlayer();
    try {
        mp.setDataSource(this, Uri.fromFile(new File("/storage/emulated/0/soundrecorder/My recording #26.wav")));
    } catch (IOException e) {
        e.printStackTrace();
    }
    mp.prepareAsync();
    mp.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
        @Override
        public void onPrepared(MediaPlayer mp) {
            mp.start();
        }
    });

I have tried different audio files with different formats but result is the same error. I have also tried searching the answer on stackoverflow but could not resolve the issue. Can you help me on this?

It seems there is some issue with your file. Update that with this :

musicUri = android.provider.MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;

And then follow these steps:

    1.  getLoaderManager().initLoader(0,null,this);
    2.  @Override
        public Loader<Cursor> onCreateLoader(int id, Bundle args) {
            switch (id){
                case 0 : return new CursorLoader(getApplicationContext(),musicUri,null,null,null,null);        
            return new Loader<>(this);
        }

3. @Override
    public void onLoadFinished(Loader<Cursor> loader, Cursor data) {
        switch (loader.getId()) {
            case 0 :
                if(data != null && data.moveToFirst()) {
                    songTitleColumnIndex = data.getColumnIndex(MediaStore.Audio.Media.TITLE);
                    do {
                        songTitle = data.getString(songTitleColumnIndex);
                        songsList.add(songTitle);
                    } while (data.moveToNext());
                }
                break;

So the SongTitles are being put in the SongList which is an ArraList of String Type.

Hope This Helps.

Try using a FileInputStream to start your MediaPlayer with a FileDescriptor instead:

String yourFilePath = "/wherever/your/file/is.wav";

MediaPlayer mPlayer = new MediaPlayer();

try{
    FileInputStream inputStream = new FileInputStream(yourFilePath);
    mPlayer.setDataSource(inputStream.getFD());
    inputStream.close();

    mPlayer.prepare();
    mPlayer.start();

}catch (IOException e){
    Log.e("IOException", e.getMessage());
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM