private View createViewFromResource( int position, View convertView, ViewGroup parent, int resource) { View view; if (convertView == null) { view = mInflater.inflate(resource, parent, false); } else { view = convertView; } AudioFile item = getItem(position); ImageView icon = (ImageView) view.findViewById(iconId); try { InputStream in = mContext.getContentResolver().openInputStream(item.getImageUri()); Bitmap bitmap = BitmapFactory.decodeStream(in); icon.setImageBitmap(bitmap); } catch (FileNotFoundException e) { icon.setImageResource(R.drawable.ic_tab_artists_grey); } TextView label = (TextView) view.findViewById(textId); label.setText(item.toString()); return view; }
private int transmit(final AudioFile audio) throws Exception { FileDescriptor fd = audio.getFileDescriptor(context); if (fd == null) { Log.e(TAG, "Null WAV File supplied to transmit()"); return 0; } if (mediaPlayer == null) initMediaPlayer(); if (mediaPlayer.isPlaying()) { return mediaPlayer.getDuration() - mediaPlayer.getCurrentPosition(); } mediaPlayer.setOnCompletionListener( new OnCompletionListener() { @Override public void onCompletion(MediaPlayer mp) { mp.reset(); // Delete audio file to save internal memory audio.delete(context); } }); try { mediaPlayer.setDataSource(fd); mediaPlayer.prepare(); mediaPlayer.start(); } catch (IllegalArgumentException e) { Log.e(TAG, "IllegalArgumentException in transmit()", e); return 0; } catch (IllegalStateException e) { Log.e(TAG, "IllegalStateException in transmit()", e); // Release and instantiate new MediaPlayer object just in case mediaPlayer.release(); initMediaPlayer(); return 0; } catch (IOException e) { Log.e(TAG, "IOException in transmit()", e); return 0; } Log.i(TAG, "Transmitting " + audio.getFilename() + ", " + mediaPlayer.getDuration() + "ms"); return mediaPlayer.getDuration(); }
@Override protected FilterResults performFiltering(CharSequence toFind) { FilterResults results = new FilterResults(); if (mOriginalValues == null) { synchronized (mLock) { mOriginalValues = new ArrayList<AudioFile>(mObjects); } } if (toFind == null || toFind.length() == 0) { synchronized (mLock) { ArrayList<AudioFile> list = new ArrayList<AudioFile>(mOriginalValues); results.values = list; results.count = list.size(); } } else { // TODO smarter regex String[] wordsToFind = toFind.toString().toLowerCase().split(" "); final ArrayList<AudioFile> newValues = new ArrayList<AudioFile>(); for (AudioFile file : mOriginalValues) { boolean keep = true; for (String word : wordsToFind) { if (!(file.getAlbum().toLowerCase().contains(word) || file.getArtist().toLowerCase().contains(word) || file.getTitle().toLowerCase().contains(word))) { keep = false; break; } } if (keep) { newValues.add(file); } } results.values = newValues; results.count = newValues.size(); } return results; }
private void insertMusicMetadata() throws IOException { atts.reset(); System.out.format("Extracting music metadata from %s.\n", src.path()); Tag tags = audio.getTag(); AudioHeader header = audio.getAudioHeader(); insert(FORMAT, header.getFormat()); insert(ENCODINGTYPE, header.getEncodingType()); insert(BITRATE, header.getBitRate()); insert(SAMPLERATE, header.getSampleRate()); insert(LENGTH, header.getTrackLength()); insert(ALBUM, tags.getFirst(FieldKey.ALBUM)); insert(ALBUMARTIST, tags.getFirst(FieldKey.ALBUM_ARTIST)); insert(ARTIST, tags.getFirst(FieldKey.ARTIST)); insert(TITLE, tags.getFirst(FieldKey.TITLE)); insert(TRACKNR, tags.getFirst(FieldKey.TRACK)); insert(DISCNR, tags.getFirst(FieldKey.DISC_NO)); insert(YEAR, tags.getFirst(FieldKey.YEAR)); insert(COMMENT, tags.getFirst(FieldKey.COMMENT)); }
private FileInfo extractFileInfo(Cursor cur, int type) { FileInfo fileInfo = null; switch (type) { case TYPE_IMAGE: fileInfo = ImageFile.getInfo(cur); break; case TYPE_AUDIO: fileInfo = AudioFile.getInfo(cur); break; case TYPE_VIDEO: fileInfo = VideoFile.getInfo(cur); break; } return fileInfo; }
public synchronized void initAudio() { if (null == mAudioCategory) { mAudioCategory = new ConcurrentHashMap<String, Category>(); } mAudioCategory.clear(); Uri uri = Audio.Media.EXTERNAL_CONTENT_URI; String[] columns = new String[] { BaseColumns._ID, MediaColumns.DATA, MediaColumns.DISPLAY_NAME, MediaColumns.SIZE, MediaColumns.DATE_ADDED, MediaColumns.DATE_MODIFIED, AudioColumns.DURATION, AudioColumns.ARTIST, AudioColumns.ALBUM, AudioColumns.ALBUM_ID, MediaColumns.MIME_TYPE, MediaColumns.TITLE }; Cursor cur = null; try { cur = mContext .getContentResolver() .query(uri, columns, buildSelectionByCategory(TYPE_AUDIO), null, null); if (cur == null) { return; } if (!cur.moveToFirst()) { return; } do { AudioFile file = AudioFile.getInfo(cur); if (null == file) { continue; } // 取一首歌曲的路径,add by yangbing // if (MediaOpenSettingConstants.sMusicPath == null) { // if (file.fullFilePath != null && !"".equals(file.fullFilePath)) { // MediaOpenSettingConstants.sMusicPath = file.fullFilePath; // } // } if (mAudioCategory.containsKey(file.album)) { Category category = mAudioCategory.get(file.album); category.addFile(file); } else { Category category = new Category(); category.filePath = file.album; category.uri = category.filePath; category.alias = file.album; category.addFile(file); mAudioCategory.put(category.filePath, category); } } while (cur.moveToNext()); } catch (SQLiteException e) { Log.e("FileEngine", "init audio error"); } finally { if (cur != null) { cur.close(); } } }