简体   繁体   English

Android MediaPlayer未知错误

[英]Android MediaPlayer unknown error

I am creating a MediaPlayer instance in MainActivity's onCreate() method like this 我正在这样在MainActivity的onCreate()方法中创建MediaPlayer实例

 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: 也尝试过此操作,但在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. 我也尝试过搜索关于stackoverflow的答案,但无法解决问题。 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; 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. 因此,将SongTitles放置在SongList中,后者是String类型的ArraList。

Hope This Helps. 希望这可以帮助。

Try using a FileInputStream to start your MediaPlayer with a FileDescriptor instead: 尝试使用FileInputStream来使用FileDescriptor启动MediaPlayer:

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());
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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