android - 我该如何在后台使用此MP3?

标签 android audio background mp3 audio-player

这是utils代码,仅当打开应用程序时,MP3才起作用,一旦单击后退或主页按钮,它就会停止。

如何将其放在后台?

public class Util {

    public ArrayList<Contact> getAllContact(Context context) {

        ArrayList<Contact> contacts = new ArrayList<Contact>();
        Cursor cursor = context.getContentResolver().query(ContactsContract.Contacts.CONTENT_URI, null, null, null, ContactsContract.Contacts.DISPLAY_NAME);

         if(cursor != null) {
                while (cursor.moveToNext()) {  
                    // This would allow you get several email addresses  
                    // if the email addresses were stored in an array  
                    String name = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
                    String id = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID));
                    String phone = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER));
                    String aaaa = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.CUSTOM_RINGTONE));

                    if (phone!=null && phone.equals("1")) {
                         Contact contact = new Contact();
                        contact.setId(Integer.parseInt(id));
                        contact.setName(name);
                        contacts.add(contact);
                    }

                }
            }  
         cursor.close();
        return contacts;

    }

    public ArrayList<SongInfo> getAllSong(Context context) {

        ArrayList<SongInfo> listSong = new ArrayList<SongInfo>();

        RingtonesSharedPreferences pref = new RingtonesSharedPreferences(
                context);

        Field[] fields = R.raw.class.getFields();

        for (int i = 0; i < fields.length - 1; i++) {
            SongInfo info = new SongInfo();
            try {
                String name = fields[i].getName();

                if (!name.equals("ringtones")) {

                    info.setFileName(name + ".mp3");
                    info.setFavorite(pref.getString(info.getFileName()));
                    int audioResource = R.raw.class.getField(name).getInt(name);
                    info.setAudioResource(audioResource);
                }
                // info.setName(name);
            } catch (Exception e) {
                // TODO: handle exception
//              Log.e("LOG", "Error: " + e.getMessage());
            }
            listSong.add(info);
        }

        InputStream inputStream = context.getResources().openRawResource(
                R.raw.zeallist);
        BufferedReader reader = new BufferedReader(new InputStreamReader(
                inputStream));

        try {
            String line;
            int i = 0;
            while ((line = reader.readLine()) != null) {
                listSong.get(i).setName(line);
                i++;
            }

        } catch (Exception e) {
            // TODO: handle exception
        } finally {

            try {
                reader.close();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

        }

        return listSong;
    }


    public void assignRingtoneToContact(Context context, SongInfo info,Contact contact) {
        File dir =null;
        ContentValues values = new ContentValues();
        boolean isRingTone = false;

        if (Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED)) {
            dir = new File(Environment.getExternalStorageDirectory(),
                    "Ringtones");
        } else {
            dir = context.getCacheDir();
        }

        if (!dir.exists()) {
            dir.mkdirs();
        }

        File file = new File(dir, info.getFileName());
        if (!file.exists()) {
            try {
                file.createNewFile();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

            try {

                InputStream inputStream = context.getResources()
                        .openRawResource(info.getAudioResource());

                OutputStream outputStream = new FileOutputStream(file);

                byte[] buffer = new byte[1024];
                int length;

                while ((length = inputStream.read(buffer)) > 0) {
                    outputStream.write(buffer, 0, length);
                }
                outputStream.flush();
                outputStream.close();
                inputStream.close();
            } catch (Exception e) {
                // TODO: handle exception
            }
        }

            String[] columns = { MediaStore.Audio.Media.DATA,
                    MediaStore.Audio.Media._ID, 
                    MediaStore.Audio.Media.TITLE,
                    MediaStore.Audio.Media.DISPLAY_NAME,
                    MediaStore.Audio.Media.IS_RINGTONE
                    };

            Cursor cursor = context.getContentResolver().query(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, columns, MediaStore.Audio.Media.DATA+" = '"+file.getAbsolutePath()+"'",null, null);
            if (cursor!=null) {
                int idColumn = cursor.getColumnIndex(MediaStore.Audio.Media._ID);
                int fileColumn = cursor.getColumnIndex(MediaStore.Audio.Media.DATA);

                int ringtoneColumn = cursor.getColumnIndex(MediaStore.Audio.Media.IS_RINGTONE);
                while (cursor.moveToNext()) {
                    String audioFilePath = cursor.getString(fileColumn);
                    if (cursor.getString(ringtoneColumn)!=null && cursor.getString(ringtoneColumn).equals("1")) {
                        Uri hasUri = MediaStore.Audio.Media.getContentUriForPath(audioFilePath);
                        Uri fullUri = Uri.withAppendedPath(hasUri, cursor.getString(idColumn));
                        isRingTone = true;
                        values.put(ContactsContract.Contacts.CUSTOM_RINGTONE, fullUri.toString());
                    }
                }
                cursor.close();

                 if(!isRingTone){
                     context.sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE));

                     Uri oldUri = MediaStore.Audio.Media.getContentUriForPath(file.getAbsolutePath());
                     ContentValues Newvalues = new ContentValues();
                     Uri newUri;
                     String uriString;
                        context.getContentResolver().delete(oldUri, MediaStore.MediaColumns.DATA + "=\"" + file.getAbsolutePath() + "\"", null);
                        Newvalues.put(MediaStore.MediaColumns.DATA, file.getAbsolutePath());
                        Newvalues.put(MediaStore.MediaColumns.TITLE, info.getName());
                        Newvalues.put(MediaStore.MediaColumns.SIZE, file.length());
                        Newvalues.put(MediaStore.MediaColumns.MIME_TYPE, "audio/mp3");
                        Newvalues.put(MediaStore.Audio.Media.IS_RINGTONE, true);

                        Uri uri = MediaStore.Audio.Media.getContentUriForPath(file.getAbsolutePath());
                        newUri = context.getContentResolver().insert(uri, Newvalues);
                        uriString = newUri.toString();
                    values.put(ContactsContract.Contacts.CUSTOM_RINGTONE, uriString);
                    Log.i("LOG", "uriString: " + uriString);
                 }

            }

        int count = context.getContentResolver().update(ContactsContract.Contacts.CONTENT_URI, values,ContactsContract.Contacts._ID +" = "+contact.getId(), null);
//      Log.i("LOG", "Update: " + count);

    }

    @SuppressWarnings("deprecation")
    public Uri getContactContentUri() {
        if(Build.VERSION.SDK_INT >= 5){
            return ContactsContract.Contacts.CONTENT_URI;
        }
        else{
            return Contacts.People.CONTENT_URI;
        }
    }

}

最佳答案

尝试使用MediaPlayer,它有很多选择

MediaPlayer mediaPlayer = new MediaPlayer();
mediaPlayer.setDataSource(context, ringtone);
mediaPlayer.setAudioStreamType(AudioManager.STREAM_ALARM);
mediaPlayer.setLooping(true);
mediaPlayer.prepare();
mediaPlayer.start();

关于android - 我该如何在后台使用此MP3?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29063557/

相关文章:

android - 在Android应用中进行屏幕录制时阻止录制音频

image - 使用 CSS3 仅显示部分背景图像

java - 调用 fragment 时 fragment 选项菜单出现问题?

升级到 Xamarin Forms 2.0 后的 Android 资源问题

java - 我可以使用什么代替AudioPlayer.player.stop();?

ios4 - iOS4 上的 Phonegap 后台服务?

android - Voice Listen API 在后台使用更多电量

java - 安卓错误: Attempt to invoke virtual method 'java.lang.String android.content.Intent.getStringExtra(java.lang.String)' on a null object reference

javascript - 在输入字段模糊时隐藏 Nativescript 中的 Android 键盘

iphone - 将部分音频文件复制到 iOS 中的新文件中