Android媒体播放器,文件未找到异常,没有此类文件或目录

时间:2014-11-10 13:37:09

标签: android

为媒体播放器设置数据源时,会引发以下异常。找不到文件异常。 "开放失败:ENOENT"。没有相应的文件和目录。但文件" kick.mp3"存在于目录" / home / user / Downloads /"中。如何设置数据源。我在ubuntu 13.10(Linux)上使用android

MainActivity.java

 public void play(View v){
      try{
      mPlayer = new MediaPlayer();

            mPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC); 


            String filePath = "/home/user/Downloads/kick.mp3";
            File file = new File(filePath);
            FileInputStream inputStream = new FileInputStream(file);
            mPlayer.setDataSource(inputStream.getFD());
            inputStream.close();

          mPlayer.prepare();
         mPlayer.start();
         button.setText("pause");
            }
        }catch (Exception e){
            Log.e("mPlayer 2", ""+e);
        }
}

2 个答案:

答案 0 :(得分:0)

尝试在project res文件夹中创建一个名为raw的文件夹,然后将mp3放在那里

*编辑:执行此操作后,您无法使用fileinputstream,因为您可以访问该文件:

mp = MediaPlayer.create(this, R.raw.yourfilename);

确保你的文件名没有大写字母,因为这会引发很多错误&不工作

答案 1 :(得分:0)

String filePath =“/ home / user / Download / kick.mp3”;

您定义的文件路径位于您的ubuntu操作系统中,它不是Android媒体文件的文件路径。 它对Android操作系统不可见。

要首先访问Android媒体,您必须通过在内容解析器上传递查询来获取媒体。

/**
     * This defines the music songs query constants
     */
    public interface SongsQuery {
        // External id to be used by loader
        final static int QUERY_ID = SequenceGenerator.getUniqueId();
        final static Uri CONTENT_URI = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
        final static String[] PROJECTIONS = {
                MediaStore.Audio.Media._ID,
                MediaStore.Audio.Media.ARTIST,
                MediaStore.Audio.Media.ALBUM,
                MediaStore.Audio.Media.ALBUM_ID,
                MediaStore.Audio.Media.TITLE,
                MediaStore.Audio.Media.TRACK,
                MediaStore.Audio.Media.DATA
        };
        final static String SELECTION = MediaStore.Audio.Media.IS_MUSIC + "=" + "?";
        final static String[] SELECTION_ARGS = {"1"};

        final static int ID = 0;
        final static int ARTIST = 1;
        final static int ALBUM = 2;
        final static int ALBUM_ID = 3;
        final static int TITLE = 4;
        final static int TRACK = 5;
        final static int DATA = 6;
    }

public class AndroidArtist implements MediaArtist {

    private final String mName;
    private final URI mArtistURI;
}

public class AndroidMediaItem implements MediaItem {

    private final MediaType mMediaType;
    private final String mName;
    private final AndroidGenre mMediaGenre;
    private final URI mFileURI;
    private final URI mMediaArturi;
}


 private List<MediaItem> mMediaList = new ArrayList<MediaItem>();


    public List<MediaItem> loadInBackground() {
        Cursor genreQuery = getContext().getContentResolver().query(GenreQuery.CONTENT_URI,
                ContentQuery.GenreQuery.PROJECTIONS,
                GenreQuery.SELECTION,
                GenreQuery.SELECTION_ARGS, null);

        if (genreQuery != null) {
            for (genreQuery.moveToFirst(); !genreQuery.isAfterLast(); genreQuery.moveToNext()) {
                String genreName = genreQuery.getString(GenreQuery.GENRE_NAME);
                int genreId = genreQuery.getInt(GenreQuery.GENRE_ID);

                Uri genreMemberUri = MediaStore.Audio.Genres.Members.getContentUri("external", genreId);
                Cursor songsQuery = getContext().getContentResolver().query(genreMemberUri,
                        SongsQuery.PROJECTIONS,
                        SongsQuery.SELECTION,
                        SongsQuery.SELECTION_ARGS, null);

                if (songsQuery != null) {
                    for (songsQuery.moveToFirst(); !songsQuery.isAfterLast(); songsQuery.moveToNext()) {
                        File mediaFile = new File(songsQuery.getString(SongsQuery.DATA));
                        if (mediaFile.exists()) {
                            AndroidArtist musicArtist = new AndroidArtist(songsQuery.getString(SongsQuery.ARTIST), null);
                            List< AndroidArtist > mediaArtists = new ArrayList< AndroidArtist >();
                            mediaArtists.add(musicArtist);
                            Uri albumArtUri = ContentUris.withAppendedId(sMusicArtworkUri, songsQuery.getLong(SongsQuery.ALBUM_ID));
                            URI mediaArtURI = null;
                            if (albumArtUri != null) {
                                try {
                                    mediaArtURI = new URI(albumArtUri.toString());
                                } catch (URISyntaxException e) {
                                    e.printStackTrace();
                                }
                            }

                            AndroidMediaItem mediaItem = new AndroidMediaItem(MediaType.AUDIO,
                                    songsQuery.getString(SongsQuery.TITLE),
                                    mediaGenre,  URI.create(LibVLC.PathToURI(mediaFile.toURI().getPath())), mediaArtURI);

                            mMediaList.add(mediaItem);
                        }
                    }
                    songsQuery.close();
                }
            }
        }
        if (genreQuery != null) {
            genreQuery.close();
        }


        return mMediaList;
    }