简体   繁体   English

Mediaplayer出现问题并在ListView中播放歌曲

[英]Issue with mediaplayer and play songs in listview

I have a problem with playing songs, which are in listview. 我在播放列表视图中的歌曲时遇到问题。

Songs are correctly save, but I can not to play them. 歌曲已正确保存,但我无法播放。

I have a path of songs, so everything should work... 我有一首歌,所以一切都应该正常工作...

Here is a code in oncreate and outside: 这是oncreate和outside中的代码:

  listView = (ListView) findViewById(R.id.filesList); ArrayList<String> FilesInFolder = GetFiles("/sdcard/Kandydaci"); listView.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, FilesInFolder)); listView.setOnItemClickListener(new AdapterView.OnItemClickListener() { public void onItemClick(AdapterView<?> parent, View v, int position, long id) { MediaPlayer mediaPlayer = new MediaPlayer(); try { File audioFile = getApplicationContext().getCacheDir(); String name = (String) parent.getItemAtPosition(position); String OUTPUT_FILE ="sdcard/Kandydaci/"+name; mediaPlayer.setDataSource(OUTPUT_FILE); mediaPlayer.prepare(); mediaPlayer.start(); } catch (IOException e) { e.printStackTrace(); } } }); public ArrayList<String> GetFiles(String DirectoryPath) { ArrayList<String> MyFiles = new ArrayList<String>(); File f = new File(DirectoryPath); f.mkdirs(); File[] files = f.listFiles(); if (files.length == 0) return null; else { for (int i=0; i<files.length; i++) MyFiles.add(files[i].getName()); } return MyFiles; } 

And here is bug, which I get after click on item in listview: 这是错误,单击列表视图中的项目后会得到此错误:

 E/ExtMediaPlayer-JNI: env->IsInstanceOf fails E/MediaPlayer-JNI: JNIMediaPlayerFactory: bIsQCMediaPlayerPresent 0 E/ExtMediaPlayer-JNI: env->IsInstanceOf fails E/MediaPlayer-JNI: JNIMediaPlayerFactory: bIsQCMediaPlayerPresent 0 E/FileSource: Failed to open file 'sdcard/Kandydaci/Thu Nov 30 17:36:34 GMT+01:00 2017.mp3'. (Permission denied) E/GenericSource: Failed to create data source! E/MediaPlayer: error (1, -2147483648) 

your code is absolutely correct but problem is you missed defining permission to access EXTERNAL STORAGE. 您的代码是绝对正确的,但是问题是您错过了定义访问EXTERNAL STORAGE的权限。

So go like that. 所以就这样吧。

Declare in manifest: 声明清单:

   <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

Now add this code before list setting: 现在,在列表设置之前添加以下代码:

If(ContextCompat.checkSelfPermission(MainActivity.this, permission) != PackageManager.PERMISSION_GRANTED) 
 { 
      // Should we show an explanation?
      if (ActivityCompat.shouldShowRequestPermissionRationale(MainActivity.this, permission)) 
      {
            //This is called if user has denied the permission before
            //In this case I am just asking the permission again 
           ActivityCompat.requestPermissions(MainActivity.this, new String[]{permission}, requestCode); 
     }
     else 
     { 
           ActivityCompat.requestPermissions(MainActivity.this, new String[]{permission}, requestCode);
     } 
 }
 else {
       Toast.makeText(this, "" + permission + " is already granted.", Toast.LENGTH_SHORT).show(); 

  }

By this code you will get the storage access permissions, here permission is: 通过此代码,您将获得存储访问权限,这里的权限是:

   String ="Manifest.permission.WRITE_EXTERNAL_STORAGE";

Happy coding!! 编码愉快!!

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

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