2017-05-02 3 views
-2

Ich bin neu in Android-Entwicklung. Ich habe versucht, externe Audiodateien auf Android zu spielen. Aber diese Benachrichtigung wurde angezeigt. "Der Player unterstützt diese Art von Audiodatei nicht". Ich kann den Grund nicht verstehen.Wie spielt man externe Audiodateien in Android?

das ist mein Code.

AndroidMainfest.xml

<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
    package="com.example.administrator.a"> 

    <application 
     android:allowBackup="true" 
     android:icon="@mipmap/ic_launcher" 
     android:label="@string/app_name" 
     android:roundIcon="@mipmap/ic_launcher_round" 
     android:supportsRtl="true" 
     android:theme="@style/AppTheme"> 
     <provider 
      android:name="android.support.v4.content.FileProvider" 
      android:authorities="${applicationId}.provider" 
      android:exported="false" 
      android:grantUriPermissions="true"> 
      <meta-data 
       android:name="android.support.FILE_PROVIDER_PATHS" 
       android:resource="@xml/provider_paths" /> 
     </provider> 

     <activity android:name=".MainActivity"> 
      <intent-filter> 
       <action android:name="android.intent.action.MAIN" /> 

       <category android:name="android.intent.category.LAUNCHER" /> 
      </intent-filter> 
     </activity> 
     <activity android:name=".MusicActivity"> 
      <intent-filter> 
       <action android:name="com.example.administrator.a.MusicActivity" /> 

       <category android:name="android.intent.category.DEFAULT" /> 
      </intent-filter> 
     </activity> 
    </application> 

</manifest> 

MusicActivity.java

public class MusicActivity extends AppCompatActivity { 

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

     /* MediaPlayer player = MediaPlayer.create(this, R.raw.aa); 
     player.start();*/ 

     File audioPath = new File(Environment.getExternalStorageDirectory(), "musics"); 
     File audio = new File(audioPath, "1.mp3"); 
     if (!audio.exists()) { 
      return; 
     } 
     Uri uri = FileProvider.getUriForFile(getApplicationContext(), BuildConfig.APPLICATION_ID + ".provider", audio); 
     Intent intent = new Intent(Intent.ACTION_VIEW); 
     intent.setDataAndType(uri, "audio/mp3"); 
     startActivity(intent); 
    } 
} 

provider_paths.xml

<?xml version="1.0" encoding="utf-8"?> 
<paths xmlns:android="http://schemas.android.com/apk/res/android"> 
    <external-path name="external_files" path="."/> 
    <external-path name="imageFiles" path="images/"/> 
    <external-path name="pdfFiles" path="pdfs/"/> 
    <external-path name="musicFiles" path="musics/"/> 
</paths> 

die Datei "1.mp3" existiert bereits in Musiken subdiretory von sdcard. das ist mein Ergebnis des Laufens. enter image description here

bitte helfen Sie mir.

+0

hallo. Bheda, danke für deine Hilfe. –

+0

Ich habe es versucht. aber bedauerlicherweise war das Problem noch nicht behoben. –

+0

Wie kann ich jetzt tun? –

Antwort

1

, wenn Sie lesen und schreiben Inhalte von externen Speichern dann fügen Sie diese Berechtigung in der Manifest-Datei über dem Programm Tag

<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
    package="com.example.administrator.a"> 
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/> 
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/> 
    <application 
     android:allowBackup="true" 
     android:icon="@mipmap/ic_launcher" 
     android:label="@string/app_name" 
     android:roundIcon="@mipmap/ic_launcher_round" 
     android:supportsRtl="true" 
     android:theme="@style/AppTheme"> 
     <provider 
      android:name="android.support.v4.content.FileProvider" 
      android:authorities="${applicationId}.provider" 
      android:exported="false" 
      android:grantUriPermissions="true"> 
      <meta-data 
       android:name="android.support.FILE_PROVIDER_PATHS" 
       android:resource="@xml/provider_paths" /> 
     </provider> 

     <activity android:name=".MainActivity"> 
      <intent-filter> 
       <action android:name="android.intent.action.MAIN" /> 

       <category android:name="android.intent.category.LAUNCHER" /> 
      </intent-filter> 
     </activity> 
     <activity android:name=".MusicActivity"> 
      <intent-filter> 
       <action android:name="com.example.administrator.a.MusicActivity" /> 

       <category android:name="android.intent.category.DEFAULT" /> 
      </intent-filter> 
     </activity> 
    </application> 

</manifest> 
+0

Froh, dass es funktioniert. Bitte akzeptieren Sie meine Antwort. Sie können auch besuchen: https://StackOverflow.com/tour, um weitere Informationen über SO und unterstützen Sie mich so viel wie Sie können. –

Verwandte Themen