Error creating MediaPlayer with Uri or file in assets

前端 未结 2 688
南旧
南旧 2021-01-04 13:38

I copied song.mp3 to my project\'s assets directory and wrote this code:

private MediaPlayer mp;

Uri uri = Uri.parse(\"file:///android_asset/song.mp3\");

m         


        
相关标签:
2条回答
  • 2021-01-04 14:10

    Try this and see if any exceptions are caught:

    try {
        MediaPlayer mp = new MediaPlayer();
        mp.setDataSource(this, uri);
    }
    catch (NullReferenceArgument e) {
        Log.d(TAG, "NullReferenceException: " + e.getMessage());
    }
    catch (IllegalStateException e) {
        Log.d(TAG, "IllegalStateException: " + e.getMessage());
    }
    catch (IOException e) {
        Log.d(TAG, "IOException: " + e.getMessage());
    }
    catch (IllegalArgumentException e) {
        Log.d(TAG, "IllegalArgumentException: " + e.getMessage());
    }
    catch (SecurityException e) {
        Log.d(TAG, "SecurityException: " + e.getMessage());
    }
    

    The exception caught will explain what is going wrong in your create. According the the docs, the static create method is just shorthand for what is in the try block above. The major difference that I can see is that the static method create doesn't throw while setDataSource does.

    0 讨论(0)
  • 2021-01-04 14:14

    Try this:

    try {
        AssetFileDescriptor afd = getAssets().openFd("AudioFile.mp3");
        player = new MediaPlayer();
        player.setDataSource(afd.getFileDescriptor(),afd.getStartOffset(),afd.getLength());
        player.prepare();
        player.start();
        } 
    catch (IllegalArgumentException e) {    } 
    catch (IllegalStateException e) { } 
    catch (IOException e) { } 
    
    0 讨论(0)
提交回复
热议问题